Top Banner
SAP COMMUNITY NETWORK scn.sap.com © 2012 SAP AG 1 HANA Stored Procedures to Create Universe Derived Tables Applies to: SAP BusinessObjects BI Solutions 4.0, the new dimensional metadata modeling tool called, information design tool. It can also apply on universe design tool delivered with SAP BusinessObjects BI Solutions 4.0 and SAP BusinessObjects BI Solutions XI 3.1 Summary This document provides a definition and description of some HANA stored procedures which can be used to create derived tables in a Universe data foundation. Those stored procedures will provide the SQL definition of Universe derived tables which + Connect to HANA views defined with input parameters + Retrieve the HANA views dimensions and measures with the correct aggregations and captions. Those stored procedures can be used to bootstrap the definition of a Universe based on HANA views which contain input parameters. The target audience is users who have at good understanding of the SAP HANA technology and of the SAP BusinessObjects semantic layer (universes). Links to the available documentation is provided at in the Related Links section. Author: Didier Mazoué Company: SAP Created on: August 1 st , 2012 Authors Bio Didier Mazoué is an expert product manager in the semantic layer team at SAP Business Objects Enterprise Information Management. Didier is the expert on most major OLAP databases, SAP Netweaver BW, HANA and relational databases. Before joining the product group in 2005 Didier was a strategic pre-sales field representative specializing in closing large database deals. He currently resides in France and works out of the SAP Lab in Levallois-Perret.
19

HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

May 27, 2018

Download

Documents

phamnhi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 1

HANA Stored Procedures to Create

Universe Derived Tables

Applies to:

SAP BusinessObjects BI Solutions 4.0, the new dimensional metadata modeling tool called, information

design tool. It can also apply on universe design tool delivered with SAP BusinessObjects BI Solutions 4.0

and SAP BusinessObjects BI Solutions XI 3.1

Summary

This document provides a definition and description of some HANA stored procedures which can be used to

create derived tables in a Universe data foundation.

Those stored procedures will provide the SQL definition of Universe derived tables which

+ Connect to HANA views defined with input parameters

+ Retrieve the HANA views dimensions and measures with the correct aggregations and captions. Those

stored procedures can be used to bootstrap the definition of a Universe based on HANA views which contain

input parameters.

The target audience is users who have at good understanding of the SAP HANA technology and of the SAP

BusinessObjects semantic layer (universes).

Links to the available documentation is provided at in the Related Links section.

Author: Didier Mazoué

Company: SAP

Created on: August 1st, 2012

Authors Bio

Didier Mazoué is an expert product manager in the semantic layer team at SAP Business

Objects Enterprise Information Management. Didier is the expert on most major OLAP

databases, SAP Netweaver BW, HANA and relational databases. Before joining the product

group in 2005 Didier was a strategic pre-sales field representative specializing in closing

large database deals. He currently resides in France and works out of the SAP Lab in

Levallois-Perret.

Page 2: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 2

Table of Contents

1. Stored procedures objectives ................................................................................................................ 3

Role of the Stored Procedures ........................................................................................................................ 3

Where to use the results of the Stored Procedures ........................................................................................ 3

Data Foundation example ............................................................................................................................... 4

2. The stored procedures definition ........................................................................................................... 7

Table “SL_DERIVED_TABLE” ........................................................................................................................ 7

Stored Procedure “DERIVED_TABLE_ALL”................................................................................................... 7

Stored Procedure “DERIVED_TABLE_SINGLE” .......................................................................................... 13

Use the stored procedures ............................................................................................................................ 17

Related Content ................................................................................................................................................ 18

Copyright........................................................................................................................................................... 19

Page 3: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 3

1. Stored procedures objectives

Role of the Stored Procedures

The stored procedures provided in this document analyze the HANA views and write data in the HANA table called “SL_DERIVED_TABLE”.

The stored procedure called “DERIVED_TABLE_ALL” analyzes all HANA views.

The stored procedure called “DERIVED_TABLE_SINGLE” analyzes the HANA view passed in parameter.

They are both writing the result in the same table

The table “SL_DERIVED_TABLE” has 2 columns:

CUBE_NAME

SQL_SCRIPT

The “CUBE_NAME” column contains the HANA view name as it has been deployed in _SYS_BIC schema like

"_SYS_BIC"."foodmart/SALES_2006”.

The “SQL_SCRIPT” column contains the SELECT statement that can be copied and paste in a derived table definition.

The derived table can be created in a Data Foundation designed with Information Design Tool.

The stored procedures will:

Detect the variables and input parameters,

Detect the attributes and measures

Create the appropriate SQL aggregation function for the measures

Generate the attribute caption and measure caption as column name

Generate the PLACEHOLDER statement for mandatory input parameters

Generate an @Prompt universe function as PLACEHOLDER value with the appropriate data type

Where to use the results of the Stored Procedures

The goal is to create a SQL statement that picks all columns of the HANA views by taking into account attributes and measures as well as input parameters. This SQL statement will be written as a text field in a HANA table.

