Skip to Content
Author's profile photo Misaq Tonse

Table Functions in SAP HANA – Demo

Dear All,


          Post SPS 11 there are changes and restructuring in the way of HANA modeling. In this document I am trying to discuss about Table Functions (User Defined Functions).

The recommendation is to make use of Graphical Calculation views as the end product, so that the complexity of the information views are hidden inside Table Functions.

SAP recommends to Migrate Script-based Calculation Views to Table Functions and then into the Graphical Calculation Views. In the future, SAP recommends using graphical calculation views for modeling any analytic use cases. A migration tool within the SAP HANA modeler allows you to convert existing script-based calculation views available in your system to table functions and graphical calculation views.


First of all let us understand what is Table Function? And how they are different from stored procedures?

Table Functions are read-only user-defined functions which accept multiple input parameters and return exactly one results table. SQLScript is the only language which is supported for table functions. Since these functions are ready-only and side-effect free, only read-only statements like SELECT can be used within the function. So you may not use statements like INSERT, UPDATE or DELETE.

Whereas Stored Procedures groups the SQL statement into a single block. Stored Procedures are used to achieve certain result across applications. The set of SQL statements and the logic that is used to perform some specific task are stored in SQL Stored Procedures. Procedures can make use of DDL languages and can be called using “CALL” statement like below.

IMG1.png

On the other hand Table functions can be executed in the FROM clause of your SELECT statements. You can pass the input parameters as well. You can call the function from the SQL Console as shown:

IMG2.png

Ideally the table functions are used as the Data Foundation for the Graphical Calculation view. (We will see this in the demo example in the upcoming pages)

In this document I would like to show how to address a business scenario with the help of Table Functions. (By this we are replacing Script based calculation view)

Scenario: Creating the information model to get the Customer Master Data (CUST_INFO_PRI) and Fact Order Data (FACT_ORDERS_DATA) by joining the tables on CUSTOMER_NUMBER. If the SHIPPING CITY is INDIA then create the new column as NEW_DISCOUNT = 0.99; for others existing discount is applied.

  • Open the HANA Development Perspective. Create the Project and the Workspace associated to it. (Your package is imported in this Workspace)

Right Click on the Workspace ->New-> Others.

Select the Table Function Wizard.

IMG3.png

Select the Project having the relevant Workspace and the package underneath. Provide the filename.

.hdbtablefunction will be appended upon clicking FINISH.

The Table Function editor will open for you. Add your Logic.

IMG4.png

Save and activate. Upon Activation you will find below object under your package.

IMG_0.png

  • Switch back to the Modeler perspective. Now we will see how to execute and use this Table function as part of Information modeling.

To check for the data consistency open the SQL console.

IMG5.png

Result of Table Function.

IMG6.png

Now we can use this table function in the calculation view, like any other table.

Conclusion: As recommended by SAP, We have used the concept of Table Function inorder to replace the script based calculation view.


Kindly spare some time and provide your feedback inorder to enhance my knowledge.

Thanks for reading!

Thanks for your time!!

Thanks for your support!!!

