Introduction to SAP HANA SQLScript Rich Heilman SESSION CODE: BT162
Disclaimer This presentation outlines our general product direction and should not be relied on in making a purchase decision. This presentation is not subject to your license agreement or any other agreement with SAP. SAP has no obligation to pursue any course of business outlined in this presentation or to develop or release any functionality mentioned in this presentation. This presentation and SAP's strategy and possible future developments are subject to change and may be changed by SAP at any time for any reason without notice. This document is provided without a warranty of any kind, either express or implied, including but not limited to, the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this document, except if such damages were caused by SAP intentionally or grossly negligent. 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 3
Agenda Overview Wizard, Editor & Viewer Debugger SQLScript Language Features User Defined Functions(UDFs) Triggers ABAP Connectivity 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 4
Overview
Overview What? SQLScript is An interface for applications to access SAP HANA Extension of ANSI Standard SQL Language for creating stored procedures in HANA Declarative Logic SELECT queries Calculation Engine(CE) functions Orchestration Logic Data Definition Language(DDL) Data Manipulation Language(DML) Assignment & imperative logic 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 6
Overview Why? Main goal is to allow the execution of data intensive calculations inside SAP HANA Two reasons why this is required to achieve the best performance: Eliminate data transfer between database & application tiers Calculations need to be executed in the database layer to get the maximum benefit from SAP HANA features such as fast column operations, query optimization and parallel execution. 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 7
Overview Advantages Compared to plain SQL queries, SQLScript has the following advantages: Returns multiple results, while a SQL query returns only one result set Complex logic can be broken down into smaller chunks of code. Enables modular programming, reuse and a better understandability by functional abstraction. For structuring complex queries, standard SQL only allows the definition of SQL views. However, SQL views have no parameters SQLScript supports local variables for intermediate results with implicitly defined types. With standard SQL, it would be required to define globally visible views even for intermediate steps SQL Script has control logic such as if/else that is not available in SQL 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 8
Overview Application Development Technologies Front-end Technologies http/s HTML5 / SAPUI5 Client-side JavaScript Client: Browser or Mobile Presentation logic Control Flow Technologies OData Server-Side JavaScript XMLA SAP HANA XS Control flow logic Data Processing Technologies SQL / SqlScript Calculation Engine Functions Application Function Library (AFL) Data Calculation logic 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 9
Overview Traditional Programming Model vs. New Programming Model Traditional: Data to Code New Model: Code to Data Massive data copies creates bottleneck Application Layer Code Transfer Minimum Result Set Application Layer DB Layer DB Layer Code 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 10
Overview SQLScript Code Example BEGIN... -- Query 1 product_ids = select "ProductId", "Category", "DescId" from "SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::products" where "Category" = 'Notebooks' or "Category" = 'PC'; -- Query 2 product_texts = select "ProductId", "Category", "DescId", "Text" from :product_ids as prod_ids inner join "SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::texts" as texts on prod_ids."descid" = texts."textid"; -- Query 3 out_notebook_count = select count(*) as cnt from :product_texts where "Category" = 'Notebooks'; Q1 Q2 Input Q5 -- Query 4 out_pc_count = select count(*) as cnt from :product_texts where "Category" = 'PC'; Q3 Q4 -- Query 5 out_total_count = select count(*) as cnt from "SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::products ;... END; Notebooks PCs Total 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 11
Overview Parallel Processing SELECT statements are executed in parallel unless: Any local scalar parameters and variables are used in the procedure Any read/write procedures or DML/DDL operations are executed Any imperative logic is used within the procedure, such as IF statements of FOR loops Any SQL statements are used that are not assigned to a intermediate variable or parameter 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 12
Wizard, Editor & Viewer
Wizard, Editors & Viewer Stored Procedure Wizard in SAP HANA Studio Stored Procedure under Database Development Two file formats.hdbprocedure is the new artifact type which will be the recommended file format moving forward..procedure is still supported, but has been deprecated and eventually will be removed. Target schema definition 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 14
Wizard, Editor & Viewer Editor Integration with SAP HANA Development Perspective Source code based editor Client side syntax checking Code hints Syntax highlighting Semantic code completion CTRL+SPACE to trigger Lists relevant objects based on context Searches for any matches within the object name 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 15
Wizard, Editor & Viewer Editor Integration in the Web-Based Development Workbench Keyword code completion Syntax highlighting.hdbprocedure file format only 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 16
Wizard, Editor & Viewer SQLScript Procedure Viewer Procedures in the catalog can now be viewed in the SQLScript editor in read-only mode. Allows for setting/removing of breakpoints within the runtime object. 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 17
Wizard, Editor & Viewer Procedure Code Breakdown Schema definition developer can define which schema in which the run-time object of the procedure will be created 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 18
Wizard, Editor & Viewer Procedure Code Breakdown Package hierarchy and procedure name contains the complete package hierarchy as well as the name of the procedure, separated by double colon(::) 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 19
Wizard, Editor & Viewer Procedure Code Breakdown Input/output parameter definition developer can define both input parameters with default values as well as output parameters. Parameters can reference simple types, in-line table types, or global types defined via CDS 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 20
Wizard, Editor & Viewer Procedure Code Breakdown Metadata declarations: developer can set language(sqlscript/r), security(invoker/definer), default schema and read/write access 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 21
Wizard, Editor & Viewer Procedure Code Breakdown Script body developer writes the body of the script between the BEGIN and END statements 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 22
Debugger
Debugger Debug Perspective within SAP HANA Studio Resume/Terminate Variable evaluation Breakpoint management Break on break-points Basic step debugging 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 24
Debugger Debug Configuration Define procedure to be debugged Debug both repository and catalog procedures 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 25
Debugger Debug Configuration Debug with input parameters 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 26
Debugger External Debugging Attach to running session By connection ID By application user ID 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 27
Debugger Debugging in the Web-based Development Workbench Set breakpoints in the runt-time object in the catalog Call procedure from the SQL console Resume & step over functions Scalar & table variable/parameter evaluations 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 28
SQLScript Language Features
SQLScript Language Features Declarative Logic Allows the developer to declare the data selection via SELECT or CE(Calculation Engine) functions Developer defines the what Engine defines the how and executes accordingly Massive parallelized 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 30
SQLScript Language Features Imperative Logic Allows developer to control the flow of the logic within SQLScript. Scalar variable manipulation DDL/DML logic WHILE loops Branching logic based on some conditions, for example IF/ELSE Executed exactly as scripted, procedurally No parallel processing 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 31
SQLScript Language Features Arrays Allows the developer to define and construct arrays within SQLScript Set elements Return elements Remove elements Concatenate two arrays Turn array into a table Turn a column of a table into an array Return cardinality of an array 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 32
SQLScript Language Features Dynamic Filtering Allows developers to apply a dynamic WHERE clause to SELECT statements Both database tables and intermediate table variables are supported 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 33
SQLScript Language Features Exception Handling Exception handling is a method for handling exception and completion conditions in an SQLScript procedures The DECLARE EXIT HANDLER statement allows you to define exception handlers to process exception conditions in your procedures. You use the DECLARE CONDITION parameter to name exception conditions, and optionally, their associated SQL state values. You can use SIGNAL or RESIGNAL with specified error code in user-defined error code range. A user-defined exception can be handled by the handler declared in the procedure. Also it can be handled by the caller which can be another procedure or client. 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 34
SQLScript Language Features Exception Handling Declare exit handlers for generic SQL exceptions Declare exit handlers for specific SQL error codes 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 35
SQLScript Language Features Exception Handling Signaling and catching user-defined conditions 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 36
SQLScript Language Features Exception Handling RESIGNAL user defined exceptions to the caller RESIGNAL can also be executed explicitly within the body of the exit handler 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 37
SQLScript Language Features Autonomous Transactions Allows developer to create an isolated block of code which runs as an independent transaction BEGIN AUTONOMOUS TRANSACTION END statement block Committed statements inside autonomous transaction block will be persisted regardless of a rollback of the main transaction. COMMIT & ROLLBACK are allowed only within the AUTONOMOUS TRANSACTION block For tables updated within the main procedure body, access to those tables is not allowed in the autonomous transaction block Used commonly for logging tasks 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 38
SQLScript Language Features Cursors Allows developers to iterate over a result set and perform row-based processing and calculations. 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 39
User Defined Functions(UDF)
User Defined Functions Overview Language used within the body is SQLScript, other languages are supported. Functions are read-only, side effect free. Two types, both have a wizard and associated editor: Table User Defined Function(.hdbtablefunction artifact) Scalar User Defined Function(.hdbscalarfunction artifact) 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 41
User Defined Functions Table UDF Can have any number of input parameters Returns exactly one table Table operations are allowed within the body Consumed in the FROM clause of a SELECT statement 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 42
User Defined Functions Scalar UDF Can have any number of input parameters Can return multiple values Cursors, SELECT INTO, and procedure calls are allowed within the body but not recommended Input parameters can not be table type Consumed from the field list or the WHERE clause of the SELECT statement. Also callable via direct assignment( x := my_scalar_func() ) 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 43
Triggers
Triggers Overview Special type of stored procedure that automatically executes when an event occurs in the database server Triggers can be executed BEFORE or AFTER an event on a given table, such as INSERT, UPDATE, or DELETE Management of objects can only be done via SQL Console. Support for storing the artifacts in the repository is coming in a future support package 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 45
Triggers Limitations INSTEAD_OF trigger not supported Statement level trigger is only supported for row-store tables Access to subject table is blocked, no operations on the table which the trigger was created on. Maximum trigger number per single table per DML is 1024, which means a table can have maximum 1024 insert triggers, 1024 update triggers and 1024 delete triggers in the same time Trigger execution order is not guaranteed Statements not supported in the body of a trigger include: Result set assignments(select resultset into table) Exit/Continue/Return statements Cursor operations Procedure calls(before SPS09) Dynamic SQL 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 46
Triggers Examples 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 47
Triggers Examples 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 48
ABAP Connectivity
How can my ABAP code benefit from SAP HANA? The new paradigm Calculation AS ABAP Data to Code Code to Data SAP HANA Database Calculation Code pushdown means delegating data intense calculations to the database layer 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 50
Secondary database connections Secondary connections can be used to access local or remote database systems are maintained via SM30 for table DBCON; entries can be transported require specification of connection data including user (=DB schema) and password are supported in the Open SQL syntax by using the CONNECTION supplement form an own transaction context Service note 1597627 describes the prerequisites and procedure for setting up a secondary connection to HANA. 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 51
ABAP Database Connectivity(ADBC) CL_SQL_CONNECTION GET_CONNECTION CREATE_STATEMENT and PREPARE_STATEMENT ROLLBACK and COMMIT Have a look at the Classbuilder (SE24) or Transaction SE80! CL_SQL_PREPARED_STATEMENT / CL_SQL_STATEMENT PREPARE / CLOSE Prepare / release an SQL Statement SET_PARAM - Set an Input/Output Parameter (variants for CLOB, BLOB, STRUCT, TABLE (available soon)) PREPARED_QUERY, PREPARED_UPDATE - Execute a Prepared Query / DML Operation EXECUTE_DDL, EXECUTE_QUERY, EXECUTE_UPDATE - Execute DDL, Query, DML (Insert, Update, Delete) CL_SQL_RESULT_SET SET_PARAM - Set an Input/Output Parameter (variants for CLOB, BLOB, STRUCT, TABLE) NEXT, NEXT_PACKAGE Read next record in the resulting set, or next set of records for internal tables 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 52
How can my ABAP code benefit from SAP HANA? New features in ABAP 7.4 Usage of HANA-specific features DB-near programming SAP HANA content integration external views SP2 database procedure proxies SP2 HANA transport container SP2 Advanced ABAP database programming ABAP-managed procedures SP5 Fulltext index in DDIC SP2 advanced Open SQL SP5 advanced ABAP view building SP5* Transparent optimizations Fast Data Access (new data exchange protocol) >= SP5** optimized SELECT... INTO ITAB and SELECT SINGLE >= SP5** optimized FOR ALL ENTRIES-clause >= SP5** Benefits of In-Memory Architecture * might also support HANA-specific features in the future (> SP5) / ** >= SP5 means SP5 or later 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 53
Integrated development options across ABAP and HANA Consuming HANA views in ABAP SAP HANA offers advanced view modeling, e.g. Attribute views (join views) Analytic views (star schemas) Calculation views (modeled or coded via SQL script) With ABAP < 7.40 these views can be accessed lowlevel via ADBC. With ABAP 7.40 they are natively supported in ABAP Access possible via standard Open SQL Support for automatic client handling Mapping to DDIC types possible Consumption from ABAP Calculation view in SAP HANA 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 54
Integrated development options across ABAP and HANA Calling HANA database procedures from ABAP SAP HANA offers writing stored procedures in SQL Script a extension to SQL - for expressing data intensive application logic. With ABAP < 7.40 stored procedures can be called using ADBC, which requires Manual handling of transfer tables for input and output parameters via temporary tables or result views Manual mapping of database types to DDIC types Invocation from ABAP With ABAP 7.40 they are natively supported in ABAP Exporting/Importing parameters like for function modules (including mapping parameter to DDIC types) Stored procedure in SAP HANA 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 55
ABAP-managed Database Procedures Overview Basic idea: manage database procedures and their lifecycles from within the ABAP development infrastructure ABAP method is used as container for database procedures additionby DATABASE PROCEDURE indicates that a method is executed in the database additionfor db_platform indicates the database platform (currently supported HDB = SAP HANA database) Implementation of method body is done in SQLScript editing environment is the ABAP class editor embedded syntax check is available for SQLScript code (different from native SQL!) other languages might be supported in the future (for example R) ABAP server creates database procedure in database catalogue during generation runtime artifact is generated into schemasap<sid> (and not_sys_bic) 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 56
ABAP-managed Database Procedures Example CLASS zcd201_cl_snippet_amdp DEFINITION... INTERFACES if_amdp_marker_hdb. METHODS: determine_sales_volume IMPORTING VALUE(iv_client) TYPE mandt EXPORTING VALUE(et_sales_volume) TYPE tt_sales_volume. ENDCLASS. CLASS zcd201_cl_snippet_amdp IMPLEMENTATION. METHOD determine_sales_volume BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT USING snwd_so_i snwd_so_sl snwd_pd. -- here you use SQLScript ENDMETHOD. ENDCLASS. Marker interface method additions forward declaration of accessed database objects 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 57
Thank you Contact information: Rich Heilman rich.heilman@sap.com 2015 SAP SE or an SAP affiliate company. All rights reserved.
2015 SAP SE or an SAP affiliate company. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company. SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. Please see http://global12.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and notices. Some software products marketed by SAP SE and its distributors contain proprietary software components of other software vendors. National product specifications may vary. These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP SE or its affiliated companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP SE or SAP affiliate company products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty. In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any functionality mentioned therein. This document, or any related presentation, and SAP SE s or its affiliated companies strategy and possible future developments, products, and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forwardlooking statements are subject to various risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements, which speak only as of their dates, and they should not be relied upon in making purchasing decisions. 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 59