The SQL statement can then be copied and pasted in a universe derived table definition. The derived table is a SQL script defined in the data foundation of a universe.

The universe is authored in Information Design Tool. Universes are part of the Semantic Layer and used by BI clients such as Web Intelligence, Crystal Reports, Dashboards or Explorer.

This will lower the TCO of creating a universe on top of a SAP HANA view and also take into account the HANA variables.

Page 4: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 4

Data Foundation example

The derived table editor:

Example of generated SQL script to be copied in the derived table definition: SELECT

Sum("AMOUNT_CURRENCY") AS "Amount currency",

Sum("AMOUNT") AS "Amount",

"ACCOUNT_DESCRIPTION" AS "Account",

"ACCOUNT_TYPE" AS "Account type",

"ACCOUNT_PARENT" AS "Account parent",

"ACCOUNT_ID" AS "Account Id",

"CATEGORY_DESCRIPTION" AS "Category",

"CATEGORY_PARENT" AS "Category Parent",

"CATEGORY_ID" AS "Category Id",

"CURRENCY_DATE" AS "Currency date",

"CURRENCY" AS "Currency",

"CONVERSION_RATIO" AS "Conversion ratio",

"CURRENCY_ID" AS "Currency Id",

"TYPE" AS "Store Type",

"STORE" AS "Store",

"CITY" AS "Store City",

"DISTRICT" AS "Sales District",

"STATE" AS "Store State",

"COUNTRY" AS "Store Country",

"MANAGER" AS "Manager",

Page 5: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 5

"SQFT" AS "Square Feet",

"WEEK" AS "Week",

"DATE" AS "Date",

"MONTH" AS "Month",

"QUARTER" AS "Quarter",

"YEAR" AS "Year"

from "_SYS_BIC"."foodmart/EXPENSES_CURR_CONV_VAR" ('PLACEHOLDER'

= ('$$CURRENCY_VAR$$', '@Prompt('CURRENCY_VAR','N',,mono,free)' ))

GROUP BY

"ACCOUNT_DESCRIPTION",

"ACCOUNT_TYPE",

"ACCOUNT_PARENT",

"ACCOUNT_ID",

"CATEGORY_DESCRIPTION",

"CATEGORY_PARENT",

"CATEGORY_ID",

"CURRENCY_DATE",

"CURRENCY",

"CONVERSION_RATIO",

"CURRENCY_ID",

"TYPE",

"STORE",

"CITY",

"DISTRICT",

"STATE",

"COUNTRY",

"MANAGER",

"SQFT",

"WEEK",

"DATE",

"MONTH",

"QUARTER",

"YEAR"

Result of a derived table execution:

Page 6: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 6

Page 7: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 7

2. The stored procedures definition

The following SQL scripts detail the different stored procedures and the table where the results of the stored procedures will be written.

By default, the table and stored procedures will be created in the default catalog attached to the HANA user.

The following scripts can be used in the HANA Studio to define the necessary table and stored procedures.

Table “SL_DERIVED_TABLE”

(this table will host the SQL sentence to copy and paste into the universe derived table)

DROP TABLE SL_DERIVED_TABLE;

CREATE TABLE SL_DERIVED_TABLE(

CUBE_NAME VARCHAR(255),

SQL_SCRIPT VARCHAR(5000)

);

Stored Procedure “DERIVED_TABLE_ALL”

(this commands prepares HANA to host the stored procedure which analyzes all HANA views)

DROP PROCEDURE DERIVED_TABLE_ALL;

SQL columns with attributes caption and measures caption (this stored procedure will define the sentence for a universe derived table using the caption of both attributes and measures as column names)

CREATE PROCEDURE DERIVED_TABLE_ALL

LANGUAGE SQLSCRIPT AS

COLUMN_OBJECT VARCHAR(255) := '';

COLUMN_NAME VARCHAR(255) := '';

COLUMN_CAPTION VARCHAR(255) := '';

COLUMN_TYPE_D VARCHAR(255) := '';

MEASURE_AGGREGATOR_DESC VARCHAR(255) := '';

VIEW_TYPE VARCHAR(255) := '';

VARIABLE_NAME VARCHAR(255) := '';

DESCRIPTION VARCHAR(255) := '';

VAR_CUBE_NAME VARCHAR(255) := '';

SQL_SCRIPT VARCHAR(5000) := 'SELECT ';

GROUP_BY VARCHAR(5000) := ' GROUP BY ';

PLACEHOLDER VARCHAR(5000) := '';

FIRST_ROW INT := 1;

NB_ROW INT := 0;

CURSOR c_cubes FOR

SELECT

"COLUMN_OBJECT"

FROM "_SYS_BI"."BIMC_CUBES";

CURSOR c_variables (the_cube VARCHAR(255)) FOR

SELECT

"VARIABLE_NAME",

"DESCRIPTION",

"COLUMN_TYPE_D"

FROM "_SYS_BI"."BIMC_VARIABLE"

WHERE

