Documente Academic
Documente Profesional
Documente Cultură
SQL Script
Development Artefacts on HANA
There are two reasons why this is required to achieve the best performance:
– Eliminates the need to transfer large amounts of data from the database to
the application
– Calculations need to be executed in the database layer to get the maximum
benefit from SAP HANA features such as fast column operations, query
optimization, and parallel execution. If applications fetch data as sets of rows
for processing on application level, they will not benefit from these features.
Benefits of SQL Script?
Compared to plain SQL queries, SQL Script has the following advantages:
– Functions can return multiple result sets, while an SQL query returns only one
result set
– Complex functions can be broken down into smaller functions enabling modular
programming, reuse, and better understand ability. For structuring complex
queries, standard SQL only allows the definition of SQL views. However, SQL views
have no parameters.
– SQL Script supports local variables for intermediate results with implicitly defined
types. With standard SQL, you need to define globally visible views even for
intermediate steps
– SQL Script has control logic such as if/else that is not available in SQL
Performance Gains
Traditional vs HANA Code push down
model
SQL Script – Recap slide
In SQL Script there are two different logic containers, Procedure and User-Defined
• A procedure is a unit/module that perform a specific task. This procedure can be combined
to form larger programs. This basically forms the 'Modular Design'. A procedure can be
invoked by another procedure which is called the calling program.
• Procedures are re-useable processing block with a specific sequence of data
transformation. The procedure can have multi-input/output parameters. The procedure can
be created as read-only or read-write.
• An SQL Procedure can be created at –
1. At Schema Level(Catalog Node)
2. At Package Level(Content Node)
SQL Scripts – Schema
A database schema is a way to logically group objects such as tables, views, stored procedures
etc. Think of a schema as a container of objects.
In HANA, with the help of SQL Script, we can create a Table Type. These are used to get
back table outputs from a SQL Script.
Syntax:
CREATE TYPE [schema.]name AS TABLE
(name1 type1 [, name2 type2,...])
Remember we cannot add record to table type. If you try to insert record, you will get
an error. (See error below in RED)
Expressions-
• Case Expressions – In this expression the user can use IF – THEN – ELSE logic without
write procedure.
• In this example we are going to use 2 tables PRODUCTS and SALES. The procedure will join
these 2 tables to get PRODUCT_NAME and SALES_AMOUNT. The output will be sent in output
using a table type.
• Example : SALES_REPORT_TT
Examples : Use Local Scalar Variable
• Local variables are declared using DECLARE keyword and they can optionally be initialized
with their declaration. By default scalar variables are initialized with NULL.
• Eg. In this example we are going to use 2 tables PRODUCTS and SALES. The procedure will
join these 2 tables to get PRODUCT_NAME and SALES_AMOUNT. The output will be sent in
output using a table type.
Examples : Variable Scope Nesting in
Procedure
• SQLScript supports local variable declaration in a nested block. Local variables are only
visible in the scope of the block in which they are defined. It is also possible to define local
variables inside LOOP / WHILE /FOR / IF-ELSE control structures.
• Eg. From this result you can see that the inner most nested block value of 3 has not been
passed to the val variable.
Examples : IF-ELSE Logic in Procedure
• The IF statement consists of a Boolean expression <bool-expr1>. If this expression evaluates
to true then the statements <then-stmts1> in the mandatory THEN block are executed. The
IF statement ends with END IF. The remaining parts are optional.
• Eg. In this example we will pass product id, product name and category as input. If the
product already exist, we will update the record. If it does not exist, we will create a new
record.
Examples : For Loop in Procedure
• The For loop iterates a range of numeric values.
BREAK:
Specifies that a loop should stop being processed.
CONTINUE:
Specifies that a loop should stop processing the current iteration, and should immediately
start processing the next.
• Eg. In this example we will define a loop sequence. If the loop value :x is less than 3 the
iterations will be skipped. If :x is 5 then the loop will terminate.
Examples : Use of Array in Procedure
• The For loop iterates a range of numeric values.
BREAK:
Specifies that a loop should stop being processed.
CONTINUE:
Specifies that a loop should stop processing the current iteration, and should immediately
start processing the next.
• Eg. In this example we will define a loop sequence. If the loop value :x is less than 3 the
iterations will be skipped. If :x is 5 then the loop will terminate.
Code Sample - Parallelization
BEGIN
-- Query 1
product_ids = select "ProductId", "Category", "DescId"
from
"SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::products"
where "Category" = 'Notebooks'
or "Category" = 'PC';
-- Query 2
product_texts = select "ProductId", "Category", "DescId", "Text"
from :product_ids as prod_ids
inner join
"SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::texts"
as texts on prod_ids."DescId" = texts."TextId";
-- Query 3
out_notebook_count = select count(*) as cnt from
:product_texts where "Category" = 'Notebooks';
-- Query 4
out_pc_count = select count(*) as cnt from
:product_texts where "Category" = 'PC';
AMDP – ABAP managed Database
Procedures
• AMDP is a class based approach for managing and calling HANA procedures from ABAP. This
class is called a AMDP class. All classes inheriting from the standard interface
• A global class is marked with a specific interface to enable the class to contain the HANA
procedure code
• The CTS transport moves just the Class to the subsequent environments and the HANA
Artefact which is the procedure will automatically get created when the class gets activated
• How does the AMDP run in HANA? – Lazy loading, created on runtime. JIT pattern
• How to create a AMDP? - ADT for Eclipse, not editable in ABAP Workbench, compile time