SQLScript Small Guide

Similar documents
Duet Enterprise: Tracing Reports in SAP, SCL, and SharePoint

Configuring relay server in Sybase Control Center

SAP AddOn Quantity Distribution. by Oliver Köhler, SAP Germany

Single Sign-on For SAP NetWeaver Mobile PDA Client

How to Find Suitable Enhancements in SAP Standard Applications

SAP NetWeaver Identity Management Identity Center Minimum System Requirements

BC100. Introduction to Programming with ABAP COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

How to Enable Single Sign-On for Mobile Devices?

SAP BusinessObjects Predictive Analysis 1.0 Supported Platforms

EWM125. Labor Management in SAP EWM COURSE OUTLINE. Course Version: 16 Course Duration: 4 Hours

BIT460. SAP Process Integration Message Mapping COURSE OUTLINE. Course Version: 15 Course Duration: 3 Day(s)

PLM210. Master Data Configuration in SAP Project System COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

AC507. Additional Functions of Product Cost Planning COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

How to Handle the System Message in SAP NetWeaver Mobile 7.1

Visual Composer Modeling: Data Validation in the UI

ADM950. Secure SAP System Management COURSE OUTLINE. Course Version: 10 Course Duration: 2 Day(s)

Quick View Insider Microblog: Why Is There No Inbox?

HA150 SQL Basics for SAP HANA

Enterprise Search Extension for SAP Master Data Governance

BC430 ABAP Dictionary

Duplicate Check and Fuzzy Search for Accounts and Contacts. Configuration with SAP NetWeaver Search and Classification (TREX) in SAP CRM WebClient UI

NET311. Advanced Web Dynpro for ABAP COURSE OUTLINE. Course Version: 10 Course Duration: 4 Day(s)

BC405 Programming ABAP Reports

DEV523 Customizing and Extending PowerDesigner

Visual Composer for SAP NetWeaver Composition Environment - Connectors

DS50. Managing Data Quality with SAP Information Steward COURSE OUTLINE. Course Version: 10 Course Duration: 2 Day(s)

How to reuse BRFplus Functions Similar to R/3 Function Modules using BRF+ Expression Type Function Call

Crystal Reports 2008 FixPack 2.4 Known Issues and Limitations

ADM900 SAP System Security Fundamentals

Visual Composer Modeling: Migrating Models from 7.1.X to 7.2.0

Installing SAP NetWeaver Mobile Client (eswt) on a Storage Card

BOCE20. SAP Crystal Reports for Enterprise: Advanced Report Design COURSE OUTLINE. Course Version: 15 Course Duration: 3 Day(s)

MDG100 Master Data Governance

BOC320. SAP Crystal Reports - Business Reporting and Report Processing Strategies COURSE OUTLINE. Course Version: 15 Course Duration: 3 Day(s)

ADM950. Secure SAP System Management COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

BC410. Programming User Dialogs with Classical Screens (Dynpros) COURSE OUTLINE. Course Version: 10 Course Duration: 3 Day(s)

BC400 Introduction to the ABAP Workbench

BC480 PDF-Based Print Forms

BC490 ABAP Performance Tuning

Using Default Values in Backend Adapter

Testing Your New Generated SAP NetWeaver Gateway Service

How to Set Up Data Sources for Crystal Reports Layouts in SAP Business One, Version for SAP HANA

BC400. ABAP Workbench Foundations COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day(s)

Quick View Insider: Understanding Quick View Configuration

ADM100 AS ABAP - Administration

BOC310. SAP Crystal Reports: Fundamentals of Report Design COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

Manual Activities of SAP Note Globalization Services, 2012/06/05

BC404. ABAP Programming in Eclipse COURSE OUTLINE. Course Version: 15 Course Duration: 3 Day(s)

Using JournalEntries and JournalVouchers Objects in SAP Business One 6.5

How to Download Software and Address Directories in SAP Service Marketplace