(

Page 8: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 8

'"_SYS_BIC"."' || "CATALOG_NAME" || '/' || "CUBE_NAME" || '"' = :the_cube

AND "MANDATORY" = 1

);

CURSOR c_columns (the_cube VARCHAR(255)) FOR

SELECT DISTINCT

View__25."COLUMN_NAME",

View__25."COLUMN_CAPTION",

View__25."MEASURE_AGGREGATOR_DESC",

Table__19."VIEW_TYPE"

FROM

(SELECT "CATALOG_NAME", "COLUMN_FLAG",

(CASE

WHEN "ALTERNATIVE_PROPERTY_NAME" <> '' THEN "ALTERNATIVE_PROPERTY_NAME"

ELSE "COLUMN_NAME"

END) AS "COLUMN_NAME",

"CUBE_NAME", "DIMENSION_UNIQUE_NAME", "DESCRIPTION" AS "COLUMN_CAPTION",

(CASE "MEASURE_AGGREGATOR"

WHEN 1 THEN 'Sum'

WHEN 2 THEN 'Count'

WHEN 3 THEN 'Min'

WHEN 4 THEN 'Max'

WHEN 5 THEN 'Average'

WHEN 6 THEN 'Variance'

ELSE 'Unknown'

END) AS "MEASURE_AGGREGATOR_DESC", "SCHEMA_NAME"

FROM "_SYS_BI"."BIMC_PROPERTIES") View__25

INNER JOIN "_SYS_BI"."BIMC_DIMENSIONS" Table__3 ON

(Table__3."CATALOG_NAME"=View__25."CATALOG_NAME" AND

Table__3."SCHEMA_NAME"=View__25."SCHEMA_NAME" AND

Table__3."CUBE_NAME"=View__25."CUBE_NAME" AND

Table__3."DIMENSION_UNIQUE_NAME"=View__25."DIMENSION_UNIQUE_NAME")

INNER JOIN "_SYS_BI"."BIMC_CUBES" Table__2 ON

(Table__2."CATALOG_NAME"=Table__3."CATALOG_NAME" AND

Table__2."SCHEMA_NAME"=Table__3."SCHEMA_NAME" AND

Table__2."CUBE_NAME"=Table__3."CUBE_NAME")

INNER JOIN (SELECT (substr("OBJECT_NAME",1, instr("OBJECT_NAME", '/')-

1)) AS "CATALOG_NAME", (substr("OBJECT_NAME",instr("OBJECT_NAME", '/')+1)) AS

"CUBE_NAME", "OBJECT_OID", "SCHEMA_NAME"

FROM "SYS"."OBJECTS") View__26

ON (Table__2."CATALOG_NAME"=View__26."CATALOG_NAME" AND

Table__2."SCHEMA_NAME"=View__26."SCHEMA_NAME" AND

Table__2."CUBE_NAME"=View__26."CUBE_NAME")

INNER JOIN "SYS"."VIEWS" Table__19 ON

(View__26."OBJECT_OID"=Table__19."VIEW_OID")

WHERE

(

Table__2."COLUMN_OBJECT" = :the_cube

AND

View__25."COLUMN_FLAG" = 'User Attribute'

);

BEGIN

/* Fetch cubes */

for row_cubes AS c_cubes DO

var_cube_name := row_cubes.column_object;

sql_script := 'SELECT ';

group_by := ' GROUP BY ';

placeholder := '';

first_row := 1;

/* Fetch columns */

for row_columns AS c_columns(:var_cube_name) DO

if :first_row > 1 then

sql_script := LEFT(sql_script || ', ', 5000);

end if;

sql_script := LEFT(sql_script || char(13) || char(10), 5000);

Page 9: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 9

if row_columns.measure_aggregator_desc <> 'Unknown' then /* This is a

measure */

sql_script := LEFT(sql_script ||

row_columns.measure_aggregator_desc || '(' || '"' || row_columns.column_name ||

'"' || ')', 5000);

if row_columns.column_caption <> '' then

sql_script := LEFT(sql_script || ' AS "' ||

Replace(row_columns.column_caption, '"', '""') || '"', 5000);

end if;

else

sql_script := LEFT(sql_script || '"' ||

row_columns.column_name || '"', 5000);

if row_columns.column_caption <> '' then

sql_script := LEFT(sql_script || ' AS "' ||

Replace(row_columns.column_caption, '"', '""') || '"', 5000);

end if;

if group_by <> ' GROUP BY ' then

group_by := LEFT(group_by || ', ', 5000);

end if;

group_by := LEFT(group_by || char(13) || char(10) || '"' ||

row_columns.column_name || '"', 5000);

end if;

first_row := first_row + 1;

end for;

/* Fetch variables */

first_row := 1;

for row_variables AS c_variables (:var_cube_name) DO

if placeholder = '' then

placeholder := ' (';

end if;

if first_row > 1 then

placeholder := LEFT(placeholder || ',', 5000);

end if;

