Professional Documents
Culture Documents
Sap New Edition Hana: SQL Script
Sap New Edition Hana: SQL Script
Sap New Edition Hana: SQL Script
EDITION HANA
SQL Script
SQL Script
■ The main goal of SQL Script is to allow the execution of data-intensive calculations
inside SAP HANA database.
■ 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 results, 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
Data Type Extensions
Besides the built-in scalar SQL datatypes, SQL Script allows you to
use user-defined types for tabular values
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,...]) CREATE TYPE SCHEMA_NAME.TT_SALES AS TABLE
(
DROP TYPE [schema.]name [CASCADE] SALES_AMOUNT DECIMAL,
NET_AMOUNT DECIMAL,
PRODUCT_NAME NVARCHAR(20),
REGION_NAME NVARCHAR(20),
SUB_REGION_NAME NVARCHAR(20)
);
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';
Table Types
Replace SCHEMA_NAME with your schema.
Remember we cannot add record to table type. If you try to insert record, you will get
an error.
Insert data into the newly created table using SQL Editor:
• 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.
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
IF_AMDP_MARKER_HDB gets classified as a AMDP class.
• 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
in the respective environment
Restrictions in AMDP
ABAP for HANA - AMDP
• Calling a AMDP? – a Static method call
• 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
validations only avlb for SQL Script for HANA currently
Cannot edit in ABAP Workbench