Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Real Time Analytics with SAP Hana

You're reading from   Real Time Analytics with SAP Hana Enhance your SAP HANA skills using this step-by-step guide to creating and reporting data models for real-time analytics

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781782174110
Length 226 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Vinay Singh Vinay Singh
Author Profile Icon Vinay Singh
Vinay Singh
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Kickoff – Before We Start 2. SAP HANA Data Modeling Approach FREE CHAPTER 3. Different Ways of SAP HANA Data Load 4. Creating SAP HANA Artifacts Attribute Views and Analytical Views 5. Creating SAP HANA Artifacts – Analytical Privileges and Calculation Views 6. Understanding Text Search and Hierarchies in SAP HANA 7. Using Decision Tables and Transporting SAP HANA Content 8. Consuming SAP HANA Data Models 9. An Introduction to Application Function Library Index

Procedures

Procedures are reusable processing blocks that are implemented using the SQLscript, which describes a sequence of operations on data passed as input and database tables. It can be created as read-only (without side-effects) or read-write (with side-effects).

Procedures can have multiple input parameters and output parameters (can be scalar or table types).

There are three different ways to create a procedure in HANA:

  • Using the SQL editor (in SAP HANA Studio)
  • Using the Modeler wizard in the modeler perspective (in SAP HANA Studio)
  • Using the SAP HANA XS project in the SAP HANA Development perspective (in SAP HANA Studio), which isn't discussed in this chapter

Creating with the SQL editor (in SAP HANA Studio)

The following syntax is used to create procedure via the SQL editor:

CREATE PROCEDURE {schema.}name 
            {({IN|OUT|INOUT} 
                        param_name data_type {,...})} 
            {LANGUAGE <LANG>} {SQL SECURITY <MODE>} 
            {READS SQL DATA {WITH RESULT VIEW <view_name>}} AS 
BEGIN 
... 
END

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

The parameters are for:

  • Reads SQL Data: This defines a procedure as read-only.
  • Language: This specifies the implementation. SQLscript is the default language.
  • With result view: This is used to create a column view for the output parameter of the type table.

Let's create a procedure where we will pass discount as the input parameter and get the sales report as the output parameter. We use the same tables that we created previously:

CREATE PROCEDURE HANA_DEMO."PROC_EU_SALES_REPORT"(
            IN DISCOUNT INTEGER,
            OUT OUTPUT_TABLE HANA_DEMO."EU_SALES" )
LANGUAGE SQLSCRIPT SQL SECURITY INVOKER AS
/*********BEGIN PROCEDURE SCRIPT ************/
BEGIN
Pvar1 = SELECT T1.REGION_NAME, T1.SUB_AREA, T2.PRODUCT_KEY, T2.AMOUNT_SOLD
            FROM HANA_DEMO.REGION AS T1
            INNER JOIN
            HANA_DEMO.SALES_FACT AS T2
            ON T1.REGION_KEY = T2.REGION_KEY;

Pvar2 = SELECT T1.REGION_NAME, T1.SUB_AREA, T1.PRODUCT_KEY, T1.AMOUNT_SOLD, T2.PRODUCT_NAME
            FROM :Pvar1 AS T1
            INNER JOIN
            HANA_DEMO.PRODUCT AS T2
            ON T1.PRODUCT_KEY = T2.PRODUCT_KEY;

OUTPUT_TABLE = SELECT SUM(AMOUNT_SOLD) AS AMOUNT_SOLD, SUM(AMOUNT_SOLD - (AMOUNT_SOLD * :DISCOUNT/ 100)) AS NET_AMOUNT,
            PRODUCT_NAME, REGION_NAME, SUB_AREA
            FROM :Pvar2 
            GROUP BY PRODUCT_NAME, REGION_NAME, SUB_AREA;
END;

We can call the previously created procedure with the following CALL statement:

CALL HANA_DEMO."PROC_SALES_REPORT" (8, null);

You can see the created procedure below our schema under the Procedure... folder.

Creating with the SQL editor (in SAP HANA Studio)

Procedure creation using the wizard

Choose the package in which you want to create the procedure and right-click on it.

A new screen will pop up; fill in the details and click on Confirm:

Procedure creation using the wizard

The SQL console opens with default syntax; we need to put our logic in between BEGIN and END.

The following is a sample logic with which I am creating the Procedure:

Procedure creation using the wizard

On the left-hand side of the screen, you can see the output pane:

Procedure creation using the wizard

Click on it and select New…:

Procedure creation using the wizard

Define the columns which we used in the preceding procedure:

Procedure creation using the wizard

Similarly, perform the same steps for input parameters as well:

Procedure creation using the wizard

Now the procedure is ready to be called via the CALL statement.

Once we build our concept about different views, then one question that will definitely come to our mind is, should we use calculation views (not yet discussed) or procedures. We will discuss this once we have discussed the calculation view in Chapter 5, Creating SAP HANA Artifacts – Analytical Privileges and Calculation Views.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime