Sapabap Hana
Sapabap Hana
Sapabap Hana
---------
Functions and Procedures:
=====================
A function is defined as the DB objects that accepts the parameters and performs an
action.
A Procedure is a group of statements that you can call by name that is it is a unit
or module that performs the material task. The body of the procedure consists of
sequence of statements separated by the semi colon.
There are tthree ways to create a procedure in SAP HANA:
1. Using SAP HANA XS Project in SAP HANA Development perspective.
TABLETYPE:
It is a database table. We can't insert or update or delete in the tabletype. At
run times, only structure is used. In tabletypes, we define parameters for the
procedure that refers the table.
Example:
P_ID = 5
P_NAME = FAN
Stock = 140
QTY = 10
End;
Step 3: Click on Execute
Now include this function in procedure as given below:
SAP HANA Table Functions replaces Scripted Calculation Views as well as it has the
scripted calculation views. It is recommended to use graphical calculation views as
an end product so that the complexity of the information views are
headed inside the table functions. SAP recommends Scripted Based Calculation Views
table functions and then into graphical calculation views. The migration rule
within SAP HANA Modeler converts existing scripted based calculation
views to table functions.
Table Functions has read only user defined functions. It accepts muiltiple input
parameters and returns exactly only one SQL statement. SQL Script is the only
language supported for Table Functions. There are read only statements like
SELECT. Table functions are the read only statements. So you may not use the
statements like INSERT, UPDATE. Table Functions can be executed in the form of
SELECT statement. You can pass the input parameters and you can also
call the function from SQL console. Table function can be used as data foundation
on the graphical calculation views. To build the table function, we will use the
CLIENT and SALES_DOC from the table VBAK. CLIENT technical field name is
MANDT. SALES_DOC field name is VBELN.
Scenario is to build a table function that provides client and sales document from
the table VBAK. For this below process needs to be followed:
(i.) Create package in systems tab
Step 1 :Log into the SAP HANA studio with your credentials.
Step 2 : Enter the password for your server
Step 3: To create Table function, we need to move to the development perspective.
Select SAP HANA Development from open Perspective
Step 4: Go to the content folder. First we need to create a package. Right click on
Content->New->Package
Step 5: Enter the name and description SAPHANA_TF
Step 6: Right click on this package and create sub package by selecting New-
>Package SAPHANA_TF_SD
Now we will create table function
To create a table function we need to switch to the repository.
Step 7: Switch to the repository
Step 8: Right click on SAPHANA_TF package->New-> Table Function->Next
Step 9: Enter file name TABLE_FUNCT
Step 10: Click Finish. Table function has the extension .hdbtablefunction.
FUNCTION "SAPHANA_TF::TABLE_FUNCTION
(
)
RETURNS RETURN_TABLE_TYPE
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
END;
Above code is generated by the system when clicking on the Table Functions.
FUNCTION is a keyword which marks the SAP HANA Function. Name of the function is a
combination of schema and package.
Any input parameters can be added in the brackets which is used in the table
function. return_table_type is a place holder for the actual one. It is similar to
the column function restricted view. Here we specify the output field.
There are two SQL SECURITY modes: INVOKER and other one is DEFINER. By default we
use INVOKER. This function will invoke a procedure. If you use DEFINER, then the
function executes with the priviledges of the DEFINER.
Its always best to execute it as an INVOKER. Whenever this function is called, it
runs on the basis of user roles and priviledges. Between the Begin and End, we need
to write the logic.
FUNCTION "SAPHANA_TF::TABLE_FUNCTION
(
)
RETURNS TABLE("CLIENT" NVHARCHAR(3), "SALES_DOC" NVARCHAR(10))
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
RETURN
SELECT "MANDT" AS "CLIENT",
"VBELN" AS "SALES_DOC"
FROM "BEST"."VBAK";
END;
Once you create table function, we will now create calculation view that will use
the table function
Step 1: Right click on subpackage SD
Step 2: Select New->Calculation View
Enter the name and description as CV_TF
Step 3: Select Calculation View type as Graphical and Data Category as Dimension
Step 4: Click Finish
Step 5: Click on + symbol in projection
Step 6: Search for the object name TABLE_FUNCTION
Step 7: Select your TABLE_FUNCTION and then press OK
Step 8: Right click on it and select Add all objects
Step 9: Select the fields and then press save and Activate
Step 10: Click on raw data to see the output
FUNCTION "SAPHANA_TF::TABLE_FUNCTION
(
)
RETURNS TABLE("CLIENT" NVHARCHAR(3), "SALES_DOC" NVARCHAR(10))
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
RETURN
SELECT "MANDT" AS "CLIENT",
"VBELN" AS "SALES_DOC"
FROM "BEST"."VBAK";
END;
Here we will use the input parameter which will check the sales document of the
user.
Requirement is to display sales document
FUNCTION "SAPHANA_TF::TABLE_FUNCTION
(
p_sales_doc NVARCHAR(10)
)
RETURNS TABLE("CLIENT" NVHARCHAR(3), "SALES_DOC" NVARCHAR(10))
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
RETURN
SELECT "MANDT" AS "CLIENT",
"VBELN" AS "SALES_DOC"
FROM "BEST"."VBAK"
WEHRE "VBELN" = :p_sales_doc;
END;
Here where statment tell HANA that we need the output where the input parameter
values matches with the sales document
Step 2: Save and activate
Step 3: Right click on your subpackage SD and select New->Calculation View
Step 4: Provide the name and description as CV_IP
Step 5: Select Calculation View type as Graphical and Data Category as Dimension
Step 6: Click Finish
Step 7: Select + symbol on projection node
Step 8: Search for table function TABLE_FUNCTION
Step 9: Select your TABLE_FUNCTION and then click on OK
Step 10: Right click on the TABLE_FUNCTION and select Add All to Output
We need to create the input parameter for this calculation view with the same
datatype
Step 11: Right click on input parameter folder and select New
Step 12: Enter the name as p_sales_doc and copy the same into the label
Step 13: Select parameter type as Direct
Step 14: Don't select any semantic type and select Datatype as NVARCHAR and length
as 10.
Step 15: Click OK
Input parameter must have the same name and datatype as passed in the table
function
Step 16: Right click on input parameter and select Manage mappings
Step 17: Select Automatic mapping
Step 18: Click OK
Step 19: Click Save and Activate and execute it
Step 20: Click on Raw data to see the data preview
Step 21: Enter the sales document number in the variable/input parameter values
Step 22: Click OK
Step 23: Click on raw data to validate
===================================================================================
==========================================================================
Day 17:
To change the datatypes, click on the Script view and then choose the edit
datatypes for the columns you want to change the data type and then click ok->Click
validate->Click save and activate
Click on Data preview
CE_COLUMN_VIEW("COLUMN_TABLE",[A,B,C])
Column Table - CE_COLUMN_TABLE
Attribute View - CE_JOIN_VIEW
Analytic View - CE_OLAP_VIEW
Calculation View - CE_CALC_VIEW
WHERE HAVING - CE_PROJECTION
GROUP BY - CE_AGGREGATION
INNER JOIN - CE_JOIN
LEFT_OUTER_JOIN - CE_LEFT_OUTER_JOIN
UNION - CE_UNION_ALL