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:
12books_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
Classification | Standard ANSI-92 Data Types | SQLScript-specific Data Types |
---|---|---|
Date/Time | DATE, TIME, TIMESTAMP | SECONDDATE |
Numeric | DECIMAL, INTEGER, SMALLINT, FLOAT, REAL, DOUBLE | TINYINT, BIGINT, SMALLDECIMAL |
Character string | CHAR, NCHAR, VARCHAR, NVARCHAR | ALPHANUM, SHORTTEXT |
Binary | BINARY, 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 Snippet1234SELECT "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 Snippet1Sales 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.