SAP HANA Data Modeling vs SQL Scripting
SAP HANA Data Modeling vs SQL Scripting
Scalar UDFs in SAP HANA return a single scalar value and do not support operations involving tables, such as using cursors or arrays. They typically perform calculations or transformations on input data. In contrast, table UDFs return a set of records as a table, facilitating operations that return multiple rows of results based on logic defined within the function. Table UDFs can use SELECT queries to return tabular data but must be invoked using a SELECT query without the CALL statement .
Table types in SQL scripting are templates defining a table structure to hold data at runtime. They can be created using a defined syntax: CREATE TYPE <tabletypename> AS TABLE (column definitions). Table types support complex data handling by allowing for implicit and explicit creation on the fly within procedures, providing flexibility in managing multiple records dynamically .
In SQL scripting, containers are blocks of SQL script code used to organize logic. There are three types of container blocks: a) Anonymous SQL Block, which is used temporarily and compiled every time it runs; b) Stored Procedure, which is a pre-compiled anonymous block stored permanently in the database and can accept IN, OUT, and INOUT parameters; c) User Defined Functions (UDF), which always have input parameters, do not have output parameters, and always return one value .
In SQL script, exception handling utilizes specialized syntax based on SQL error codes, such as declaring exit handlers for specific SQL error codes, handling generic SQL exceptions, and signaling custom error messages. In contrast, traditional object-oriented programming (OOP) languages, such as ABAP, typically utilize class-based exception handling frameworks with a hierarchy (e.g., CX_ROOT in ABAP) and allow raising custom exceptions through such classes .
Procedures and functions in SAP HANA have distinct differences: Procedures can have IN, OUT, and INOUT parameters and are called using CALL statements but cannot be used in SQL queries. They can return multiple values. In contrast, functions always return a single value (scalar or table), must be called using SQL statements, and cannot have INOUT parameters. Direct assignment of output to variables is possible in functions, which is not the case for procedures .
HANA data modeling has several disadvantages compared to SQL scripting: 1) Views cannot be debugged. 2) Complex requirements cannot be decomposed effectively. 3) Control or orchestration logic, such as IF...ELSEIF or loops, cannot be implemented inside views. 4) SQL queries within views always return one result, with limited ability to change outcomes. 5) It cannot handle complex logic like looping, sorting, or searching. These limitations can be overcome through SQL scripting, which offers procedural capabilities within the database .
In SQL scripting, arrays cannot be returned from a procedure due to the procedural constraints that only allow returning tables or scalar values. To transfer data from an array to a table, you can unnest the array and assign it to a table variable within the procedure. This allows the transformation of array data into a tabular format, which can then be returned from the procedure .
SQL script exits parallel mode during execution under specific conditions: when a local variable or scalar parameter is used, when a data manipulation language (DML) or data definition language (DDL) operation is found inside a processing block, when imperative logic like IF statements, loops, or cursors are encountered, and when SQL statements are not assigned to implicit variables .
Anonymous SQL blocks in SQL scripting are limited as they cannot be used for productive purposes, they require recompilation every execution, and have maintenance challenges. Stored procedures address these limitations by being pre-compiled and permanently stored in the database, allowing for reuse across different containers, ensuring better performance, and maintaining consistency without needing recompilation every time .
SAP HANA achieves parallelization through declarative logic, where SQL code is converted into a Data Flow graph, allowing for potential parallel processing. However, parallelization is lost when imperative logic (such as IF, loops, or cursors) is used, when local variables or scalar parameters are declared, during data manipulation language (DML) or data definition language (DDL) operations inside a processing block, and when SQL statements are not assigned to implicit variables .