placeholder := LEFT(placeholder || '''PLACEHOLDER'' = (''$$' ||

row_variables.variable_name || '$$'', ', 5000);

if row_variables.column_type_d = 'INTEGER' THEN

placeholder := LEFT(placeholder || '''@Prompt(''' ||

row_variables.description || ''',''N'',,mono,free)'' )', 5000);

else

placeholder := LEFT(placeholder || '@Prompt(''' ||

row_variables.description || ''',''A'',,mono,free) )', 5000);

end if;

first_row := first_row + 1;

end for;

if placeholder <> '' then

placeholder := LEFT(placeholder || ')', 5000);

end if;

/* Generate SQL script */

sql_script := LEFT(sql_script || char(13) || char(10) || ' from ' ||

:var_cube_name || :placeholder, 5000);

if group_by <> ' GROUP BY ' then

sql_script := LEFT(sql_script || char(13) || char(10) || :group_by,

5000);

end if;

SELECT COUNT(*) INTO nb_row FROM SL_DERIVED_TABLE WHERE CUBE_NAME =

:var_cube_name;

if :nb_row >= 1 then

DELETE FROM SL_DERIVED_TABLE WHERE CUBE_NAME = :var_cube_name;

end if;

INSERT INTO SL_DERIVED_TABLE VALUES (:var_cube_name, :sql_script);

end for;

END;

Page 10: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 10

SQL columns with attributes name and measures name (this stored procedure will define the sentence for a universe derived table using the technical name of both attributes and measures as column names; it can be used as an alternative to the previous stored procedure)

CREATE PROCEDURE DERIVED_TABLE_ALL

LANGUAGE SQLSCRIPT AS

COLUMN_OBJECT VARCHAR(255) := '';

COLUMN_NAME VARCHAR(255) := '';

COLUMN_TYPE_D VARCHAR(255) := '';

MEASURE_AGGREGATOR_DESC VARCHAR(255) := '';

VIEW_TYPE VARCHAR(255) := '';

VARIABLE_NAME VARCHAR(255) := '';

DESCRIPTION VARCHAR(255) := '';

VAR_CUBE_NAME VARCHAR(255) := '';

SQL_SCRIPT VARCHAR(5000) := 'SELECT ';

GROUP_BY VARCHAR(5000) := ' GROUP BY ';

PLACEHOLDER VARCHAR(5000) := '';

FIRST_ROW INT := 1;

NB_ROW INT := 0;

CURSOR c_cubes FOR

SELECT

"COLUMN_OBJECT"

FROM "_SYS_BI"."BIMC_CUBES";

CURSOR c_variables (the_cube VARCHAR(255)) FOR

SELECT

"VARIABLE_NAME",

"DESCRIPTION",

"COLUMN_TYPE_D"

FROM "_SYS_BI"."BIMC_VARIABLE"

WHERE

(

'"_SYS_BIC"."' || "CATALOG_NAME" || '/' || "CUBE_NAME" || '"' = :the_cube

AND "MANDATORY" = 1

);

CURSOR c_columns (the_cube VARCHAR(255)) FOR

SELECT DISTINCT

View__25."COLUMN_NAME",

View__25."MEASURE_AGGREGATOR_DESC",

Table__19."VIEW_TYPE"

FROM

(SELECT "CATALOG_NAME", "COLUMN_FLAG",

(CASE

WHEN "ALTERNATIVE_PROPERTY_NAME" <> '' THEN "ALTERNATIVE_PROPERTY_NAME"

ELSE "COLUMN_NAME"

END) AS "COLUMN_NAME",

"CUBE_NAME", "DIMENSION_UNIQUE_NAME",

(CASE "MEASURE_AGGREGATOR"

WHEN 1 THEN 'Sum'

WHEN 2 THEN 'Count'

WHEN 3 THEN 'Min'

WHEN 4 THEN 'Max'

WHEN 5 THEN 'Average'

WHEN 6 THEN 'Variance'

ELSE 'Unknown'

END) AS "MEASURE_AGGREGATOR_DESC", "SCHEMA_NAME"

FROM "_SYS_BI"."BIMC_PROPERTIES") View__25

INNER JOIN "_SYS_BI"."BIMC_DIMENSIONS" Table__3 ON

(Table__3."CATALOG_NAME"=View__25."CATALOG_NAME" AND

Table__3."SCHEMA_NAME"=View__25."SCHEMA_NAME" AND

Table__3."CUBE_NAME"=View__25."CUBE_NAME" AND

Table__3."DIMENSION_UNIQUE_NAME"=View__25."DIMENSION_UNIQUE_NAME")

Page 11: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 11

INNER JOIN "_SYS_BI"."BIMC_CUBES" Table__2 ON

(Table__2."CATALOG_NAME"=Table__3."CATALOG_NAME" AND

Table__2."SCHEMA_NAME"=Table__3."SCHEMA_NAME" AND

Table__2."CUBE_NAME"=Table__3."CUBE_NAME")

INNER JOIN (SELECT (substr("OBJECT_NAME",1, instr("OBJECT_NAME", '/')-

1)) AS "CATALOG_NAME", (substr("OBJECT_NAME",instr("OBJECT_NAME", '/')+1)) AS

"CUBE_NAME", "OBJECT_OID", "SCHEMA_NAME"

FROM "SYS"."OBJECTS") View__26

ON (Table__2."CATALOG_NAME"=View__26."CATALOG_NAME" AND

Table__2."SCHEMA_NAME"=View__26."SCHEMA_NAME" AND

Table__2."CUBE_NAME"=View__26."CUBE_NAME")

INNER JOIN "SYS"."VIEWS" Table__19 ON

(View__26."OBJECT_OID"=Table__19."VIEW_OID")

WHERE

(

Table__2."COLUMN_OBJECT" = :the_cube

AND

View__25."COLUMN_FLAG" = 'User Attribute'

);

BEGIN

/* Fetch cubes */

for row_cubes AS c_cubes DO

var_cube_name := row_cubes.column_object;

sql_script := 'SELECT ';

group_by := ' GROUP BY ';

placeholder := '';

first_row := 1;

/* Fetch columns */

for row_columns AS c_columns(:var_cube_name) DO

if :first_row > 1 then

sql_script := LEFT(sql_script || ', ', 5000);

end if;

sql_script := LEFT(sql_script || char(13) || char(10), 5000);

if row_columns.measure_aggregator_desc <> 'Unknown' then /* This is a

measure */

sql_script := LEFT(sql_script ||

row_columns.measure_aggregator_desc || '(' || '"' || row_columns.column_name ||

'"' || ')', 5000);

else

sql_script := LEFT(sql_script || '"' ||

row_columns.column_name || '"', 5000);

if group_by <> ' GROUP BY ' then

group_by := LEFT(group_by || ', ', 5000);

end if;

group_by := LEFT(group_by || char(13) || char(10) || '"' ||

row_columns.column_name || '"', 5000);

end if;

first_row := first_row + 1;

end for;

/* Fetch variables */

first_row := 1;

for row_variables AS c_variables (:var_cube_name) DO

if placeholder = '' then

placeholder := ' (';

end if;

if first_row > 1 then

placeholder := LEFT(placeholder || ',', 5000);

end if;

placeholder := LEFT(placeholder || '''PLACEHOLDER'' = (''$$' ||

row_variables.variable_name || '$$'', ', 5000);

if row_variables.column_type_d = 'INTEGER' THEN

placeholder := LEFT(placeholder || '''@Prompt(''' ||

row_variables.description || ''',''N'',,mono,free)'' )', 5000);

else

Page 12: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 12

placeholder := LEFT(placeholder || '@Prompt(''' ||

row_variables.description || ''',''A'',,mono,free) )', 5000);

end if;

first_row := first_row + 1;

end for;

if placeholder <> '' then

placeholder := LEFT(placeholder || ')', 5000);

end if;

/* Generate SQL script */

sql_script := LEFT(sql_script || char(13) || char(10) || ' from ' ||

:var_cube_name || :placeholder, 5000);

if group_by <> ' GROUP BY ' then

sql_script := LEFT(sql_script || char(13) || char(10) || :group_by,

5000);

end if;

SELECT COUNT(*) INTO nb_row FROM SL_DERIVED_TABLE WHERE CUBE_NAME =

:var_cube_name;

if :nb_row >= 1 then

DELETE FROM SL_DERIVED_TABLE WHERE CUBE_NAME = :var_cube_name;

end if;

INSERT INTO SL_DERIVED_TABLE VALUES (:var_cube_name, :sql_script);

end for;

END;

Page 13: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 13

Stored Procedure “DERIVED_TABLE_SINGLE”

(this commands prepares HANA to host the stored procedure which analyzes a single HANA views passed as a parameter)

DROP PROCEDURE DERIVED_TABLE_SINGLE;

SQL columns with attributes name and measures caption (this stored procedure will define the sentence for a universe derived table using the caption of both attributes and measures as column names)

CREATE PROCEDURE DERIVED_TABLE_SINGLE (IN cube_name VARCHAR(255))

LANGUAGE SQLSCRIPT AS

COLUMN_OBJECT VARCHAR(255) := '';

COLUMN_NAME VARCHAR(255) := '';

COLUMN_CAPTION VARCHAR(1024) := '';

COLUMN_TYPE_D VARCHAR(255) := '';

MEASURE_AGGREGATOR_DESC VARCHAR(255) := '';

VIEW_TYPE VARCHAR(255) := '';

VARIABLE_NAME VARCHAR(255) := '';

DESCRIPTION VARCHAR(255) := '';

SQL_SCRIPT VARCHAR(5000) := 'SELECT ';

GROUP_BY VARCHAR(5000) := ' GROUP BY ';

PLACEHOLDER VARCHAR(5000) := '';

FIRST_ROW INT := 1;

NB_ROW INT := 0;

CURSOR c_variables (cube_name VARCHAR(255)) FOR

SELECT

"VARIABLE_NAME",

"DESCRIPTION",

"COLUMN_TYPE_D"

FROM "_SYS_BI"."BIMC_VARIABLE"

WHERE

(

'"_SYS_BIC"."' || "CATALOG_NAME" || '/' || "CUBE_NAME" || '"' =

:cube_name

AND "MANDATORY" = 1

);

CURSOR c_columns (cube_name VARCHAR(255)) FOR

SELECT DISTINCT

View__25."COLUMN_NAME",

View__25."COLUMN_CAPTION",

View__25."MEASURE_AGGREGATOR_DESC",

Table__19."VIEW_TYPE"

FROM

(SELECT "CATALOG_NAME", "COLUMN_FLAG",

(CASE

WHEN "ALTERNATIVE_PROPERTY_NAME" <> '' THEN "ALTERNATIVE_PROPERTY_NAME"

ELSE "COLUMN_NAME"

END) AS "COLUMN_NAME",

"CUBE_NAME", "DIMENSION_UNIQUE_NAME", "DESCRIPTION" AS "COLUMN_CAPTION",

(CASE "MEASURE_AGGREGATOR"

WHEN 1 THEN 'Sum'

WHEN 2 THEN 'Count'

WHEN 3 THEN 'Min'

WHEN 4 THEN 'Max'

WHEN 5 THEN 'Average'

WHEN 6 THEN 'Variance'

ELSE 'Unknown'

Page 14: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 14

END) AS "MEASURE_AGGREGATOR_DESC", "SCHEMA_NAME"

FROM "_SYS_BI"."BIMC_PROPERTIES") View__25

INNER JOIN "_SYS_BI"."BIMC_DIMENSIONS" Table__3 ON

(Table__3."CATALOG_NAME"=View__25."CATALOG_NAME" AND

Table__3."SCHEMA_NAME"=View__25."SCHEMA_NAME" AND

Table__3."CUBE_NAME"=View__25."CUBE_NAME" AND

Table__3."DIMENSION_UNIQUE_NAME"=View__25."DIMENSION_UNIQUE_NAME")

INNER JOIN "_SYS_BI"."BIMC_CUBES" Table__2 ON

(Table__2."CATALOG_NAME"=Table__3."CATALOG_NAME" AND

Table__2."SCHEMA_NAME"=Table__3."SCHEMA_NAME" AND

Table__2."CUBE_NAME"=Table__3."CUBE_NAME")

INNER JOIN (SELECT (substr("OBJECT_NAME",1, instr("OBJECT_NAME", '/')-

1)) AS "CATALOG_NAME", (substr("OBJECT_NAME",instr("OBJECT_NAME", '/')+1)) AS

"CUBE_NAME", "OBJECT_OID", "SCHEMA_NAME"

FROM "SYS"."OBJECTS") View__26

ON (Table__2."CATALOG_NAME"=View__26."CATALOG_NAME" AND

Table__2."SCHEMA_NAME"=View__26."SCHEMA_NAME" AND

Table__2."CUBE_NAME"=View__26."CUBE_NAME")

INNER JOIN "SYS"."VIEWS" Table__19 ON

(View__26."OBJECT_OID"=Table__19."VIEW_OID")

WHERE

(

Table__2."COLUMN_OBJECT" = :cube_name

AND

View__25."COLUMN_FLAG" = 'User Attribute'

);

BEGIN

/* Fetch columns */

for row_columns AS c_columns(:cube_name) DO

if :first_row > 1 then

sql_script := LEFT(sql_script || ', ', 5000);

end if;

sql_script := LEFT(sql_script || char(13) || char(10), 5000);

if row_columns.measure_aggregator_desc <> 'Unknown' then /* This is a

measure*/

sql_script := LEFT(sql_script || row_columns.measure_aggregator_desc

|| '(' || '"' || row_columns.column_name || '"' || ')', 5000);

if row_columns.column_caption <> '' then

sql_script := LEFT(sql_script || ' AS "' ||

Replace(row_columns.column_caption, '"', '""') || '"', 5000);

end if;

else

sql_script := LEFT(sql_script || '"' || row_columns.column_name ||

'"', 5000);

if row_columns.column_caption <> '' then

sql_script := LEFT(sql_script || ' AS "' ||

Replace(row_columns.column_caption, '"', '""') || '"', 5000);

end if;

if group_by <> ' GROUP BY ' then

group_by := LEFT(group_by || ', ', 5000);

end if;

group_by := LEFT(group_by || char(13) || char(10) || '"' ||

row_columns.column_name || '"', 5000);

end if;

first_row := first_row + 1;

end for;

/* Fetch variables */

first_row := 1;

for row_variables AS c_variables (:cube_name) DO

if placeholder = '' then

placeholder := ' (';

end if;

if first_row > 1 then

placeholder := LEFT(placeholder || ',', 5000);

Page 15: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 15

end if;

placeholder := LEFT(placeholder || '''PLACEHOLDER'' = (''$$' ||

row_variables.variable_name || '$$'', ', 5000);

if row_variables.column_type_d = 'INTEGER' THEN

placeholder := LEFT(placeholder || '''@Prompt(''' ||

row_variables.description || ''',''N'',,mono,free)'' )', 5000);

else

placeholder := LEFT(placeholder || '@Prompt(''' ||

row_variables.description || ''',''A'',,mono,free) )', 5000);

end if;

first_row := first_row + 1;

end for;

if placeholder <> '' then

placeholder := LEFT(placeholder || ')', 5000);

end if;

/* Generate SQL script */

sql_script := LEFT(sql_script || char(13) || char(10) || ' from ' ||

:cube_name || :placeholder, 5000);

if group_by <> ' GROUP BY ' then

sql_script := LEFT(sql_script || char(13) || char(10) || :group_by, 5000);

end if;

SELECT COUNT(*) INTO nb_row FROM SL_DERIVED_TABLE WHERE CUBE_NAME =

:cube_name;

if :nb_row >= 1 then

DELETE FROM SL_DERIVED_TABLE WHERE CUBE_NAME = :cube_name;

end if;

INSERT INTO SL_DERIVED_TABLE VALUES (:cube_name, :sql_script);

END;

SQL columns with attributes name and measures name (this stored procedure will define the sentence for a universe derived table using the technical name of both attributes and measures as column names; it can be used as an alternative to the previous stored procedure)

CREATE PROCEDURE DERIVED_TABLE_SINGLE (IN cube_name VARCHAR(255))

LANGUAGE SQLSCRIPT AS

COLUMN_OBJECT VARCHAR(255) := '';

COLUMN_NAME VARCHAR(255) := '';

COLUMN_TYPE_D VARCHAR(255) := '';

MEASURE_AGGREGATOR_DESC VARCHAR(255) := '';

VIEW_TYPE VARCHAR(255) := '';

VARIABLE_NAME VARCHAR(255) := '';

DESCRIPTION VARCHAR(255) := '';

SQL_SCRIPT VARCHAR(5000) := 'SELECT ';

GROUP_BY VARCHAR(5000) := ' GROUP BY ';

PLACEHOLDER VARCHAR(5000) := '';

FIRST_ROW INT := 1;

NB_ROW INT := 0;

CURSOR c_variables (cube_name VARCHAR(255)) FOR

SELECT

"VARIABLE_NAME",

"DESCRIPTION",

"COLUMN_TYPE_D"

FROM "_SYS_BI"."BIMC_VARIABLE"

WHERE

(

'"_SYS_BIC"."' || "CATALOG_NAME" || '/' || "CUBE_NAME" || '"' =

:cube_name

AND "MANDATORY" = 1

);

CURSOR c_columns (cube_name VARCHAR(255)) FOR

Page 16: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 16

SELECT DISTINCT

View__25."COLUMN_NAME",

View__25."MEASURE_AGGREGATOR_DESC",

Table__19."VIEW_TYPE"

FROM

(SELECT "CATALOG_NAME", "COLUMN_FLAG",

(CASE

WHEN "ALTERNATIVE_PROPERTY_NAME" <> '' THEN "ALTERNATIVE_PROPERTY_NAME"

ELSE "COLUMN_NAME"

END) AS "COLUMN_NAME",

"CUBE_NAME", "DIMENSION_UNIQUE_NAME",

(CASE "MEASURE_AGGREGATOR"

WHEN 1 THEN 'Sum'

WHEN 2 THEN 'Count'

WHEN 3 THEN 'Min'

WHEN 4 THEN 'Max'

WHEN 5 THEN 'Average'

WHEN 6 THEN 'Variance'

ELSE 'Unknown'

END) AS "MEASURE_AGGREGATOR_DESC", "SCHEMA_NAME"

FROM "_SYS_BI"."BIMC_PROPERTIES") View__25

INNER JOIN "_SYS_BI"."BIMC_DIMENSIONS" Table__3 ON

(Table__3."CATALOG_NAME"=View__25."CATALOG_NAME" AND

Table__3."SCHEMA_NAME"=View__25."SCHEMA_NAME" AND

Table__3."CUBE_NAME"=View__25."CUBE_NAME" AND

Table__3."DIMENSION_UNIQUE_NAME"=View__25."DIMENSION_UNIQUE_NAME")

INNER JOIN "_SYS_BI"."BIMC_CUBES" Table__2 ON

(Table__2."CATALOG_NAME"=Table__3."CATALOG_NAME" AND

Table__2."SCHEMA_NAME"=Table__3."SCHEMA_NAME" AND

Table__2."CUBE_NAME"=Table__3."CUBE_NAME")

INNER JOIN (SELECT (substr("OBJECT_NAME",1, instr("OBJECT_NAME", '/')-

1)) AS "CATALOG_NAME", (substr("OBJECT_NAME",instr("OBJECT_NAME", '/')+1)) AS

"CUBE_NAME", "OBJECT_OID", "SCHEMA_NAME"

FROM "SYS"."OBJECTS") View__26

ON (Table__2."CATALOG_NAME"=View__26."CATALOG_NAME" AND

Table__2."SCHEMA_NAME"=View__26."SCHEMA_NAME" AND

Table__2."CUBE_NAME"=View__26."CUBE_NAME")

INNER JOIN "SYS"."VIEWS" Table__19 ON

(View__26."OBJECT_OID"=Table__19."VIEW_OID")

WHERE

(

Table__2."COLUMN_OBJECT" = :cube_name

AND

View__25."COLUMN_FLAG" = 'User Attribute'

);

BEGIN

/* Fetch columns */

for row_columns AS c_columns(:cube_name) DO

if :first_row > 1 then

sql_script := LEFT(sql_script || ', ', 5000);

end if;

sql_script := LEFT(sql_script || char(13) || char(10), 5000);

if row_columns.measure_aggregator_desc <> 'Unknown' then /* This is a

measure*/

sql_script := LEFT(sql_script || row_columns.measure_aggregator_desc

|| '(' || '"' || row_columns.column_name || '"' || ')', 5000);

else

sql_script := LEFT(sql_script || '"' || row_columns.column_name ||

'"', 5000);

if group_by <> ' GROUP BY ' then

group_by := LEFT(group_by || ', ', 5000);

end if;

group_by := LEFT(group_by || char(13) || char(10) || '"' ||

row_columns.column_name || '"', 5000);

end if;

Page 17: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 17

first_row := first_row + 1;

end for;

/* Fetch variables */

first_row := 1;

for row_variables AS c_variables (:cube_name) DO

if placeholder = '' then

placeholder := ' (';

end if;

if first_row > 1 then

placeholder := LEFT(placeholder || ',', 5000);

end if;

placeholder := LEFT(placeholder || '''PLACEHOLDER'' = (''$$' ||

row_variables.variable_name || '$$'', ', 5000);

if row_variables.column_type_d = 'INTEGER' THEN

placeholder := LEFT(placeholder || '''@Prompt(''' ||

row_variables.description || ''',''N'',,mono,free)'' )', 5000);

else

placeholder := LEFT(placeholder || '@Prompt(''' ||

row_variables.description || ''',''A'',,mono,free) )', 5000);

end if;

first_row := first_row + 1;

end for;

if placeholder <> '' then

placeholder := LEFT(placeholder || ')', 5000);

end if;

/* Generate SQL script */

sql_script := LEFT(sql_script || char(13) || char(10) || ' from ' ||

:cube_name || :placeholder, 5000);

if group_by <> ' GROUP BY ' then

sql_script := LEFT(sql_script || char(13) || char(10) || :group_by, 5000);

end if;

SELECT COUNT(*) INTO nb_row FROM SL_DERIVED_TABLE WHERE CUBE_NAME =

:cube_name;

if :nb_row >= 1 then

DELETE FROM SL_DERIVED_TABLE WHERE CUBE_NAME = :cube_name;

end if;

INSERT INTO SL_DERIVED_TABLE VALUES (:cube_name, :sql_script);

END;

Use the stored procedures

The following commands can be executed in the HANA studio SQL editor or in connectivity SQL editor of the information design tool, when connected to HANA

Stored Procedure “DERIVED_TABLE_ALL”: CALL DERIVED_TABLE_ALL;

Stored Procedure “DERIVED_TABLE_SINGLE” (examples): CALL DERIVED_TABLE_SINGLE ('"_SYS_BIC"."foodmart/INVENTORY_2006"');

CALL DERIVED_TABLE_SINGLE ('"_SYS_BIC"."mini/MINI_C1"');

CALL DERIVED_TABLE_SINGLE ('"_SYS_BIC"."eim360-99/GRA02_99"');

View content of the table SELECT * FROM SL_DERIVED_TABLE;

SELECT SQL_SCRIPT FROM SL_DERIVED_TABLE WHERE CUBE_NAME =

'"_SYS_BIC"."foodmart/INVENTORY _2006"';

Page 18: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 18

Related Content

Semantic Layer forum on SDN: http://forums.sdn.sap.com/forum.jspa?forumID=308

Universe best practices on SAP HANA: http://scn.sap.com/docs/DOC-20569

Information design tool tutorials: http://scn.sap.com/docs/DOC-8461

Using SAP HANA variables with SAP BusinessObjects BI4.0: https://scn.sap.com/docs/DOC-27676

BI on HANA basic tutorial: http://www.sdn.sap.com/irj/scn/bi-suite-tutorials?rid=/library/uuid/703298ef-02f9-2e10-2691-cfef154fd920

Information design tool on HANA viewlets: http://www.sdn.sap.com/irj/scn/info-design-tool-elearning?refer=main

Page 19: HANA Stored Procedures to Create Universe Derived … · HANA Stored Procedures to Create Universe Derived Tables ... Those stored procedures will provide the SQL ... HANA Stored

HANA Stored Procedures to Create Universe Derived Tables

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 19

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 compyounts 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 mentiyoud 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 mentiyoud 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 mentiyoud 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.