How to Guide to create Sample Application in IOS using SUP ODP 2.2

Message Alerting for SAP NetWeaver PI Advanced Adapter Engine Extended

BW Text Variables of Type Replacement Path

GRC100. GRC Principles and Harmonization COURSE OUTLINE. Course Version: 10 Course Duration: 2 Day(s)

TBIT44 PI Mapping and ccbpm

EDB785 SAP IQ Administration

How to Check or Derive an Attribute Value in MDG using BRFPlus

Quick View Insider: How Can I Change the Colors? (SNC 7.0)

Upgrade MS SQL 2005 to MS SQL 2008 (R2) for Non-High-Availability NW Mobile ABAP System

Remote Monitoring User for IBM DB2 for LUW

SAP Afaria Post- Installation Part 1

ADM960. SAP NetWeaver Application Server Security COURSE OUTLINE. Course Version: 10 Course Duration: 5 Day(s)

TBW30 SAP BW Modeling & Implementation

EDB358. System and Database Administration: Adaptive Server Enterprise COURSE OUTLINE. Course Version: 10 Course Duration: 5 Day(s)

EDB116. Fast Track to SAP Adaptive Server Enterprise COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day(s)

AFA461 SAP Afaria 7.0 System Administration (SP03)

LO Extraction - Part 6 Implementation Methodology

BW310. BW - Enterprise Data Warehousing COURSE OUTLINE. Course Version: 10 Course Duration: 5 Day(s)

ADM960. SAP NetWeaver Application Server Security COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day

ADM920 SAP Identity Management

EDB367. Powering Up with SAP Adaptative Server Enterprise 15.7 COURSE OUTLINE. Course Version: 10 Course Duration: 2 Day(s)

Visual Composer s Control Types

Working with Data Sources in the SAP Business One UI API

EP350. Innovated Content Management and Collaboration COURSE OUTLINE. Course Version: 10 Course Duration: 5 Day(s)

SMP541. SAP Mobile Platform 3.0 Native and Hybrid Application Development COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day(s)

SAP BusinessObjects Dashboards 4.0 SAP Crystal Dashboard Design 2011 SAP Crystal Presentation Design 2011

Quick View Insider: How Do I Set Quick View as SNC s Entry Screen?

How to Work with Analytical Portal

How to Integrate Microsoft Bing Maps into SAP EHS Management

Overview of Caffeine ABAP to Go

BC401. ABAP Objects COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day(s)

Building a Real-time Dashboard using Xcelsius and Data Integrator

Web Dynpro: Column Coloring in ALV

EP200. SAP NetWeaver Portal: System Administration COURSE OUTLINE. Course Version: 10 Course Duration: 5 Day(s)

SMP521. SAP Mobile Platform - Native and Hybrid Application Development COURSE OUTLINE. Course Version: 10 Course Duration: 5 Day(s)

EDB377. Fast Track to SAP Replication Server Administration COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day(s)

OData Service in the SAP Backend System for CRUDQ Operations in Purchase Order Scenario

Extending DME Transfer Files According to Spanish Banking Control Council to Support Non- Euro Payments

How to Integrate Google Maps into a Web Dynpro ABAP Application Using the Page Builder

SAP ME Build Tool 6.1

What are Specifics Concerning the Creation of New Master Data?

TBW60. BW: Operations and Performance COURSE OUTLINE. Course Version: 10 Course Duration: 5 Day(s)

Personalizing SAP BusinessObjects Explorer Information Spaces

TBIT40 SAP NetWeaver Process Integration

NET312. UI Development with Web Dynpro for ABAP COURSE OUTLINE. Course Version: 10 Course Duration: 4 Day(s)

Automatic Deletion of Similar/Identical Requests from InfoCube after Update

Architecture of the SAP NetWeaver Application Server

How can a Reference Query Be used?

How to do a Manual Kernel Upgrade of an SAP Server

What s New / Release Notes SAP Strategy Management 10.1

Transcription:

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