You are on page 1of 32

DEV360 – Using Advanced Features of

SQLScript

Public
Speakers

Las Vegas, Oct 19 - 23 Barcelona, Nov 10 - 12

 Rich Heilman, SAP Labs, LLC.  Rich Heilman, SAP Labs, LLC.
 Katharina Schell, SAP SE  Katharina Schell, SAP SE
 Andreas Bader, SAP SE  Andreas Bader, SAP SE

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 2


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
Editor & Debugger
SQLScript Language Features
Exercise

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 4


Overview

Public
Overview
What? Why?

SQLScript is Two reasons why this is required to achieve the


 An interface for applications to access SAP best performance:
HANA  Eliminate data transfer between database &
 Extension of ANSI Standard SQL application tiers
 Calculations need to be executed in the
 Language for creating stored procedures and
database layer to get the maximum benefit
user defined functions in HANA
from SAP HANA features such as fast column
 Combines set based declarative SQL with operations, query optimization and parallel
imperative control flow constructs thus being execution.
suitable for mass data processing and OLTP
scenarios as well.

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 6


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

 Complex SQL statement can be well structured using table variables improving maintainability and
gaining performance by parallel execution based on statement dependencies. 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 7


Overview
Application Development Technologies

Front-end Technologies Client: Browser or Mobile


 http/s
 HTML5 / SAPUI5
Presentation logic
 Client-side JavaScript

Control Flow Technologies


 OData
SAP HANA
 Server-Side JavaScript XS Control flow logic
 XMLA

Data Processing Technologies


 SQL / SqlScript
Data Calculation logic
 Application Function Library (AFL)

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 8


Overview
Traditional Programming Model vs. New Programming Model

Traditional: “Data to Code” New Model: “Code to Data”

Application Layer Application Layer


Code
Transfer
Massive data Minimum
copies creates
bottleneck Result Set

DB Layer DB Layer
Code

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 9


Overview
Parallel Processing
BEGIN
...
Input
-- 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';
Q1 Q5
-- 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";
Q2
-- Query 3
out_notebook_count = select count(*) as cnt from
:product_texts where "Category" = 'Notebooks';

-- Query 4
out_pc_count = select count(*) as cnt from Q3 Q4
:product_texts where "Category" = 'PC';

-- Query 5
out_total_count = select count(*) as cnt
from "SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::products“;
...
Notebooks PCs Total
END;

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 10


Editor & Debugger

Public
Editor & Debugger
Editor Integration in the Web-Based Development Workbench
 Full semantic code completion
 Code snippets
 Syntax highlighting
 .hdbprocedure file format only

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 12


Editor & Debugger
Debugging in the Web-based Development Workbench
 Set breakpoints in the
run-time object in the
catalog or the design-
time artifact in the
repository
 Use the “Invoke
Procedure” button to
execute and debug the
procedure.
 Resume & step over
functions
 Scalar & table
variable/parameter
evaluations

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 13


SQLScript Language Features

Public
SQLScript Language Features
Declarative Logic

 Allows the developer to declare the data selection via SELECT statements
 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 15


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

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 16


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 17


SQLScript Language Features
Dynamic Filtering

 Allows developers to apply a


dynamic WHERE clause to
SELECT statements by using
the APPLY_FILTER statement
 Both database tables and
intermediate table variables
are supported

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 18


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 19


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 20


SQLScript Language Features
Exception Handling

 Signaling and catching user-defined conditions

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 21


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 22


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.
 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 23


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 24


SQLScript Language Features
Commit/Rollback

 Supported in procedures only, not supported for scalar or table UDFs


 COMMIT command commits the current transaction and all changes before the COMMIT
command
 ROLLBACK command rolls back the current transaction and undoes all changes since the last
COMMIT
 Transaction boundary is not tied to the procedure block, so if there are nested procedures that
contain COMMIT/ROLLBACK then all statements in the top-level procedure are affected.
 If dynamic SQL was used in the past to execute COMMIT and ROLLBACK statements, it is
recommended to replace all occurrences with the native command because they are more
secure.

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 25


SQLScript Language Features
Commit/Rollback

 The first and third INSERT


statements affect the productLog
table, but not the second since it
was rolled back.
 With the first COMMIT, the first
transaction is written to
persistence, and a new transaction
is started
 By triggering ROLLBACK, all
changes in the second transaction
are reverted, and a new
transaction is started

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 26


Exercises

Public
SAP TechEd Online
Continue your SAP TechEd education after the event!

SAP TechEd Online


 Access replays of keynotes, Demo Jam, SAP TechEd live interviews, select lecture sessions, and more!
 Hands-on replays

http://sapteched.com/online

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 28


Further Information

Related SAP TechEd sessions:


DEV161 – Introduction to SQLScript
DEV601 - CodeJam - Exploring Advanced SQLScript Concepts
DEV603 – CodeJam - Getting Started with SQLScript
SAP Public Web
scn.sap.com
www.sap.com
SAP Education and Certification Opportunities
www.sap.com/education
Watch SAP TechEd Online
www.sapteched.com/online

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 29


Feedback
Please complete your session evaluation for
DEV360

©©2015
2015
SAP
SAP
SESE
oror
anan
SAP
SAP
affiliate
affiliate
company.
company.
AllAll
rights
rights
reserved.
reserved. Public 3030
Thank you

Contact information:

Rich Heilman, SAP Labs, LLC


SAP HANA Product Management
rich.heilman@sap.com

© 2015 SAP SE or an SAP affiliate company. All rights reserved. Public 31


© 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 forward-
looking 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 32