Assigned Tags

      25 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      THANK YOU SO MUCH. VERY USEFUL.

      Author's profile photo Sudhakar Gurram
      Sudhakar Gurram

      I read somewhere that Table Functions can read R as well. Can you confirm on that

      Author's profile photo Misaq Tonse
      Misaq Tonse
      Blog Post Author

      Hi Sudhakar,

      If you can see the syntax of Table function I have used "LANGUAGE SQLSCRIPT" which tells the HANA system that SAP HANA SQL Script would be the language used to code the logic for this table function. You can try using the analytical language ‘R’ instead, by using "LANGUAGE R".

      Author's profile photo Former Member
      Former Member

      I am trying to use WITH clause in UDF, it's throwing syntax error "Return is incorrect or misplaced" I have tried with

      - 'return' before the 'WITH'.

      - 'return' before the final 'SELECT', still error continues, any help ??

      Author's profile photo Ibrahim Noorali
      Ibrahim Noorali

       

      As per the table function syntax, the phrase should be RETURNS.

      Author's profile photo Misaq Tonse
      Misaq Tonse
      Blog Post Author

      Yes, That's Right Ibrahim!

      Subhasish, Please correct the keyword to RETURNS instead of RETURN

      Author's profile photo Lakshminarasimhan N
      Lakshminarasimhan N

      Hi, Nice document. Is there any place where i can check what SAP is heading to.

      For example - I want creating SQL script based cal. view for many scenarios.Now i realize "Table Functions" soon going to replace the Script based cal. view.

      If i had not come across your document, i never know!! 🙂

      So is there any place i can look for the SAP's upcoming changes(From developer point of view).

      I have checked road maps for HANA, its very much high level(No mention of Table functions there).

      Author's profile photo Benedict Venmani Felix
      Benedict Venmani Felix

      Hi Lakshminarasimhan,

      You can register for SAP JAM. The SAP HANA International Focus Group @ SAP JAM has various webinar sessions whenever a new product or feature is released. It would be a good place for the kind of information you are looking for. Also, the SAP HANA Academy channel in YouTube is a great place to start.

      Author's profile photo Lakshminarasimhan N
      Lakshminarasimhan N

      Thanks Benedict 🙂

      Author's profile photo Mujahid Mohammad
      Mujahid Mohammad

      Hi Benedict,

      how can I register myself with SAP JAM ? It is asking me to request for invitation to join the group.

      A quick response will be greatly appreciated.

       

      Thanks.

      Author's profile photo Ashwin Narayan
      Ashwin Narayan

      Hi Misaq,

       

      Great Blog!! Way to go. Very helpful .

       

      Thanks,

      Ashwin

       

      Author's profile photo Utente Vimar Generico Utente Vimar Generico
      Utente Vimar Generico Utente Vimar Generico

      Hi Misaq,

      Thanks for this great Blog!

      Have you try to use the Table Function with return table containing SECONDDATE data type field? I have a problem with this as described here https://answers.sap.com/questions/220949/hana-modeling-table-function-doesnt-return-secondd.html.

      Any suggestion?

      Thanks,

      Denis P.

       

      Author's profile photo Misaq Tonse
      Misaq Tonse
      Blog Post Author

      Hi Utente,

      SECOND DATE is always interesting and unique!

      Your question has been answered in the thread opened by you 🙂

      Good luck!

      Author's profile photo nilima rodrigues
      nilima rodrigues

      Hello Misaq,

      May be a basic question but I would like to understand why would I use the table-function instead of using the stored procedure concept ? I am trying to understand in which scenarios table function based calculation view makes more sense than using procedures?

       

      Thank You,

      Nilima

       

      Author's profile photo Misaq Tonse
      Misaq Tonse

      Hi Nilima,

      A procedure is a module that is intended for particular task. They are reusable processing blocks can be called with CALL statement in SQL console. You cannot report on top of a procedure.

      To have meaningful report(for the business decision) , we can create a procedure and then we call the procedure in the calculation view (i.e. Table Function)

      For instance a scenario, in which you are creating a Table function and calling a procedure in it.

      Author's profile photo Former Member
      Former Member

      Things that a Table function can do but "SQL Script (stored procedure)" in a scripted calculation view cannot:

      1. Reuse the same script in multiple calculation views
      2. Select directly from table function using select statement (although it's just a change in syntax i.e., use select instead of call)
      Author's profile photo Former Member
      Former Member

      Hi Misaq,

       

      Thanks. This is very much helpful and useful.

      However, some questions and doubts.

      1. By default, this table function will get created in the schema of the user who logged in. If I log in with my ID as MAHENDRA, this function definition will get created/opened as "MAHENDRA.<package path>::<Table Function Name>
      2. Even though we can edit the this schema in the definition, is there a way we can do a dynamic schema mapping.
      3. As we have schema variance while moving into higher environments, is there a way we can auto map the schema.

      We are stuck up with the schema where our table function is getting created. It would be great if you can throw some insight on this.

      Thanks in Advance.

      Mahendra.

      Author's profile photo Misaq Tonse
      Misaq Tonse
      Blog Post Author

      Hi Mahendra,

      Sorry for the delay in my response.

      Table functions acts like FUNCTIONS, you can use them in your calculation views to address the business scenario. They are present in your schema under the folder Function.

      I would say, as a developers you create all your table functions in one schema and use them wherever you require. (make sure your id has the access on this schema)

      If you want to promote any of the deliverables (calculation view or Table function) from DEV to QA, please do a schema mapping from your DEV_SCHEMA to QA_SCHEMA in QA system.

       

       

      Author's profile photo Former Member
      Former Member

      Hi Misaq!

      Can you explain me in more detail about mapping from DEV_SCHEMA and Q_SCHEMA or  PROD_SCHEMA?

      For example - in Table Function I have sql like "select * from DEV_SCHEMA."/BIC/ATABLE" ".

      I have an error on transport in to PROD_SYSTEM , because prod system have another shema .

      Thanks in Advance.

      Alexander

      Author's profile photo Misaq Tonse
      Misaq Tonse

      Hi Alexander,

      Sorry for the late reply, This can be fixed by Schema Mapping, You need to map the references of script-based calculation views / Table Functions and procedures manually by changing the script, and by checking if the tables are qualified with the schema. If the tables are not qualified, the default schema of the view is used, and the schema mapping is also applied to the default schema.

      For more details refer : https://help.sap.com/doc/fc5ace7a367c434190a8047881f92ed8/2.0.00/en-US/4cba3069bc39448581e7f83d52218bc5.html

       

      Author's profile photo Kabilarasan R
      Kabilarasan R

      Hi Experts,

      I created a Table function. I want to know how to move it from Dev System to Quality System..

      I also tried the delivery units. it move only views and other object it wont move Table Function...

       

       

      Author's profile photo Misaq Tonse
      Misaq Tonse
      Blog Post Author

      Hi Kabilarasan R

      There are 2 ways.

      1. Take the Table Function definition and replace the reference to the DEV table with that of QA tables. and ask the DBA to run the script in QA.
      2. Embed the Table Function into a graphical based CV and promote it to QA as part of Delivery Units.
      Author's profile photo Kabilarasan R
      Kabilarasan R

      Hi Misaq T ,

      In a Body of the table function, I tried to use Common Table Expressions(CTE) lith With CTE... But it Throws a syntax Error i.e., Return is misplaced , near that... WITH

       

      Author's profile photo Veera Kommineni
      Veera Kommineni

      Hi Misaq,

       

      logic in your screenshot of table function is not clear . please can you zoom it up.

      Author's profile photo Misaq Tonse
      Misaq Tonse
      Blog Post Author

      HI Veera Kommineni ,

       

      Place below code under RETURN:

       

      select F.ORDER_NUMBER, C.CUST_NAME, C.SHIP_CITY, 
             C.SHIP_COUNTRY, F.ORDERED_DATE,
             F.QTY_ORDERED, F.AMOUNT,
      	   case when C.SHIP_COUNTRY = 'INDIA' then 0.99
      		 else F.DISCOUNT 
      	   end new_discount,
             F.COST_PRICE
      from <SCHEMA>."FACR_ORDERS_DATA" F
      join <SCHEMA>. "CUST_INFO_PRI" C
            on F.CUST_NO = C.CUST.NO