Working with SQLScript

Objective

After completing this lesson, you will be able to Describe the additional features provided by SQLScript compared to standard SQL.

SQLScript Capabilities

SQLScript is a database language developed by SAP. It is based on standard SQL but adds additional capabilities to exploit the advanced features of SAP HANA Cloud.

Here are some of the key features of SQL Script:

  • SQLScript extends ANSI-92 SQL. Just like other database vendors extend SQL in their databases, SAP HANA extends SQL using SQLScript.
  • SQLScript enables you to access SAP HANA-specific features like column tables, parameterized information views, delta buffers, working with multiple result sets in parallel, built-in currency conversions at database level, fuzzy text searching, spatial data types, and predictive analysis libraries.
  • SQLScript allows developers to push data intensive logic into the database.
  • SQLScript encourages developers to maintain algorithms using a set-oriented paradigm, instead of a one record at a time paradigm.
  • SQLScript does however allow looping through results and using if-then-else logic
  • SQLScript allows you to break complex queries into multiple smaller statements, thereby simplifying your SQL coding, and enhancing parallelism via the optimizer.

Note

The version of standard SQL that SQLScript is based on is the popular ANSI-92 version which is supported by nearly all databases.

Additional features of SQLScript include working with SAP HANA Cloud column tables, passing parameters between calculation views and SQLScript, working with spatial data types, and predictive analysis libraries.

Developing Business Logic with SQLScript

In traditional client-server approaches, the business logic is defined and executed in the application server using application programming languages such as ABAP or C++. With SAP HANA Cloud, much of this logic can now be defined using SQLScript, which allows the developer to define and execute all data processing tasks in the database in order to achieve improved performance.

SQLScript allows you to use variables to break a large complex SQL statement into smaller, simpler statements. This makes the code much easier to understand. It also helps with SQL HANA Cloud’s performance because many of these smaller statements can be run in parallel.

Watch this video to look at an example:

In the studied example, we want to find out which publisher has the most books:

Code Snippet
Copy code
Switch to dark mode
12
books_per_publisher = SELECT publisher, COUNT (*) AS num_books FROM BOOKS GROUP BY publisher; publisher_with_most_books = SELECT * FROM :books_per_publisher WHERE num_books >= (SELECT MAX (num_books) FROM :books_per_publisher);

The SQLScript compiler and optimizer determine how to best execute these statements, whether by using a common sub-query expression with database hints or by combining this into a single complex query. The code becomes easier to write and understand, more maintainable, and developer productivity increases.

By breaking the SQLScript into smaller statements and filling table variables, you also mirror the way in which you have learned to build your calculation views. Just like when you start building calculation views in layers, starting from the bottom up, you do the same with your SQLScript code. Keep in mind that the precise sequence of your SQLScript steps is not necessarily the runtime order, because the optimizer will always decide on the best execution plan. However, the optimizer will never alter your desired outcome. This is also true for calculation views.

The Use of Variables

The use of variables in SQL statements is not specified by the ANSI-92 SQL. In SAP HANA Cloud, SQLScript implements the concept of variable as an extension to ANSI-92 SQL.

Variables can be declared, assigned, and reused within a script.

Using Variables in SQLScript

  • Declare a variable

    DECLARE <variable_name> <type> [NOT NULL] [= <value>]

    Example 1: DECLARE a int;

    Example 2: DECLARE b int = 0; (the value is assigned when creating the variable)

  • Assign a variable (define the value of a variable)

    <variable_name> = <value> or <expression>

    Example 1: a = 3;

    Example 2: b = select count(*) from baseTable;

  • Use a variable in an expression

    :<variable_name> (with a colon) returns the current value of the variable

    Example: b = :a + 5; (assigns the value of a + 5 to b)

SQLScript Data Types

SQLScript also adds extra data types to help address missing features from standard SQL - for example, spatial data types and text data types.

ANSI-92 and SQLScript Data Types

ClassificationStandard ANSI-92 Data TypesSQLScript-specific Data Types
Date/TimeDATE, TIME, TIMESTAMPSECONDDATE
NumericDECIMAL, INTEGER, SMALLINT, FLOAT, REAL, DOUBLETINYINT, BIGINT, SMALLDECIMAL
Character stringCHAR, NCHAR, VARCHAR, NVARCHARALPHANUM, SHORTTEXT
BinaryBINARY, VARBINARY 
Large Object BLOB, CLOB, NCLOB, TEXT, BINTEXT
Boolean BOOLEAN
Spatial ST_POINT, ST_GEOMETRY

