SAP HANA SQLScript: Top Questions & Expert Answers

Introduction

SAP HANA SQLScript is an advanced database programming language that enhances SQL with procedural capabilities, allowing developers to push data-intensive logic into the HANA database. If you're working with SAP HANA, understanding SQLScript is essential for optimizing performance and handling complex data operations.

In this guide, we'll answer 16 essential questions related to SQLScript, covering everything from syntax and execution to data types and debugging.

1. What is SQLScript?

SQLScript is a set of SQL extensions for SAP HANA that enables developers to write complex logic within the database. It enhances standard SQL by supporting procedural constructs, which help optimize performance for data-intensive operations.

2. When Should SQLScript Be Used?

SQLScript should be used when:
  • Standard modeling constructs like Attribute Views or Analytic Views are insufficient.
  • The business logic requires complex calculations and transformations that cannot be efficiently handled by SQL alone.
  • You need better performance by pushing calculations to the database layer instead of handling them at the application level.

3. How Is an SQLScript Procedure Processed in HANA?

When a new SQLScript procedure is created, the HANA database processes it in the following steps:
  • Parsing – The procedure is analyzed for syntax errors.
  • Semantic Check – Ensures the correctness of SQL statements.
  • Optimization – The compiler optimizes declarative and imperative logic.
  • Code Generation – Creates calculation models for declarative logic and L nodes for imperative logic.
  • Compilation – The procedure is compiled and stored in the HANA Catalog and Repository.

4. What Happens When You Compile an SQLScript Procedure?

During compilation, the procedure call is rewritten for processing by the Calculation Engine. This improves performance by optimizing execution plans before running queries.

5. What Happens When You Execute an SQLScript Procedure?

When executing a procedure:
  • Actual parameters are bound to the calculation models created during definition.
  • The procedure is executed in the database using these predefined models.
  • Results are returned to the user based on optimized execution plans.

6. What Data Types Are Supported in SQLScript?

SQLScript supports various primitive and complex data types:

Primitive Data Types

  • INTEGER
  • DECIMAL(p,q)
  • VARCHAR(n)
  • BLOB
  • TIMESTAMP

Table Types

SQLScript also allows defining table types, which represent tabular results in procedures.
CREATE TYPE tt_publishers AS TABLE (
    publisher INTEGER,
    name VARCHAR(50),
    price DECIMAL,
    cnt INTEGER
);

7. What Is a Table Type in SQLScript?

A table type defines a structure for a tabular result set and is primarily used for procedure parameters.
CREATE TYPE tt_years AS TABLE (
    year VARCHAR(4),
    price DECIMAL,
    cnt INTEGER
);
Table types allow flexibility when working with result sets inside procedures and functions.

8. What Is the Syntax for Creating an SQLScript Procedure?

CREATE PROCEDURE <proc_name> [(<parameter_clause>)]  
[LANGUAGE <lang>] [SQL SECURITY <mode>]  
[READS SQL DATA [WITH RESULT VIEW <view_name>]] AS  
<local_scalar_variables>  
BEGIN  
    <procedure_code>  
END;
Example:
CREATE PROCEDURE get_books(IN min_price DECIMAL, OUT result TABLE)
AS 
BEGIN
    result = SELECT * FROM books WHERE price > :min_price;
END;

9. How to Recompile an SQLScript Procedure?

To recompile a procedure, use:
ALTER PROCEDURE proc_name RECOMPILE [WITH PLAN];
This ensures the latest execution plan is generated for optimal performance.

10. Can You Specify Default Values in an SQLScript Procedure?

Yes, default values can be assigned to procedure parameters.

Example:

CREATE PROCEDURE my_proc (IN P1 INT, IN P2 INT DEFAULT 1, OUT out1 DUMMY) AS  
BEGIN  
    out1 = SELECT :P1 + :P2 AS DUMMY FROM DUMMY;  
END;
Calling the procedure with named parameters:
CALL my_proc (P1 => 3, out1 => ?);

11. What Is the Purpose of ‘CALL… WITH OVERVIEW’ in SQLScript?

The WITH OVERVIEW option allows the procedure results to be written directly into a physical table.

Example:

CALL my_proc (3, 4, NULL) WITH OVERVIEW;

12. What Is the Purpose of ‘CALL… IN DEBUG MODE’ in SQLScript?

Executing a procedure in debug mode creates additional debugging information during execution.

Example:

CALL my_proc (3, 4, NULL) IN DEBUG MODE;
Note: Debug mode has a performance impact and should only be used for troubleshooting.

13. What Is a Table Variable in SQLScript?

A table variable holds tabular data within a procedure and is bound to a physical table, SQL query, or a Calculation Engine plan operator.

14. What Is a Scalar Variable in SQLScript?

A scalar variable is defined as IN, OUT, or INOUT and holds a single value.

Example:

lt_expensive_books = SELECT title, price FROM :it_books WHERE price > :minPrice;
  • it_books: IN parameter (table type)
  • minPrice: IN parameter (scalar type)

15. What Is a CE Plan Operator? What Are Its Categories?

A CE (Calculation Engine) Plan Operator is used for data transformations in SQLScript.

Types of CE Operators

  1. Data Source Access Operators
    • CE_COLUMN_TABLE, CE_JOIN_VIEW, CE_OLAP_VIEW, CE_CALC_VIEW
  2. Relational Operators
    • CE_JOIN, CE_PROJECTION, CE_AGGREGATION, CE_UNION_ALL
  3. Special Extensions
    • CE_VERTICAL_UNION, CE_CONVERSION

16. What Is the CE_COLUMN_TABLE Operator?

The CE_COLUMN_TABLE operator retrieves data from a column table without invoking SQL processing.

Example:

ot_books = CE_COLUMN_TABLE("BOOKS");
This is equivalent to:
SELECT * FROM BOOKS;
Benefit: Bypasses SQL processing, directly accessing column-store tables for better performance.

Conclusion

SQLScript in SAP HANA is a powerful tool for handling complex data transformations and high-performance calculations. Understanding its syntax, execution process, and optimization techniques ensures better database efficiency and improved query performance.

FAQs

1. Is SQLScript the same as SQL?

No, SQLScript extends SQL by adding procedural logic and optimized execution in SAP HANA.

2. How do I debug an SQLScript procedure?

Use the CALL… IN DEBUG MODE command to generate debugging information.

3. What are the benefits of using SQLScript in HANA?

  • Optimized performance
  • Pushes logic to the database
  • Supports complex transformations

4. Can SQLScript be used with other SQL queries?

Yes, SQLScript works alongside standard SQL queries to enhance functionality.

5. Does SQLScript support loops and conditional statements?

Yes, SQLScript supports IF conditions, loops, and case statements.

SAP HANA

Get help for your SAP HANA problems
SAP HANA Forum - Do you have a SAP HANA Question?

SAP HANA
SAP HANA Hints and Tips

Main Index
SAP ERP Modules, Basis, ABAP and Other IMG Stuff

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.