SQLScript Extends Standard SQL
SQLScript is a set of extensions on top of standard SQL that employs the unique features of SAP HANA. It is based on the ANSI-92 SQL standard but adds extensions to exploit SAP HANA features:
- Datatype extensions
- Additional security
- Logic containers (procedures, functions)
- Implicitly- and explicitly-defined table variables
- Adds imperative logic
- Orchestration logic to control both imperative and declarative statements
By using these extensions, it allows much more pushdown of data-intensive processing to the SAP HANA database, which otherwise would have to be done at the application level.
Applications benefit most from the potential of SAP HANA when they perform as many data-intensive computations in the database as possible. This avoids loading large amounts of data into an application server separate from SAP HANA, and it leverages fast column operations, query optimization, and parallel execution. This can be achieved to a certain extent if the applications use advanced SQL statements, but sometimes you may want to push more logic into the database than is possible by using individual SQL statements, or by making the logic more readable and maintainable.
SQLScript has been introduced to assist with this task.
SQLScript Definition and Goal
SQLScript Advantages
Compared to plain SQL, SQLScript provides the following advantages:
Using SQLScript, complex logic can be broken down into smaller chunks of code. This encourages a modular programming style, which means better code reuse. SQL only allows the definition of SQL views to structure complex queries, and SQL views have no parameters.
SQLScript supports local variables for staging intermediate results with implicitly-defined types. With standard SQL, it would be required to define globally visible views even for intermediate steps.
SQLScript has flow control logic, such as "if-then-else" clauses and looping constructs, that are not available in SQL.
Stored procedures can return multiple results with different tabular structures, while an SQL query returns only one result set.
SQLScript supports input parameters so that code can be re-used in different scenarios by passing different parameter values. SQL does not support parameters and so SQL is usually limited in its use.
Pushing the processing to SAP HANA is a good thing because there are lots of opportunities for SAP HANA to optimize the execution with in-memory, parallel processing.
Standard SQL does not provide sufficient syntax to push many calculations to the database and, as a result, the application layer has to take on this duty. This means huge amounts of data must be copied between the database server and the application server.
The primitive data types, mentioned in the table below, are supported by SQLScript.
SQLScript currently allows a length of 8,388,607 characters for the NVARCHAR and the VARCHAR data types, unlike SQL where the length of that data type is limited to 5,000.
SQLScript Data Types
| Numeric types | TINYINT SMALLINT INT BIGINT DECIMAL SMALLDECIMAL REAL DOUBLE |
| Character String Types | VARCHAR NVARCHAR ALPHANUM |
| Date-Time Types | TIMESTAMP SECONDDATE DATE TIME |
| Binary Types | VARBINARY |
| Large Object Types | CLOB NCLOB BLOB |
| Spatial Types | ST_GEOMETRY |
| Boolean Type | BOOLEAN |
SQL vs. SQLScript
| SQL | SQLScript |
|---|
| Offload very limited functionality into the database using SQL. Most of the application logic is normally executed on an application server. | SQLScript uses the SQL extensions (for the SAP HANA database), allowing developers to push data-intensive logic to the database, better performance. |
| SQL views cannot be parameterized, which limits their reuse. | Re-usable views. |
| Does not have features to express business logic (for example a complex currency conversion). | Provide superior optimization possibilities. |
| SQL query can only return one result at a time. | Implement algorithms using a set-oriented paradigm and not using a one record at a time paradigm (imperative logic is required like iterative approximation algorithms). |
| SQL is a declarative language. | It is possible to mix ímperative constructs (procedural language) from stored procedures with declarative ones. |
It is important to remember that SQLScript is not a full application programming language such as ABAP or C++. But SQLScript can significantly reduce the need to program data-intensive tasks in the application layer by providing imperative language in the database. Imperative language allows the developer to add very precise control flow logic, for example, to the read tables one record at a time and process each record, then return to read the next record. Standard SQL does not allow this type of processing and only provides a way to write set-based logic that works on complete data sets from an instruction. That is why standard SQL needed to be extended with SQLScript to add more programmatic control in the database layer. This is especially necessary for transaction-based applications that operate at the record level.