SQLScript Syntax For SAP HANA
SQLScript Syntax For SAP HANA
SQLScript Syntax For SAP HANA
In contrast to definitions of database tables, table types do not have an instance that is created when the table
type is created (i.e., no Data Manipulation Language (DML) statements (INSERT, UPDATE, DELETE) are
supported on table types).
Example:
CREATE TYPE tt_publishers AS TABLE ( publisher INTEGER, name VARCHAR(50), price
DECIMAL, cnt INTEGER);
CREATE TYPE tt_years AS TABLE ( year VARCHAR(4), price DECIMAL, cnt INTEGER);
Create Procedure
It is a callable statement, and so it can be called using a CALL statement. Read-only procedures with one
table output parameter support the additional creation of a result view.
BEGIN
END;
The SQLScript above defines a read-only procedure which has 2 scalar input parameters and 2 output
parameters of type table. As different languages are supported in the body of the procedure, the
implementation language is defined to be SQLScript. Moreover, the procedure is a read-only procedure.
Hence it is free of side effects and can be called by other read-only procedures.
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 1/11
07/08/2017 SQLScript Syntax for SAP HANA
Syntax:
CREATE PROCEDURE {schema.}name {({IN|OUT|INOUT} param_name data_type {,...})}
{LANGUAGE <LANG>} {SQL SECURITY <MODE>} {READS SQL DATA {WITH RESULT VIEW
<view_name>}} AS
BEGIN
...
END
The implementation language is by default SQLSCRIPT. You can specify the security mode. Privileges are
always checked with the privileges of the definer of a procedure when the procedure is created. With security
mode definer, which is the default, execution of the procedure is then performed with the privileges of the
definer of the procedure. The other alternative is mode invoker.
If a read-only procedure has exactly one table output parameter a RESULT VIEW can be specified.
Normally, procedures can only be executed via the call statement. By using WITH RESULT VIEW it is possible to
query the result of a procedure as part of a SQL statement. Please no ce that no variable references are supported in
the WITH PARAMETERS clause and that all constants are passed as string constants (i.e. enclosed in single quotes). Also,
parameter names are lower case independent from the original capitaliza on in the procedure signature.
Example:
DROP PROCEDURE
Syntax:
DROP PROCEDURE {schema.}name {CASCADE}
ALTER PROCEDURE
Syntax:
Procedure Calls
A procedure can be called by a client on the outer-most level, using any of the supported client interfaces, or
within the body of a procedure.
A common requirement is to write the result of a procedure call directly into a physical table. For this purpose
a procedure can be called using the CALL WITH OVERVIEW syntax. This flavor of calling a procedure
returns one result set that holds the information of which table contains the result of a particular tables output
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 2/11
07/08/2017 SQLScript Syntax for SAP HANA
variable. Scalar outputs will be represented as temporary tables with only one cell. CALL WITH OVERVIEW
is the replacement for the former CALLS statement. When passing existing tables to the output parameters
CALL WITH OVERVIEW will insert the result set tuples of the procedure into the given tables. When
passing NULL to the output parameters, temporary tables holding the result sets will be generated. These
tables will be dropped automatically once the session is closed. The semantics of the optional IN DEBUG
MODE clause is the same as for the call without WITH OVERVIEW clause.
Example:
CALL getOutput (1000, 'EUR', ot_publishers, NULL) WITH OVERVIEW;
Returns the following table: Variable Table
OUPPUT_PUBS "SYSTEM"."OT_PUBLISHERS"
OUTPUT_YEAR "SYSTEM"."OUTPUT_YEAR_4CF5FA0BF9397F8
8E10000000A12737F"
Example:
When procedure addDiscount is called, the variable :lt_expensive_books is assigned to the func on and the
variable lt_on_sales is bound by this func on call.
Scalar Variables
BEGIN
v_isbn := '978-3-8266-1664-8';
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 3/11
07/08/2017 SQLScript Syntax for SAP HANA
SELECT isbn, title, price, crcy INTO v_isbn, v_title, v_price, v_crcy FROM books
WHERE isbn = :v_isbn;
v_isbn := '978-3-642-19362-0';
v_price := 42.75;
SELECT isbn, title, price, crcy INTO v_isbn, v_title, v_price, v_crcy FROM books
WHERE isbn = :v_isbn;
SELECT isbn, title, price, crcy INTO v_isbn, v_title, v_price, v_crcy FROM books
WHERE isbn = :v_isbn;
END;
Control Structures
Conditionals
Syntax:
IF <bool-expr1>
THEN {then-stmts1}
{ELSEIF <bool-expr2>
THEN
{then-stmts2}}
{ELSE
{else-stmts3}}
END IF
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 op onal.
If the Boolean expression bool-expr1 does not evaluate to true the ELSE-branch is evaluated. In most cases this
branch starts with ELSE. The statements else-stmts3 are executed without further checks. A er an else branch no
further ELSE branch or ELSEIF branch is allowed.
Alterna vely, when ELSEIF is used instead of ELSE another boolean expression bool-expr2 is evaluated. If it
evaluates to true, the statements then-stmts2 is executed. In this fashion an arbitrary number of ELSEIF clauses
can be added.
This statement can be used to simulate the switch-case statement known from many programming languages.
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 4/11
07/08/2017 SQLScript Syntax for SAP HANA
IF :found = 0
THEN INSERT INTO books VALUES (:v_isbn, 'In-Memory Data Management', 1, 1,
'2011', 42.75, 'EUR');
ELSE UPDATE books SET price = 42.75 WHERE isbn =:v_isbn;
END IF;
END;
While Loop
Syntax:
WHILE <bool-stmt> DO {stmts} END WHILE
v_index1 := 0;
WHILE :v_index1 < 5 DO
v_index2 := 0;
WHILE :v_index2 < 5 DO
v_index2 := :v_index2 + 1;
END WHILE;
v_index1 := :v_index1 + 1;
END WHILE;
For Loop
Syntax:
FOR <loop-var> IN {REVERSE} <start> .. <end> DO
{stmts}
END FOR
The for loop iterates a range of numeric values denoted by start and end in the syntax and binds the
current value to a variable (loop-var) in ascending order. Iteration starts with value start and is incremented by
one until the loop-var is larger than end. Hence, if start is larger than end, the body loop will not be evaluated.
FOR v_index1 IN -2 .. 2 DO
FOR v_index2 IN REVERSE 0 .. 5 DO
CALL ins_msg_proc('Here is ' ||:v_index1 || '~' ||:v_index2 || '.');
END FOR;
END FOR;
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 5/11
07/08/2017 SQLScript Syntax for SAP HANA
END IF;
END FOR;
Cursors
Cursors are used to fetch single rows from the result set returned by a query. When the cursor is declared, it is bound to
a query. It is possible to parameterize the query to which the cursor is bound.
Cursors are defined like local variables, i.e. after the signature of the procedure and before the procedures
body. The cursor is defined with a name, optionally a list of parameters, and a SQL SELECT statement. The
cursor provides the functionality to iterate through a query result row-by-row. Updating cursors is not
supported.
Open Cursor
Syntax:
OPEN <cursor-name>{(arg1, ...)}
Close Cursor
Syntax:
CLOSE <cursor-name>
A ribute Descrip on
cur::ISCLOSED Is true if cursor cur is closed, true otherwise.
cur::NOTFOUND Is true if the previous fetch opera on returned
no valid row, false otherwise. Before calling OPEN
or a er calling CLOSE on a cursor this will always
return true.
cur::ROWCOUNT Returns the number of rows in the result set of
the query bound to the cursor. This value is
available a er the rst FETCH opera on.
BEGIN
OPEN c_cursor1('978-3-86894-012-1');
IF c_cursor1::ISCLOSED
THEN
CALL ins_msg_proc('WRONG: cursor not open'); ELSE CALL ins_msg_proc('OK: cursor
open');
END IF;
FETCH c_cursor1 INTO v_isbn, v_title, v_price, v_crcy;
IF c_cursor1::NOTFOUND
THEN
CALL ins_msg_proc('WRONG: cursor contains no valid data');
ELSE
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 6/11
07/08/2017 SQLScript Syntax for SAP HANA
Dynamic SQL
With dynamic SQL it is possible to construct SQL statements at run me of a SQLScript procedure.
Syntax:
EXEC '<sql-statement>'
It is recommended to avoid dynamic SQL because it might have a negative impact on security or performance.
Catalog Information
When a procedure or func on is created, this will be reected in the database catalog. One can use this informa on for
debugging purposes. For procedures informa on are stored in system views summarized below.
View SYS.PROCEDURES:
typically required to understand errors returned by the SQLScript compiler or unexpected runtime behavior of
SQLScript procedures.
menu View Open Perspective Administration Console. In this perspective tracing information is
available in the panel called Diagnosis Files.
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 7/11
07/08/2017 SQLScript Syntax for SAP HANA
sqlscript: informa on related to the SQLScript compiler, i.e. the early phases of compiling SQLScript into calcula on
models
calcengineinstan ate: informa on collected during instan a on of a calcula on model, i.e. the nal phase of
compiling SQLScript into calcula on models and when the SQLScript procedure is invoked.
calcengine: informa on collected by the calcula on engine at run me. This is relevant for the parts of SQLScript that
are translated into calcula on models.
llang: informa on collected by the L component. This is relevant for all aspect of SQLScript the relate to L, i.e.
compila on into L and execu on of L code.
Performance Trace
To analyze the performance characteris cs of a given workload it is possible to collect performance informa on. It
allows specialists to associate long-running SQLScript or SQL features with code fragments. Based on this informa on
rewrites of the SQL / SQLScript code can be proposed or performance issues in the database engine can be iden ed.
The temporary table #PROCEDURE_DATAFLOWS_ can be used to iden fy the data ows into which the SQLScript
compiler translated the SQLScript source. In our example, the compiler creates a single dataow, which is op mal for
op mizing the script into an ecient plan.
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 8/11
07/08/2017 SQLScript Syntax for SAP HANA
BEGIN
t_big_pub_ids = SELECT publisher AS pid FROM books -- Query Q1 GROUP BY publisher
HAVING COUNT(isbn) > :cnt;
big_pub_ids = TRACE(:t_big_pub_ids);
t_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;
big_pub_books = TRACE(:t_big_pub_books);
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;
If you want to clear the content you may call the following built-in procedure.
call truncate_sqlscript_trace;
Posted 8th November 2012 by JD
18 View comments
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 10/11
07/08/2017 SQLScript Syntax for SAP HANA
http://inmemoryhana.blogspot.in/2012/11/sqlscript-syntax-for-sap-hana.html 11/11