Applies to: HANA Summary SQLScript is a superset of the SQL language used to manage databases. This small guide aims to show SQLScript constructs by using two simple tables to help get a quick understanding of this new language. Author: Alvaro Tejada Galindo Company: SAP Labs Created on: 12 December 2011 Author Bio Alvaro joined SAP Labs after almost eleven years of doing ABAP development in both Peru and Canada. Besides ABAP, his main area of expertise is Scripting Languages, which he uses to develop integrations with SAP. He s an SAP Mentor Alumni, Top Contributor and SCN Moderator. 2011 SAP AG 1
Table of Contents Creating the tables... 3 Employees... 3 Roles... 3 Adding some information... 4 SQLScripting... 4 Select... 4 Select some fields... 5 Select with parameters... 5 Join... 6 Inner Join... 6 Left Outer Join... 7 Right Outer Join... 7 Projection... 8 Aggregation... 9 Union... 10 Vertical Union... 11 Copyright... 12 2011 SAP AG 2
Creating the tables In order to get a better understanding of how SQLScript works, we re going to build two simple tables that are going to be used for all the examples. Employees Roles Employees The Employees table will only contain, the Id, first and last name and the role number assigned to it. CREATE COLUMN TABLE EMPLOYEE ( ID INTEGER PRIMARY KEY, NAME VARCHAR(30), LAST_NAME VARCHAR(30), ROLE_ID INTEGER Roles The Roles table will have only two fields, one for the Id and one for the role description. CREATE COLUMN TABLE ROLE ( ROLE_ID INTEGER PRIMARY KEY, ROLE VARCHAR(30) 2011 SAP AG 3
Adding some information Now that we have our tables ready, it s time to add some data. Just replace P075400 with your own schema. insert into "P075400"."EMPLOYEE" values(001,'alvaro','tejada',001 insert into "P075400"."EMPLOYEE" values(002,'lennon','shimokawa',006 insert into "P075400"."EMPLOYEE" values(003,'raul','mosquera',002 insert into "P075400"."EMPLOYEE" values(004,'kiara','tejada',003 insert into "P075400"."EMPLOYEE" values(005,'pablo','cornejo',004 insert into "P075400"."EMPLOYEE" values(006,'welden','aguilar',007 insert into "P075400"."ROLE" values(001,'development Expert' insert into "P075400"."ROLE" values(002,'senior ABAP Developer' insert into "P075400"."ROLE" values(003,'python Developer' insert into "P075400"."ROLE" values(004,'r Developer' insert into "P075400"."ROLE" values(005,'ruby Developer' insert into "P075400"."ROLE" values(006,'java Developer' SQLScripting For the examples, we re going to create store procedures that are going to populate a buffer table called ProcView. This will allow us to fetch the information without having to fill up another tables or views. Select First statement that we must know, is the Select statement. This will selct everything from the table we want to fetch in. CREATE PROCEDURE ProcWithResultView(OUT EMPLOYEE_TAB EMPLOYEE) LANGUAGE SQLSCRIPT READS SQL DATA WITH RESULT ProcView AS EMPLOYEE_TAB = CE_COLUMN_TABLE( EMPLOYEE SELECT * FROM ProcView; 2011 SAP AG 4
Select some fields If we want to select only a couple of fields, we need to create a Table Type: CREATE TYPE EMPLOYEE_VIEW AS TABLE ( NAME VARCHAR(30), LAST_NAME VARCHAR(30) Now, we can create the procedure again (If you have create the procedure, then comment out the first two lines): CREATE PROCEDURE ProcWithResultView(OUT EMPLOYEE_TAB EMPLOYEE_VIEW) EMPLOYEE_TAB = CE_COLUMN_TABLE( EMPLOYEE,[ NAME, LAST_NAME ] SELECT * FROM ProcView; Select with parameters It s easy to use parameters when selecting data. CREATE PROCEDURE ProcWithResultView(IN last_name VARCHAR(30), OUT EMPLOYEE_TAB EMPLOYEE_VIEW) EMPLOYEE_TAB = SELECT NAME, LAST_NAME FROM EMPLOYEE WHERE LAST_NAME = :last_name; ( placeholder =( $$last_name$$, Tejada 2011 SAP AG 5
Join A very common statement, is the JOIN which allows us to fetch data from two or more tables. We have three types of joins, INNER JOIN, LEFT OUTER JOIN and RIGHT OUTER JOIN. For this set of examples, we need to create a Table Type first. CREATE TYPE EMPLOYEE_ROLE AS TABLE ( NAME VARCHAR(30), LAST_NAME VARCHAR(30), ROLE_ID INTEGER, ROLE VARCHAR(30), You may have realized that we are including the ROLE_ID, which the common field between the two tables. We re doing this because it s a requisite when doing JOIN on SQLScript. Inner Join This is the dafult join, which fetches data from two or more tables by using a set of common fields. CREATE PROCEDURE ProcWithResultView(IN lt_employee EMPLOYEE,IN lt_role ROLE, OUT EMPLOYEE_ROLE EMPLOYEE_ROLE) EMPLOYEE_ROLE = CE_JOIN(:lt_employee,:lt_role,[ ROLE_ID ],[ NAME, LAST_NAME, ROLE_ID, ROLE ] ( placeholder =( $$lt_employee$$, EMPLOYEE ), placeholder =( $$lt_role$$, ROLE ) As you may have noticed, we re passing the JOIN tables as parameters, as in SQLScript we can t call the tables directly. 2011 SAP AG 6
Left Outer Join The LEFT OUTER JOIN will take data from the left table, even when it doesn t exist on the right table. CREATE PROCEDURE ProcWithResultView(IN lt_employee EMPLOYEE,IN lt_role ROLE, OUT EMPLOYEE_ROLE EMPLOYEE_ROLE) EMPLOYEE_ROLE = CE_LEFT_OUTER_JOIN(:lt_employee,:lt_role,[ ROLE_ID ],[ NAME, LAST_NAME, ROLE_ID, ROLE ] ( placeholder =( $$lt_employee$$, EMPLOYEE ), placeholder =( $$lt_role$$, ROLE ) Right Outer Join The RIGHT OUTER JOIN will take data from the right table, even when it doesn t exist on the left table. CREATE PROCEDURE ProcWithResultView(IN lt_employee EMPLOYEE,IN lt_role ROLE, OUT EMPLOYEE_ROLE EMPLOYEE_ROLE) EMPLOYEE_ROLE = CE_RIGHT_OUTER_JOIN(:lt_employee,:lt_role,[ ROLE_ID ],[ NAME, LAST_NAME, ROLE_ID, ROLE ] ( placeholder =( $$lt_employee$$, EMPLOYEE ), placeholder =( $$lt_role$$, ROLE ) 2011 SAP AG 7
Projection A projection is like a Select, but allows us to use parameters and also change the name of the field we re fetching. CREATE PROCEDURE ProcWithResultView(IN lt_employee EMPLOYEE, OUT EMPLOYEE_ROLE EMPLOYEE) EMPLOYEE_ROLE = CE_PROJECTION(:lt_employee,[ ID, NAME, LAST_NAME, ROLE_ID ], ID > 3 ( placeholder =( $$lt_employee$$, EMPLOYEE ) Let s create a new Table Type, to change the name of the fields we re fetching. --DROP TYPE EMPLOYEE_VIEW CREATE TYPE EMPLOYEE_VIEW AS TABLE ( FIRST_NAME VARCHAR(30), LAST_NAME VARCHAR(30) 2011 SAP AG 8
Then, we can create our procedure: CREATE PROCEDURE ProcWithResultView(IN lt_employee EMPLOYEE,IN lt_role ROLE, OUT EMPLOYEE_ROLE EMPLOYEE_ROLE) EMPLOYEE_ROLE = CE_PROJECTION(:lt_employee,[ NAME AS FIRST_NAME, LAST_NAME ] ( placeholder =( $$lt_employee$$, EMPLOYEE ) Aggregation Allows us to perform aggregations like COUNT, SUM or MAX. --DROP TYPE EMPLOYEE_VIEW CREATE TYPE EMPLOYEE_VIEW AS TABLE ( NUM_EMPLOYEES INTEGER, After we created the Table Type, we can create the procedure. CREATE PROCEDURE ProcWithResultView(IN lt_employee EMPLOYEE, OUT EMPLOYEE_ROLE EMPLOYEE_ROLE) EMPLOYEE_ROLE = CE_AGGREGATION(:lt_employee,[COUNT( NAME ) AS NUM_EMPLOYEES ] ( placeholder =( $$lt_employee$$, EMPLOYEE ) 2011 SAP AG 9
Union Allows us to mix two tables that share the same structure. For this, let s create a new table: CREATE COLUMN TABLE NEW_EMPLOYEE ( ID INTEGER PRIMARY KEY, NAME VARCHAR(30), LAST_NAME VARCHAR(30), ROLE_ID INTEGER And fill it with the following data: insert into "P075400"."EMPLOYEE" values(001,'milly','castro',001 insert into "P075400"."EMPLOYEE" values(002,'marcelo','ramos',002 insert into "P075400"."EMPLOYEE" values(003,'cheko','trepo',006 With that, we can proceed and create the procedure: CREATE PROCEDURE ProcWithResultView(IN lt_employee EMPLOYEE, IN lt_new_employee NEW_EMPLOYEE, OUT EMPLOYEE_VIEW EMPLOYEE) EMPLOYEE_VIEW = CE_UNION_ALL(:lt_employee,:lt_new_employee ( placeholder =( $$lt_employee$$, EMPLOYEE ), ( placeholder =( $$lt_new_employee$$, NEW_EMPLOYEE ) 2011 SAP AG 10
Vertical Union The Verticular Union allows us to mix two tables, choosing the fields from each table. CREATE PROCEDURE ProcWithResultView(IN lt_employee EMPLOYEE, IN lt_new_employee NEW_EMPLOYEE, OUT EMPLOYEE_VIEW EMPLOYEE) EMPLOYEE_VIEW = CE_VERTICAL_UNION(:lt_employee,[ ID, NAME ],:lt_new_employee,[ LAST_NAME, ROLE_ID ] ( placeholder =( $$lt_employee$$, EMPLOYEE ), ( placeholder =( $$lt_new_employee$$, NEW_EMPLOYEE ) 2011 SAP AG 11
Copyright Copyright 2012 SAP AG. 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 AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iseries, pseries, xseries, zseries, eserver, z/vm, z/os, i5/os, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Oracle Corporation. JavaScript is a registered trademark of Oracle Corporation, used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group 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. 2011 SAP AG 12