Create Calculation View - SQL Script Table Functions Procedure
Create Calculation View - SQL Script Table Functions Procedure
Community
Former Member
September 1, 2016
| 7 minute read
Hello Everyone, When I was trying to learn HANA scripted calculation view, I had
Like to spend lot of time in creating tables, views and data records in order to get my
hands dirty and learn how the scripted calculation views works. What I am trying
to do here is gather all of these information in this blog so that one can create
RSS Feed their own tables, data and finally scripted calculation Views in the following ways.
2. Table Functions
3. Procedure
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 1/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
Please note here we will not learn how to create the above shown graphical view but
learn how to create this calcualtion view using SQL script.
Step1: Create a new calculation view of the type script as shown below.
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 2/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
Step 2: Here start writing the SQL script code. We will use CE functions here. I will not
spend time in explaining what each CE function does. It has been wonderfully
described here. Please refer that. I will show below how the code looks like. Also make
sure you have created Columns on the right hand side for the VAR_OUT to export the
tabular data. Please see below.
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 3/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
region = CE_COLUMN_TABLE(“HANA_TUTORIAL”.“REGION”,
[ “REGION_ID” ,
“REGION_NAME” ,
“SUB_REGION_NAME”]);
sales = CE_COLUMN_TABLE(“HANA_TUTORIAL”.“SALES”,
[ “REGION_ID”,
“PRODUCT_ID”,
“SALES_AMOUNT” ]);
product =
CE_COLUMN_TABLE(“HANA_TUTORIAL”.“PRODUCT”,
[ “PRODUCT_ID”,
“PRODUCT_NAME” ]);
join1 = CE_JOIN(:region, :sales,
[“REGION_ID”],
[“REGION_ID”,
“SUB_REGION_NAME”,
“PRODUCT_ID”,
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 4/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
[“PRODUCT_ID”],
[“PRODUCT_ID”,
“PRODUCT_NAME”,
“REGION_ID”,
“SUB_REGION_NAME”,
“SALES_AMOUNT”]);
p_out = CE_PROJECTION(:join2,
[“PRODUCT_ID”,
“PRODUCT_NAME”,
“REGION_ID”,
“SUB_REGION_NAME”,
“SALES_AMOUNT”,
CE_CALC(‘”SALES_AMOUNT”*0.3’,DOUBLE) as “MARGIN”
]);
END;
/********* End Procedure Script ************/
Step 1: Please go to developement Perspective first. Choose the tab ‘Repository’ and
right click on the content package and ‘Import remote workspace’.
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 5/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
Step 2: Rightclick on the package ‘New->Other’ Search for and choose ‘Table
Function’. Click next and Put the name as shown below
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 6/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 7/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
Code:
FUNCTION “SYSTEM”.“P1942191456::TBF_GET_SALES_DATA” ( )
RETURNS TABLE (
“PRODUCT_ID” int,
“PRODUCT_NAME” varchar(100),
“REGION_ID” int,
“SUB_REGION_NAME” varchar(100),
“SALES_AMOUNT” double, “MARGIN” double )
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
/*****************************
Write your function logic
*****************************/
RETURN select b.“PRODUCT_ID”, c.“PRODUCT_NAME”, a.“REGION_ID”,
a.“SUB_REGION_NAME”, b.“SALES_AMOUNT”,
get_margin(b.“SALES_AMOUNT”) as “MARGIN”
from
“HANA_TUTORIAL”.“REGION” as a
inner join
“HANA_TUTORIAL”.“SALES” as b
on a.“REGION_ID” = b.“REGION_ID”
inner Join “HANA_TUTORIAL”.“PRODUCT” as c
on b.“PRODUCT_ID” = c.“PRODUCT_ID”;
END;
I have added a scaler user defined function called ‘GET_MARGIN’ in the above code.
You can refer to how to build a scaler user defined functions in this wonderful blog
written by Rich Heilman here. Please find the code below.
/* Begin of Code
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 8/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
/* End of Code
Step 4: Now create a claculation view of the type graphical. Put the name and
description. Keep everything as default and in the ‘Aggregation’ node click in add an
object as you normally do. Here you will find the Table function you have just created
as a table. Please see below
You have now added the table function like you do for tables or views. Add the fields
to output and in the ‘Semantics’ you can choose ‘SALES_AMOUNT’ and ‘MARGIN’ as
measures and rest as attributes.
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 9/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
Step 1: Create a procedure similarly as we did in SQL script using CE Functions in the
biginning of this blog. Under Catalogue->your_schema->procedure, right click and
create a new procedure. Also create the output parameters, let’s say P_OUT and
make sure it has all the fields you would want in the final CE_PROJECTION function.
In turn structure of P_OUT should be equivalent to structure of VAR_OUT in the final
scripted calculation view. This will get clearer in the next step.
The above code is an exact copy as shown in ‘SQL Script – Using CE Functions’ above
Step 2: Create a new Calculation View of the type script. And in the script area put
Alert Moderator
the code as shown below. Also donot forget to create the output structure. It is
shown in the below image on the right hand side under Output. This structure is the
‘Var_Out’ structure and is equivalent to the P_OUT we had created in the above step
Assigned Tags in the procedure. (Assign the correct datatypes for the fields). Lastly go to schema ‘
_SYS_BIC -> Procedure’ and drag the Procedure in the scripting workbench, as
SAP HANA shown below.
Database
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 10/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
Related Questions
How Scripted Calculation View executed internally in HANA memory
By
Jyoti Senapati Oct 09, 2017
What Exactly Difference between Scripted Base calculation View and Stored Procedure....
By
Raju Skh Jan 07, 2019
Coffee Corner
Join the new Coffee Corner Discussion Group.
3 Comments
Florian Pfeffer
September 2, 2016 at 5:46 am
Hello Anik,
Scripted calculation views are a kind of obsolete technique. The future way to go is to use graphical
calculation views. For scripted parts table functions have to be used which can be consumed in a
graphical calc. view. SAP provides migration tools to migrate scripted calc. views to graphical ones with
table functions.
Since SPS09 it is not recommended anymore to use CE functions. Check Calculation Engine (CE)
Functions - Rest in Peace | SCN and New SQLScript Features in SAP HANA 1.0 SPS9.
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 11/12
11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs
For point 3, step 1 it seems that the obsolete ".procedure" form is used. Please use only the newer
".hdbprocedure".
Regards,
Florian
Like 1 | Share
Hi FLorian,
Thanks for your comments and added information. I am sharing the basic steps and concept.
Best Regards
Anik
Like 0 | Share
Former Member
July 9, 2017 at 4:36 pm
Like 0 | Share
Find us on
Newsletter Support
https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 12/12