Processing Input Parameters Defined in Calculation Views with SQLScript

When you use a select statement in SQLScript to query a calculation view that has input parameters, you use the PLACEHOLDER keyword with the name of the input parameter wrapped in $$ symbols, and the value you want to pass to the input parameter - for example:

Passing input parameters to the calculation view

Use the PLACEHOLDER keyword to pass input parameters to the calculation view in a select statement:

Code Snippet
Copy code
Switch to dark mode
1234
SELECT "PRODUCT", sum("TAXED_AMOUNT") FROM "_SYS_BIC"."MyPackage/SalesResults" ('PLACEHOLDER' = ('$$Tax Amount$$', '17.5')) GROUP BY "PRODUCT"
Referring to the input parameter in SQL Script

Within SQLScript, you refer to the input parameter using a colon:;

Code Snippet
Copy code
Switch to dark mode
1
Sales with tax = Sales Revenue * :Tax_Amount

It is also possible to pass output parameters generated from an SQLScript, into a calculation view using the parameter mapping feature.

Looping and Conditional Logic

In general, SQL code is written in a declarative way. This means that you state the intent of the action and do not dictate how the data processing steps should be carried out in a particular order. We do this so that you allow the SQL optimizer to figure out the best execution plan based on many dynamic factors at runtime.

However, sometimes you need to control the flow logic of the SQL so that steps are performed in a particular order. For example, when you need to perform a step only if a certain condition is met, or you need to loop a number of times. In these cases, you will use imperative logic.

This is (mostly) not available in ANSI SQL, and therefore SAP HANA provides declarative logic language as part of SQLScript to provide flow control.

A lack of imperative logic language is what often causes the developer to move the processing of data to the application layer. When an application runs on HANA, you really should push as much data processing as possible to the database. SAP HANA SQLScript imperative logic enables this so less data handling needs to be done in the application layers and more can be done in the database.

Hint

Remember that imperative logic cannot have the same performance as declarative logic. This is because the SQL optimizer is not free to decide on the sequence of steps and has to take care to follow your logic, which might destroy optimizations. If you require the best performance, try to avoid imperative logic.

Conditional Logic

The IF statement
Code Snippet
Copy code
Switch to dark mode
12345678910
IF <bool-expr1> THEN {then-stmts1} {ELSEIF <bool-expr2> THEN {then-stmts2}} {ELSE {else-stmts3}} END IF
Example of an IF statement using an ELSE clause.

The previous figure shows you an example of the IF statement, which consists of a Boolean expression, :found is NULL. If the expression is evaluated as true, then the statement CALL ins_msg_proc('result of count(*) cannot be NULL'); in the mandatory THEN block is executed. The IF statement ends with END IF. The remaining parts are optional.

If the Boolean expression :found is NULL does not evaluate as true, then the ELSE branch is evaluated. In most cases this branch starts with ELSE. The statement CALL ins_msg_proc('result of count(*) not NULL- as expected'); is executed without further checks. After an else branch, no further ELSE branch or ELSEIF branch is allowed.

Alternatively, when ELSEIF is used instead of ELSE, another Boolean expression can be evaluated. If It evaluates as true, the following statement is executed. You can add an arbitrary number of ELSEIF clauses in this way.

This statement can be used to simulate the switch-case statement known from many programming languages.

WHILE and FOR Loops

The WHILE loop
Code Snippet
Copy code
Switch to dark mode
123
WHILE <bool-stmt> DO {stmts} END WHILE
The WHILE loop executes the statement stmts in the body of the loop as long as the Boolean expression at the beginning bool-stmt of the loop evaluates as true.
The FOR loop
Code Snippet
Copy code
Switch to dark mode
123
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.

Therefore, if start is larger than end, the body loop will not be evaluated. For each enumerated value of the loop variable the statements in the body of the loop are evaluated. The optional keyword REVERSE specifies to iterate the range in descending order.

Log in to track your progress & complete quizzes