Professional Documents
Culture Documents
DEV161.SQL Script - Excercise PDF
DEV161.SQL Script - Excercise PDF
DEV161
Exercises / Solutions
Rich Heilman, SAP Labs, LLC.
Katharina Schell, SAP SE
Andreas Bader, SAP SE
DEV161
Contents
2
DEV161
Explanation Screenshot
http://lt5069.wdf.sap.corp:801
0/sap/hana/ide/editor/
3
DEV161
4
DEV161
11.Click “Save”.
5
DEV161
http://lt5069.wdf.sap.corp:801
0/workshop/admin/ui/exercise
Master/index.html?workshop=
dev161
6
DEV161
In this exercise we will create a small procedure “get_po_header_data” with two implicit SELECT
queries.
We will …
- Create an empty HDB Procedure “get_po_header_data” (steps 1-5)
- Add two implicit SELECT statements to the procedure (steps 6-8)
- Call the procedure (steps 9-12)
Explanation Screenshot
7
DEV161
3. Enter “HANA_DEV161_XXX”
(where XXX is your group
number) in the search box.
Select your schema which you
created earlier. DO NOT USE
HANA_DEV161_000. Click
“Ok”.
4. Click “Create”.
8
DEV161
END
9
DEV161
In this exercise we will modify the code of procedure “get_po_header_data” so that it takes full
advantage of the parallel processing within HANA by using table variables.
We will …
- Define two tabular output parameter that matches the result structure of the implicit queries
(steps 1-2)
- Assign the queries to the newly created output parameters (steps 3-5)
- Call the procedure (steps 6-8)
Explanation Screenshot
10
DEV161
END
11
DEV161
In this exercise you will modify the code of procedure “get_po_header_data” again to use a single
tabular output. Existing queries will be reused based on intermediate table variables.
We will …
- Remove the existing tabular output parameters (steps 1-2)
- Add the new table output parameter to the signature of the procedure (step 3)
- Rename the existing table variables to “PO_CREATE_CNT” & “PO_CHANGE_CNT” and
enrich the queries by an alias for employee id column (steps 4-5)
- Add a query that joins employee master data with the data from the two existing table
variables and assign the result to the new output parameter
“EX_TOP_3_EMP_PO_COMBINED_CNT” (step 6)
- Call the procedure (steps 9-11)
Explanation Screenshot
12
DEV161
4. Rename
EX_PO_CREATE_CNT to
PO_CREATE_CNT. Also
rename
EX_PO_CHANGE_CNT to
PO_CHANGE_CNT.
13
DEV161
EX_TOP_3_EMP_PO_COMBINED_CNT =
SELECT emp.LOGINNAME, crcnt.CREATE_CNT,
chcnt.CHANGE_CNT, crcnt.CREATE_CNT +
chcnt.CHANGE_CNT AS COMBINED_CNT
FROM
"SAP_HANA_EPM_NEXT"."sap.hana.democontent.epmNext.data::MD
.Employees" as emp
LEFT OUTER JOIN :PO_CREATE_CNT AS crcnt
ON emp.EMPLOYEEID = crcnt.EID
LEFT OUTER JOIN :PO_CHANGE_CNT AS chcnt
ON emp.EMPLOYEEID = chcnt.EID
ORDER BY COMBINED_CNT DESC LIMIT 3;
END
10.Click “Run”.
In this exercise we are creating a scalar UDF for generating a full name from the last, first and middle
name of the employee.
We will …
- Create a new package specifically for functions (steps 1-2)
- Create a scalar function “get_full_name” that uses imperative SQLScript logic to concatenate
the full name out of scalar input parameters (steps 3-6)
- Modify the procedure “get_po_header_data” by replacing the column “LOGINNAME” with our
newly created scalar function using the alias “FULLNAME” (steps 7-10)
- Call the procedure (steps 11-13)
Explanation Screenshot
15
DEV161
6. Click “Save”.
16
DEV161
EX_TOP_3_EMP_PO_COMBINED_CNT =
SELECT
"HANA_DEV161_<group_number>"."dev161.exercises.g<group_num
ber>.functions::get_full_name"( "NAME.FIRST",
"NAME.MIDDLE", "NAME.LAST") as FULLNAME, crcnt.CREATE_CNT,
chcnt.CHANGE_CNT, crcnt.CREATE_CNT + chcnt.CHANGE_CNT AS
COMBINED_CNT
FROM
"SAP_HANA_EPM_NEXT"."sap.hana.democontent.epmNext.data::MD
.Employees" as emp
LEFT OUTER JOIN :PO_CREATE_CNT AS crcnt
ON emp.EMPLOYEEID = crcnt.EID
LEFT OUTER JOIN :PO_CHANGE_CNT AS chcnt
ON emp.EMPLOYEEID = chcnt.EID
ORDER BY COMBINED_CNT DESC LIMIT 3;
END
10.Click “Save”.
17
DEV161
18
DEV161
We will …
- Create a table function “get_po_counts” with a scalar input parameter “im_fdate” and tabular
return type that matches the output parameter structure of procedure “get_po_header_data”
(steps 1-3)
- Copy the logic from the body of procedure “get_po_header_data” into the table function
“get_po_counts” (step 4)
- Use the scalar input parameter “im_fdate” for filtering the column “CHANGEDAT” in the query
assigned to table variables “po_change_cnt”. The month of “CHANGEAT” should match the
month of “im_fdate”. Do similar for the column “CREATEDAT” in the query assigned to table
variables “po_create_cnt”. (step 5)
- Remove the LIMIT from the final query and rename the table variable name to
“EMP_PO_COMBINED_CNT” (steps 6-8)
- Add a query on table variable “EMP_PO_COMBINED_CNT” as return statement (steps 9-11)
- Query the TOP 3 employees by using a SELECT statement with LIMIT clause on the table
function from the catalog view of the WEB IDE (steps 12-16)
Explanation Screenshot
19
DEV161
20
DEV161
EMP_PO_COMBINED_CNT =
SELECT EMPLOYEEID,
"HANA_DEV161_<group_number>"."dev161.exercises.g<group_num
ber>.functions::get_full_name"(
"NAME.FIRST", "NAME.MIDDLE", "NAME.LAST") as
FULLNAME, crcnt.CREATE_CNT,
chcnt.CHANGE_CNT, crcnt.CREATE_CNT +
chcnt.CHANGE_CNT AS COMBINED_CNT
FROM
"SAP_HANA_EPM_NEXT"."sap.hana.democontent.epmNext.data::MD
.Employees"
as emp
LEFT OUTER JOIN :PO_CREATE_CNT AS crcnt
ON emp.EMPLOYEEID = crcnt.EID
LEFT OUTER JOIN :PO_CHANGE_CNT AS chcnt
ON emp.EMPLOYEEID = chcnt.EID
ORDER BY COMBINED_CNT DESC ;
END;
21
DEV161
11.Click “Save”
22
DEV161
We will …
- Set a breakpoint in procedure “get_po_header_data” (steps 1-2)
- Call the procedure “get_po_header_data” and stop during execution in the debugger (step 3-
5)
- Step Over and investigate intermediate variables (steps 6-9)
- Evaluate expressions (steps 10-14)
- Stepping through the procedure and resume to check the result (15-18)
Explanation Screenshot
23
DEV161
24
DEV161
25
DEV161
26
DEV161
27
DEV161
We will …
- Write an empty anonymous block skeleton in the SQL console of WEB IDE’s catalog view
(steps 1-3)
- Copy the logic from the body of procedure “get_po_header_data” into the body of the
anonymous block statement (step 4)
- Make “EX_TOP_3_EMP_PO_COMBINED_CNT” an explicitly declared table variable to
ensure data type conversion as we assume our calling application can only handle string type
columns (step 5)
- Add an implicit SELECT in table variable “EX_TOP_3_EMP_PO_COMBINED_CNT” to
retrieve the desired result by executing the anonymous block (steps 6-9)
Explanation Screenshot
END;
28
DEV161
29
DEV161
SELECT PURCHASEORDERID
FROM
"sap.hana.democontent.epmNext.data::PO.Item"
WHERE "PRODUCT.PRODUCTID" IS NOT NULL)
GROUP BY "HISTORY.CREATEDBY.EMPLOYEEID";
EX_TOP_3_EMP_PO_COMBINED_CNT =
SELECT
"HANA_DEV161_<group_number>"."dev161.exercises.g<group_num
ber>.functions::get_full_name"( "NAME.FIRST",
"NAME.MIDDLE", "NAME.LAST") as FULLNAME, crcnt.CREATE_CNT,
chcnt.CHANGE_CNT, crcnt.CREATE_CNT + chcnt.CHANGE_CNT AS
COMBINED_CNT
FROM
"SAP_HANA_EPM_NEXT"."sap.hana.democontent.epmNext.data::MD
.Employees" as emp
LEFT OUTER JOIN :PO_CREATE_CNT AS crcnt
ON emp.EMPLOYEEID = crcnt.EID
LEFT OUTER JOIN :PO_CHANGE_CNT AS chcnt
ON emp.EMPLOYEEID = chcnt.EID
ORDER BY COMBINED_CNT DESC LIMIT 3;
END;
8. Click “Run”.
30
DEV161
31