0% found this document useful (0 votes)
72 views

Create Calculation View - SQL Script Table Functions Procedure

The document discusses how to create scripted calculation views in SAP HANA. It provides examples using SQL scripts with CE functions and table functions. It includes code samples to create the necessary tables and views, join the tables, and project the columns including calculated fields.

Uploaded by

amcreddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Create Calculation View - SQL Script Table Functions Procedure

The document discusses how to create scripted calculation views in SAP HANA. It provides examples using SQL scripts with CE functions and table functions. It includes code samples to create the necessary tables and views, join the tables, and project the columns including calculated fields.

Uploaded by

amcreddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

11/4/21, 12:31 PM SAP HANA Scripted Calculation View | SAP Blogs

Community

Ask a Question Write a Blog Post Login

Former Member
September 1, 2016
| 7 minute read

SAP HANA Scripted Calculation


View
 3  4  43,740
Follow

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.

We will Discuss Calculation View using.

1. SQL Script – Using CE Functions

2. Table Functions

3. Procedure

There is a basic creation of graphical calculation view shown here in


saphanatutorial.com. Please use this link to build the tables REGION, SALES and
PRODUCT. Also create records into these tables using the same link. This is a
wonderful site to learn HANA from. Here is an overview of how the graphical
calculation view will look like.

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.

1. SQL Script – Using CE Functions

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

Please find the code below to try it yourself.

/********* Begin Procedure Script ************/


BEGIN

   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

                      “SALES_AMOUNT” ]);

   join2 = CE_JOIN(:join1, :product,

           [“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 ************/

2. SQL Script – Using Table Functions

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

Step 3: Write your code.

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

create function get_margin(im_var1 double)


returns result double
language SQLSCRIPT
SQL SECURITY INVOKER as
BEGIN
result := :im_var1 * 0.3;
end;

/* 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.

3. SQL Script – Procedure

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

Similar Blog Posts 


Debugging – HANA Scripted Calculation view
Finally the output for each calculation views will be the same as we tried to
By
Deric Pavoo Dec 04, 2015
replicate the same output using different methods. This is the Analysis view.

HANA Artifacts Part4.3(Calculation Views)


By
Former Member Nov 13, 2013

Script Based Calculation View in COPA Scenario_Part 4


By
Former Member May 09, 2013

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

Currency Conversion in SQL Scripted view - features disabled ?


By
Former Member Jun 09, 2016

Join the Conversation 


SAP TechEd
Tune in for tech talk. Stay for inspiration. Upskill your future.

Coffee Corner
Join the new Coffee Corner Discussion Group.

3 Comments

You must be Logged on to comment or reply to a post.

Florian Pfeffer
September 2, 2016 at 5:46 am

Hello Anik,

thx for sharing your experiences.

Maybe some hints for your future developments:

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

Former Member | Blog Post Author


September 2, 2016 at 6:04 am

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

Thank you Anik Sircar for the information. It is a in a detailed manner.

Like 0 | Share

Find us on

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Cookie Preferences

Newsletter Support

https://blogs.sap.com/2016/09/01/sap-hana-scripted-calculation-view-part-1/ 12/12

You might also like