ABAP on HANA Workshop �
SAP Active Global Support
INTERNAL
1
RKT
ABAP Database Connectivity - ADBC
2
RKT
�Learning Objectives: ADBC
After completing this unit you will be able to:
3
RKT
Introduction of the ADBC
4
RKT
Why do we need ADBC in 7.40?
Note – More details in DEMO.
5
RKT
Difference to Open SQL and ADBC ( Native SQL )
Advantage of Open SQL:
Advantage of ADBC (Native SQL) :
6
RKT
Classes in ADBC
The following four ADBC classes are the most important classes.
7
RKT
CL_SQL_STATEMENT
*Refer to http://help.sap.com/abapdocu_702/de/abencl_sql_connection.htm
8
RKT
CL_SQL_STATEMENT
9
RKT
CL_SQL_STATEMENT
10
RKT
Example Code�DDL & DML & Query Commands
DATA: lo_sql TYPE REF TO cl_sql_statement .
CREATE OBJECT lo_sql .
* Execute DDL statement
lo_sql->execute_ddl(
`CREATE TABLE dbtable` &&
`(val1 char(10) NOT NULL,` &&
` val2 char(10) NOT NULL,` &&
` PRIMARY KEY (val1) )`).
* Execute DML statement
lo_sql->execute_update(
`INSERT INTO dbtable VALUES('10', '10')`).
* Execute Query ( Describe in detail in following slides )
lo_result = lo_sql->execute_query(
'SELECT val1, val2 FROM dbtable' ).
11
RKT
CL_SQL_STATEMENT
12
RKT
Example Code�SET_TABLE_NAME_FOR_TRACE
DATA: lo_sql TYPE REF TO cl_sql_statement,
lo_result TYPE REF TO cl_sql_result_set,� dref TYPE REF TO data,� val1 TYPE snwd_bpa-bp_id, val2 TYPE snwd_bpa-company_name,� val3 type snwd_so_i-gross_amount, tname TYPE tabname.�CREATE OBJECT lo_sql .�tname = 'AN_OPEN_ITEMS'.
lo_sql->set_table_name_for_trace( tname ).�lo_result = lo_sql->execute_query(
'SELECT bp_id, company_name,
SUM( gross_amount_converted ) AS sum_amount
FROM "_SYS_BIC"."test.oia.300.316/AN_OPEN_ITEMS"
GROUP BY bp_id, company_name ').�GET REFERENCE OF val1 INTO dref. lo_result->set_param( dref ).�GET REFERENCE OF val2 INTO dref. lo_result->set_param( dref ).�GET REFERENCE OF val3 INTO dref. lo_result->set_param( dref ).�lo_result->next( ). lo_result->close( ).�WRITE: / val1, val2, val3.
13
RKT
Example Code�SET_TABLE_NAME_FOR_TRACE
14
RKT
Example Code�SET_TABLE_NAME_FOR_TRACE
15
RKT
CL_SQL_RESULT_SET
Like : ' SELECT SINGLE val1 val2 FROM dbtable INTO (wa1, wa2) ‘
Like : ' SELECT SINGLE val1 val2 FROM dbtable INTO ls_result '
16
RKT
CL_SQL_RESULT_SET
Like : ' SELECT val1 val2 FROM dbtable INTO TABLE lt_result '
17
RKT
CL_SQL_RESULT_SET
SET_PARAM, NEXT, and CLOSE :
lo_result = sql->execute_query('SELECT val1, val2 FROM dbtable').
GET REFERENCE OF wa1 INTO dref. lo_result->set_param( dref ).
GET REFERENCE OF wa2 INTO dref. lo_result->set_param( dref ).
lo_result->next( ).
wa1
wa2
val 1 | val 2 |
XX | XX |
XX | XX |
…… | …… |
val 0 | val 1 | val 2 | val 3 | |
XX | XX | XX | XX | …… |
XX | XX | XX | XX | …… |
…… | …… | …… | …… | …… |
SELECT val1, val2
FROM dbtable
Database Table
Result
Set
18
RKT
CL_SQL_RESULT_SET
SET_PARAM, NEXT, and CLOSE :
IF rc > 0.� ... “Handling the result� ELSE.� WRITE 'No entry found.'.� ENDIF.
19
RKT
CL_SQL_RESULT_SET
SET_PARAM_STRUCT, NEXT, and CLOSE
lo_result = sql->execute_query('SELECT val1, val2 FROM dbtable').
GET REFERENCE OF ls_result INTO dref.
lo_result->set_param_struct( dref ).
lo_result->next( ).
wa1
wa2
ls_result
val 1 | val 2 |
XX | XX |
XX | XX |
…… | …… |
val 0 | val 1 | val 2 | val 3 | |
XX | XX | XX | XX | …… |
XX | XX | XX | XX | …… |
…… | …… | …… | …… | …… |
SELECT val1, val2
FROM dbtable
Database Table
Result
Set
20
RKT
CL_SQL_RESULT_SET
SET_PARAM_STRUCT, NEXT, and CLOSE
IF rc > 0.� ... “Handling the result� ELSE.� WRITE 'No entry found.'.� ENDIF.
21
RKT
CL_SQL_RESULT_SET
SET_PARAM_TABLE, NEXT_PACKAGE, and CLOSE
lo_result = sql->execute_query('SELECT val1, val2 FROM dbtable').
GET REFERENCE OF lt_result INTO dref.
lo_result->set_param_table( dref ).
lo_result->next_package( ).
val 1 | val 2 |
XX | XX |
XX | XX |
…… | …… |
val 0 | val 1 | val 2 | val 3 | |
XX | XX | XX | XX | …… |
XX | XX | XX | XX | …… |
…… | …… | …… | …… | …… |
SELECT val1, val2
FROM dbtable
lt_result | |
…… | …… |
XX | XX |
XX | XX |
|
|
Database Table
Result
Set
APPEND
22
RKT
CL_SQL_RESULT_SET
SET_PARAM_TABLE, NEXT_PACKAGE, and CLOSE
rc = lo_result->next_package( ).
lo_result->close( ).
23
RKT
CL_SQL_RESULT_SET
24
RKT
DAY 2�Summary: ADBC
You should now be able to:
25
RKT
Demo – Introduction of the ADBC
DEMO:
26
RKT
HANA Specific solutions : Code Pushdown
27
RKT
DAY 2 �Learning Objectives: HANA Specific solutions – Code pushdown
After completing this unit you will be able to:
28
RKT
HANA Specific Solutions �
29
RKT
Modeling Views in a Nutshell�Modeling with SAP HANA Studio
30
RKT
Modeling Views�
Analytic View
Attribute View
Column Tables
Calculation View
:can use
Legend:
Calculation Engine
Join Engine
OLAP Engine
31
RKT
Decide on where to Build Content?
Analyze Query
Logic
Information in existing tables
Only Joins & calculated expressions
no
Access Base Tables
yes
Use Attribute Views
Star Schema or Aggregation
no
yes
Use Analytic Views
yes
Calculation View
no
Use Graphical or Scripted Calc Views
yes
Use DB Procedures with SQLScript
no
32
RKT
Attribute View�
33
RKT
Modeling Views �Attribute View
34
RKT
Creating Attribute Views
35
RKT
Creating Attribute Views �Select tables and attributes
36
RKT
Creating Attribute Views �Key Attribute
37
RKT
Creating Attribute Views �Join tables
38
RKT
Creating Attribute Views �Data Preview
39
RKT
Calculated Columns�
40
RKT
Calculated Columns
41
RKT
Calculated Columns�Risks
42
RKT
Calculated Columns�Risks
Instead of a Calculated Column, it might be better (depending on the scenario) to add a new fix table column and do the calculation during the time of inserting a new data set into the table, e.g. via SLT.
Or do the calculation into a fix column via procedures scheduled periodically.
43
RKT
External Views
44
RKT
External Views
45
RKT
External Views
HANA Data Type | Description | ABAP Dictionary Type |
SMALLINT | 2-Byte integer | INT2 |
INTEGER | 4-Byte integer | INT4 |
DECIMAL | Packed number | DEC |
SMALLDECIMAL | Packed number | DEC |
FLOAT | Binary floating point number | FLTP |
VARCHAR | Character string | CHAR |
46
RKT
External Views
HANA Data Type | Description | ABAP Dictionary Type |
NVARCHAR | Unicode character string | CHAR |
VARBINARY | Byte string | RAW |
BLOB | Byte string | RAWSTRING |
CLOB | Character string | STRING |
NCLOB | Unicode character string | STRING |
47
RKT
External Views
48
RKT
External Views
*NOTE: Maximum length for names of external views is limited to 16 characters.
49
RKT
Analytic View�
50
RKT
Modeling Views �Analytic View
51
RKT
Analytic View �OLAP Star Schema - Example
Fact table
N
1
⟖
Attribute View
Delivery Header |
Delivery ID |
Delivery Date |
Ship-to Party |
Sold-to Party |
Sales Org |
Material |
Material ID |
Material Class |
Data Foundation
Delivery Item |
Delivery ID |
Item ID |
Material |
Measure |
52
RKT
Creating Analytic Views
53
RKT
Creating Analytic Views
54
RKT
Creating Analytic Views�Select output, define Joins
55
RKT
Creating Analytic Views�Define attributes and measures
attribute
measure
aggregation type
56
RKT
Creating Analytic Views�Data Preview
Select * from “_SYS_BIC”.“<package_name>/<view_name>”
57
RKT
Defining Join Types
matches in the right (left) table.
left
right
left
right
58
RKT
Defining Join Types
is given (e.g. ERP tables) and matching data exists in the joined tables.
left
right
left
right
59
RKT
Results depending on Join Types�Example
SALE_ID | COST | ITEM_ID |
sale 1 | 20000 | hnd |
sale 2 | 25000 | tyo |
sale 3 | 30000 | bmw |
sale 4 | 35000 | mrd |
sale 5 | 20000 | bmw |
ITEM_ID | ITEM_DESCR |
hnd | Hyundai |
tyo | Toyota |
frd | Ford |
mrd | Mercedes |
Fact table – Data Foundation
Dimension table – Attribute View
JOIN on ITEM_ID
SALE_ID | COST | ITEM_ID | ITEM_DESCR |
sale 1 | 20000 | hnd | Hyundai |
sale 2 | 25000 | tyo | Toyota |
sale 3 | 30000 | bmw | ? |
sale 4 | 35000 | mrd | Mercedes |
sale 5 | 20000 | bmw | ? |
SALE_ID | COST | ITEM_ID | ITEM_DESCR |
sale 1 | 20000 | hnd | Hyundai |
sale 2 | 25000 | tyo | Toyota |
sale 4 | 35000 | mrd | Mercedes |
SALE_ID | COST |
sale 1 | 20000 |
sale 2 | 25000 |
sale 3 | 30000 |
sale 4 | 35000 |
sale 5 | 20000 |
SALE_ID | COST | ITEM_ID | ITEM_DESCR |
sale 1 | 20000 | hnd | Hyundai |
sale 2 | 25000 | tyo | Toyota |
sale 4 | 35000 | mrd | Mercedes |
BUT
60
RKT
Calculation View�
61
RKT
Modeling Views�Calculation View
62
RKT
Types of Calculation Views
Graphical Calculation View
SQL Script based Calculation View
(here with CE functions)
Union
Projection
Analytic View
Union
Projection
Analytic View
Column View structure
63
RKT
Graphical Calculation View�
64
RKT
Graphical Calculation View�
Graphical Calculation View
Analytic View
Attribute View or Column Store table
Calculation View
65
RKT
Create Graphical Calculation View
66
RKT
Create Graphical Calculation View�Join, Union, Projection or Aggregation nodes
67
RKT
Create Graphical Calculation View�Join, Union, Projection or Aggregation nodes
68
RKT
Create Graphical Calculation View �Specify Attributes and Measures, Activate
69
RKT
Scripted Calculation View �
70
RKT
Scripted Calculation View �
71
RKT
Scripted Calculation View �Example, SQL Script
Column View
72
RKT
Scripted Calculation View�Example, CE functions
Union of both Analytic Views
Projection = Filtering or adding calculated columns
Analytic View 2
Analytic View 1
73
RKT
Create Scripted Calculation View
74
RKT
Create Scripted Calculation View
Measures for the Output node.
75
RKT
�CE Functions
and maintenance efforts for
CE Functions, limited list
of available functions.
CE functions
SQL
76
RKT
CE Functions
Supported aggregation functions: count, sum, min, max, average = sum/count
no counterpart in SQL
77
RKT
SQL vs. CE Functions�Examples 1
| SQL | CE-Build In Function |
SELECT on Column table | SELECT A, B, C from "COLUMN_TABLE" | CE_COLUMN_TABLE("COLUMN_TABLE", [A, B, C]) |
SELECT on Attribute view | SELECT A, B, C �from "ATTRIBUTE_VIEW" | CE_JOIN_VIEW("ATTRIBUTE_VIEW", [A, B, C]) |
SELECT on Analytical view | SELECT A, B, C, SUM(D) from "ANALYTIC_VIEW" GROUP BY A, B, C | CE_OLAP_VIEW("ANALYTIC_VIEW", [A, B, C, D]); |
SELECT on Calculation View | SELECT A, B, C, SUM(D) from “CALC_VIEW" GROUP BY A, B, C | CE_CALC_VIEW("ANALYTIC_VIEW", [A, B, C, D]); |
78
RKT
SQL vs. CE Functions�Examples 2
| SQL | CE-Build In Function |
WHERE clause | SELECT A, B, C, SUM(D) from "ANALYTIC_VIEW" WHERE B = 'value' AND C = 'value' | var_tab = CE_COLUMN_TABLE("COLUMN_TABLE"); var_proj = CE_PROJECTION(:var_tab, [A, B, C], ' "B" = ''value'' AND "C" = ''value'' '); |
GROUP BY | SELECT A, B, C, SUM(D) FROM "COLUMN_TABLE" GROUP BY � A, B, C | var_tab = CE_COLUMN_TABLE("COLUMN_TABLE"); var_agg = CE_AGGREGATION(:var_tab, [SUM(D)], [A, B, C]); |
INNER JOIN | SELECT A, B, Y, SUM(D) from "COLTAB1" INNER JOIN "COLTAB2" ON "COLTAB1"."KEY1" = "COLTAB2"."KEY1" AND "COLTAB1"."KEY2" = "COLTAB2"."KEY2" | CE_JOIN("COLTAB1","COLTAB2", [KEY1, KEY2], [A, B, Y, D]) |
LEFT OUTER JOIN | SELECT A, B, Y, SUM(D) from "COLTAB1" LEFT OUTER JOIN "COLTAB2" ON "COLTAB1"."KEY1" = "COLTAB2"."KEY1" AND "COLTAB1"."KEY2" = "COLTAB2"."KEY2" | CE_LEFT_OUTER_JOIN("COLTAB1","COLTAB2", [KEY1, KEY2], [A, B, Y, D]) |
79
RKT
SQL vs. CE Functions�Examples 3
| SQL | CE-Build In Function |
UNION ALL | var_tab1 = SELECT A, B, C, D FROM "COLUMN_TABLE1"; var_tab2 = SELECT A, B, C, D FROM "COLUMN_TABLE2"; SELECT * FROM :var_tab1 UNION ALL SELECT * FROM :var_tab2; | var_tab1 = CE_COLUMN_TABLE("COLUMN_TABLE1",[A,B,C,D]); var_tab2 = CE_COLUMN_TABLE("COLUMN_TABLE2",[A,B,C,D]); var_out = CE_UNION_ALL(:var_tab1,:var_tab2); |
UNION | var_tab1 = SELECT A, B, C, D FROM "COLUMN_TABLE1"; var_tab2 = SELECT A, B, C, D FROM "COLUMN_TABLE2"; SELECT * FROM :var_tab1 UNION SELECT * FROM :var_tab2; | n/a use CE_UNION_ALL |
SQL Expressions | SELECT A, B, C, SUBSTRING(D,2,5) FROM "COLUMN_TABLE" | var_tab = CE_COLUMN_TABLE("COLUMN_TABLE"); var_proj = CE_PROJECTION( :var_tab, ["A", "B", "C", CE_CALC('midstr("D",2,5)', string) ]); |
Right OUTER JOIN | SELECT A, B, Y, SUM(D) from "COLTAB1" LEFT OUTER JOIN "COLTAB2" ON "COLTAB1"."KEY1" = "COLTAB2"."KEY1" AND "COLTAB1"."KEY2" = "COLTAB2"."KEY2" | CE_RIGHT_OUTER_JOIN("COLTAB1","COLTAB2", [KEY1, KEY2], [A, B, Y, D]) |
80
RKT
HANA Views:
Which to use when�
81
RKT
Modeling Views�
Analytic View
Attribute View
Column Tables
Calculation View
:can use
Legend:
Calculation Engine
Join Engine
OLAP Engine
82
RKT
SQL Script for Procedures-
Introduction
83
RKT
SQL Script�Introduction
84
RKT
SQL Script vs SQL Queries�Introduction
SQL Script | SQL Queries |
SQL Script can be used to implement complex business logic. It therefore allows complex business logic to be pushed to the database level. | SQL Queries do not have features to implement business logic. As a consequence a business logic cannot be pushed down into the database using SQL queries. |
| |
SQL script can return multiple result sets. Therefore the computation of multiple result sets can be done in one access to the Database. | SQL query can only return one result at a time. For the computation of multiple result sets multiple Database accesses are necessary. |
Note: For more details on how to write SQL script, refer to Appendix
HANA database provides Database Procedures to push down business logic to database. The logics are programmed in SQL Script language.
85
RKT
HANA Database Procedures
86
RKT
Procedure�Introduction
87
RKT
Procedure�Creation
88
RKT
Procedure�Procedures with more than on result set
*More details during DEMO
89
RKT
Which HANA Artifact to use�
90
RKT
Decide on where to Build Content?
Analyze Query
Logic
Information in existing tables
Only Joins & calculated expressions
no
Access Base Tables
yes
Use Attribute Views
Star Schema or Aggregation
no
yes
Use Analytic Views
yes
Calculation View
no
Use Graphical or Scripted Calc Views
yes
Use DB Procedures with SQLScript
no
91
RKT
Pros and Cons of different HANA Artifacts�
92
RKT
Code Pushdown – Pros and Cons of different HANA Artifacts
| 1: Attribute View | 2: Analytic View | 3: Calculation View (Graphical) | 4: Scripted Calculation View (SQL script/CE Functions) | 5: Database Procedures |
Usage | Used for simple applications involving joins and calculated expressions | Used for analytical purposes where read operations on mass data and aggregations are required. | Used for implementing business logics that can’t be modeled using only Attribute or Analytic Views. | Used for implementing business logics that can’t be modeled using Attribute or Analytic Views or graphical CalcView. | Used for implementing complex business logics that can’t be modeled using Attribute or Analytic Views or CalcView (Graphical and scripted) . |
Pros | Easy to model as compared to other artifacts. Good performance when dealing with joins. | Very good performance when dealing with huge amount of data and aggregations. | User-friendly 🡪 No SQL, SQL Script or CE functions knowledge required. Supports UNION. | Gives high flexibility as compared to other views to implement complex business logic. Supports UNION | Gives highest flexibility for implementing very complex logics. Multiple result-sets can move from HANA to AS in one call to the database. |
Cons | No support for complex calculation and logic e.g. currency conversion etc. | Limitations in regards to functionalities e.g. No UNION supported. | Limitations in regards to graphical functions. Only UNION, AGGREGATION, JOIN and PROJECTION supported | High efforts for development because have to write/code own logic. High in complexity if CE functions are used. | Performance depends on how the logic is written (if CE functions are used, if the queries are dependent on each other , if cursor is used for single line access). Performance is not always optimal as compared to other views. |
93
RKT
Database Procedure Proxy
94
RKT
Database Procedure Proxy
95
RKT
Database Procedure Proxy
ABAP Interface that contains type Definition
Importing Parameter
Exporting Parameter
Table type
Scalar type
96
RKT
Database Procedure Proxy
97
RKT
Database Procedure Proxy
98
RKT
Database Procedure Proxy
Note – With DDIC type override only data elements and flat structures from the ABAP Dictionary can be assigned to the proxy parameters as data types. Use of deep structure types is not supported
99
RKT
Database Procedure Proxy
100
RKT
Database Procedure Proxy
101
RKT
Database Procedure Proxy
102
RKT
Database Procedure Proxy
103
RKT
Database Procedure Proxy
104
RKT
DAY 2�Summary: HANA specific solution – Code pushdown
You should now be able to:
105
RKT
Demo + Exercises – HANA Specific solutions – Code pushdown
Lessons | Topics |
Demos + Exercise | Modeling, ABAP: Creating a Attribute View to calculate the DAYS OPEN (difference between todays’ date and date on which SO was created) for each SO. Accessing the attribute view with OPEN SQL. |
| |
Demos | Performance: Open days in ABAP v/s Open days in HANA |
| |
Demos + Exercise | Modeling , ABAP: Creating Analytical view to calculate gross amount per business partner in USD. Accessing the analytical view through ADBC. |
| |
Demos | Performance: Currency conversion in ABAP v/s Currency conversion in HANA |
| |
Demos | Modeling: Creating Calculation views to mark specific Business Partners as SPECIAL based on predefined conditions (Scripted and Graphical) |
| |
106
RKT
Demo + Exercises – HANA Specific solutions – Code pushdown
Lessons | Topics |
Demos + Exercise | Code Push down: Writing DB procedure to find the Business partners with highest 5 and bottom 5 Gross Revenue |
| |
Demos+ Exercise | Code Push down, ABAP: Creating DB procedure Proxy and calling DB procedures from ABAP code. |
| |
107
RKT
Relation between the tables used in Exercises and Demos
108
RKT
Appendix
ABAP Database Connectivity - ADBC
109
RKT
CL_SQL_CONNECTION
DATA lo_con_ref TYPE REF TO cl_sql_connection.
Connected to ABC:
lo_con_ref = cl_sql_connection=>get_connection( ‘ABC’ ).
.
110
RKT
CL_SQL_CONNECTION
Connected to Central DB:
CREATE OBJECT lo_con_ref.
111
RKT
CL_SQL_CONNECTION
CREATE OBJECT lo_sql EXPORTING con_ref = lo_con_ref.
112
RKT
Example Code�A Simple Statement to Secondary Database Connection
DATA: lo_sql TYPE REF TO cl_sql_statement,
lo_result TYPE REF TO cl_sql_result_set,
lr_flight TYPE REF TO DATA,
lt_flight TYPE TABLE OF sflight.
CREATE OBJECT lo_sql
EXPORTING con_ref = cl_sql_connection=>get_connection( 'HDB' ).
lo_result = lo_sql->execute_query(
'SELECT * FROM SFLIGHT WHERE ...' ).
GET REFERENCE OF lt_flight INTO lr_flight.
lo_result->set_param_table( lr_flight ).
lo_result->next_package( ).
lo_result->close( ).
113
RKT
CX_SQL_EXCEPTION
114
RKT
CX_SQL_EXCEPTION
TRY .
lo_result = lo_sql->execute_query('...').
...
CATCH cx_sql_exception
INTO lo_err.
MESSAGE lo_err
TYPE 'I'
DISPLAY LIKE 'E'.
ENDTRY.
Attribute | Meaning |
DB_ERROR | "X", if an SQL statement was not executed by DBMS. |
DBOBJECT_EXISTS | "X", if you want to create a database object that already exists. |
DBOBJECT_NOT_EXISTS | "X", if you want to access a database object that does not exist. |
DUPLICATE_KEY | "X", if a DML statement would violate a unique table key. |
INTERNAL_ERROR | Internal error code from DBMS. |
INVALID_CURSOR | "X", if you want to use an invalid or closed database cursor. |
SQL_CODE | Database-specific error code, if DB_ERROR is "X". |
SQL_MESSAGE | Database-specific error code, if DB_ERROR is "X". |
115
RKT
Appendix
SQL Scripts in HANA
116
RKT
SQL Script�Data Types
Scalar Data types:
117
RKT
SQL Script�Data Types
Table Type Definition
Syntax:
CREATE TYPE {schema.}name AS TABLE (attribute1 type1 , attribute2 type2,...)
118
RKT
SQL Script�Table Types
tt_publishers | |
publisher | INTEGER |
name | VARCHAR(50) |
price | DECIMAL |
cnt | INTEGER |
119
RKT
SQL Script�Table Types
120
RKT
SQL Script�Writing simple procedure
CREATE PROCEDURE testproc( IN cnt INTEGER, OUT result tt_result)
LANGUAGE SQLSCRIPT READS SQL DATA AS
BEGIN
RESULT = SELECT …… FROM dbtab WHERE count = :cnt;
End;
121
RKT
SQL Script�Writing simple procedure
CREATE PROCEDURE testproc( IN cnt INTEGER, OUT result tt_result)
LANGUAGE SQLSCRIPT
READS SQL DATA AS
BEGIN
RESULT = SELECT …… FROM dbtab WHERE count = :cnt;
END;
122
RKT
SQL Script�Writing simple procedure
CREATE PROCEDURE testproc( IN cnt INTEGER, OUT result tt_result)
LANGUAGE SQLSCRIPT
READS SQL DATA AS
BEGIN
RESULT = SELECT …… FROM dbtab WHERE count = :cnt;
END;
123
RKT
SQL Script�Writing Simple procedures
DROP PROCEDURE {schema.}name {CASCADE}
124
RKT
SQL Script�Writing Simple procedures
CALL {schema.}name (param1 {, ...})
E.g. call testproc (10,lt_result);
125
RKT
SQL Script�Example of a Procedure
CREATE PROCEDURE getOutput( IN cnt INTEGER, IN currency VARCHAR(3), OUT output_pubs tt_publishers, OUT output_year tt_years) LANGUAGE SQLSCRIPT READS SQL DATA AS
BEGIN
big_pub_ids = SELECT publisher AS pid FROM books -- Query Q1 GROUP BY publisher HAVING COUNT(isbn) > :cnt;
big_pub_books = SELECT title, name, publisher, -- Query Q2 year, price FROM :big_pub_ids, publishers, books WHERE pub_id = pid AND pub_id = publisher AND crcy = :currency;
output_pubs = SELECT publisher, name, -- Query Q3 SUM(price) AS price, COUNT(title) AS cnt FROM :big_pub_books GROUP BY publisher, name;
output_year = SELECT year, SUM(price) AS price, -- Query Q4 COUNT(title) AS cnt FROM :big_pub_books GROUP BY year;
END;
126
RKT
SQL Script�Writing simple procedures
lv_bp_id varchar(10):= ‘0100000001';
SELECT bp_id, company_name, phone_number
INTO lv_bp_id, lv_company_name, lv_phone_number
FROM SAPNVM.SNWD_BPA WHERE bp_id = :lv_bp_id;
‘:’ before variable means getting value of the variable.
‘:=’ is the assignment operator which assign the value on the left side to the variable on the right side.
127
RKT
SQL Script�Control Structures (IF structure)
128
RKT
SQL Script�Control Structures (WHILE and FOR)
WHILE <bool-stmt> DO
{statements}
END WHILE;
FOR <loop-var> IN {REVERSE} <start> .. <end> DO
{statements}
END FOR;
129
RKT
SQL Script�Interesting Example
Note: Please find a complete reference of SAP HANA database SQL in SAP HANA Database - SQL Reference Manual.
130
RKT