0% found this document useful (0 votes)
3 views

CDS Views Notes

The document provides an overview of SQL basics, including its sublanguages (DDL, DML, DCL) and the limitations of classical Open SQL compared to new Open SQL. It introduces Core Data Services (CDS) and their components, including data definition, query language, and data control language, while also detailing the features and syntax rules for creating CDS views. Additionally, it covers built-in functions, aggregations, joins, and examples of using annotations and SQL expressions in CDS views.

Uploaded by

Kumar Gadde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CDS Views Notes

The document provides an overview of SQL basics, including its sublanguages (DDL, DML, DCL) and the limitations of classical Open SQL compared to new Open SQL. It introduces Core Data Services (CDS) and their components, including data definition, query language, and data control language, while also detailing the features and syntax rules for creating CDS views. Additionally, it covers built-in functions, aggregations, joins, and examples of using annotations and SQL expressions in CDS views.

Uploaded by

Kumar Gadde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Understanding background for CDS SQL Basics

•SQL
•Structured Query Language
•There are three sublanguages of SQL:

Data Definition Language (DDL)

CREATE TABLE, CREATE VIEW, ALTER TABLE, DROP TABLE

Data Manipulation Language (DML)

SELECT, INSERT, UPDATE, and DELETE

Data Control Language (DCL)

GRANT and REVOKE

The Limitations of Classical Open SQL

Open SQL ( < ABAP 7.40 SPO5 )

•No fixed-values and computed columns


•No CASE expression
•No right outer joins
•No UNIONs 'No sub-queries-in SELECT or FROM clauses
•No access to SAP HANA built-in functions, for example. DAYS_BETWEEN ( )
•No access to SAP HANA views and procedures

New Open SQL ( >. ABAP 7.40 SP05 )

Support arithmetic expressions with operators + - /


Arithmetic functions FLOOR, CEIL, MOD, ABS, DIV
Support CASE expression
Support right outer join
Support UNION and UNION ALL
The maximum number of tables supported in Open SQL JOIN clauses has been increased to 50.
The maximum number of sub-queries supported in an Open SQL statement has been increased
from 9 to50
Access to SAP HANA built-in functions, for example, DAYS_BETWEEN ( )
Access to SAP HANA views and procedures
The Limitations of ABAP Dictionary Views

Support only Inner Join


Calculation expression, aggregation, grouping not supported
Nested Views not supported

Core Data Services (CDS )


Core Data Services are a collection of domain specific languages and services for defining and
consuming semantically rich data model.

Semantics:
As attributes that help describe an underlying entity
Data models:
Provide a standardized method for defining and formatting database contents consistently across
systems, enabling different applications to share the same data

Core Data Services are made of up 3 sub languages


Data Definition Language (DDL)
Model and retrieve data on a semantic level higher than SQL
Extends native SQL means for increased productivity

2. Query Language (QL)


Consume CDS entities via Open SQL in ABAP
Fully transparent SQL extensions

3. Data Control Langue (DCL)


Define authorizations for CDS views
Modeled and declarative Approach
Integrates with classical authorization concepts

CDS related Repository Objects


With ABAP CDS, the following new repository object types were introduced
1. Data definition
2. Access control

Data Definition:
• Also referred to as DDL Source (for Data Definition Language, named after the DDL part of
SQL)
• Contains the definition of either a CDS View or a CDS Table function
• Display only in ABAP workbench
• Editing requires the user of ABAP Development Tool (ADT in Eclipse)
Access Control:
• Also referred to as DCL Source (for Data Controlling language, named after the DCL part of
SQL)
• Contains the definition of authorization rules hat are automatically checked when a program
access a certain CDS View or CDS table functions
• Display only in ABAP workbench
• Editing requires the user of ABAP Development Tool (ADT in Eclipse)

Basic Features of ABAP CDS


• Database Independent
Use ABAP CDS with any database supported by SAP

• Advanced View Building


CDS view provide much more SQL features than classical dictionary views

• Annotations to add semantic information


Add end user texts, currency keys, buffer settings
Add semantic information for consumers (BI analytics, Odata, SAP U15)

• Implicit Authorization checks


Define authorization rules for CDS objects

• Associations instead of joins


Defines relations between CDS objects that will be translated into joins

• CDS Table Functions


Views based on scripted coding

Syntax rules inside DDL Sources


Keywords
Keywords must be all uppercase, all lowercase, or in lowercase with an uppercase initial letter.
Mixed uppercase and lowercase is not allowed.
Allowed: SELECT, Select, Not allowed ( SeLect)

Comments
Two forward slashes (II) introduce a comment, which continues until the end of the line.
Comments within lines or that span multiple lines are enclosed by the characters /* and */
Names
Names are not case-sensitive. A name can have a maximum of 30 characters.
Literals
Number literals must always be specified in full and a decimal point (.) used as a decimal
separator if necessary. Character literals are enclosed in single quotations marks (')
Allowed: 1, 2.0, 0. 5
Not allowed: .5
Naming Rules
DDL Source name:
Max 30 characters
Always Upper-Case
CDS View name:
Max 30 characters
Not Case-sensitive Can be different from DDL Source name (not recommended)
SQL View name:
Max 16 characters
Not Case-sensitive
Different from CDS View name

Object: Create a CDS View with KUNNR, NAME1,ORT01 and LAND1


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL1'
@EndUserText.label: 'Simple cds'
Define view ZSP_8PM_OCDS1 as select from kna1
{
kunnr,
name1,
ort01,
land1

ABAP program to consume the data from CDS View


REPORT zsp_8pm_occds1.

SELECT * FROM ZSP_8PM_OCDS1 INTO TABLE @DATA(IT_KNA1).


CL_SALV_TABLE=>factory(
IMPORTING
r_salv_table = DATA(OB_SALV)
CHANGING
t_table = IT_KNA1
).
OB_SALV->DISPLAY( ).

Annotations
Annotations are code snippets used to enable certain functions or to add component-specific
metadata to a CDS view.
5 groups of annotations:
View Annotation
@AbapCatalog.sqlViewName: 'Z_CDS_SQL_EX2'
@EndUserText.label: 'use of annotation in cds views'
Element annotations
@Semantics.amount.currencyCode: ‘local_currency_key’
@Semantics.currencyCode: true
Parameter annotations
@Environment.systemField: SSYSTE M_DATE
Extension annotations
@AbapCatalog.sqlViewAppendName: ‘Z_CDS_SQL_EX9'
Function annotations

Example on Annotations in CDS Views


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL2'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@AbapCatalog.buffering.status: #ACTIVE
@AbapCatalog.buffering.type: #SINGLE
@EndUserText.label: 'Working with annotations'
define view ZSP_8PM_OCDS2 as select from bsik
{
bukrs,
belnr,
gjahr,
bldat,
budat,
dats_days_between(bldat, budat) as DUE_DAYS,
@Semantics.amount.currencyCode: 'WAERS'
dmbtr,
@Semantics.currencyCode: true
waers
}

Program to consume the data from ABAP CDS


REPORT zsp_8pm_occds2.

TABLES BSIK.
SELECT-OPTIONS S_BUKRS FOR BSIK-BUKRS.

SELECT * FROM ZSP_8PM_OCDS2 INTO TABLE @DATA(IT_BSIK)


WHERE BUKRS IN @S_BUKRS.

CL_SALV_TABLE=>factory(
IMPORTING
r_salv_table = DATA( OB_SALV)
CHANGING
t_table = IT_BSIK
).

OB_SALV->DISPLAY( ).
Syntax of CASE in CDS
CASE <Operand/element>
When '<Value1>' Then '<Result1>'
When '<Value2>' Then '<Result2>'
...
Else '<Result N>'
END AS <Alias name>’

OR

CASE
WHEN <Operend1/Elemen1> = '<Value1>' and <Operend2/Element2> = '<Value2>' Then
'<Result1>'
WHEN <Operend1/Elemen1> = '<Value3>' and <Operend2/Element2> = '<Value4>' Then
'<Result2>'
....
ELSE '<Result N>'
END as <Alias name>
Object : To display material numbers, material types, material groups and material type
description(Ex: MTART = 'ROH'---->'Raw material', MTART = 'HALB'---> 'Semi
finished product', MTART = 'FERT'---->'Finished product'...)
@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL9'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Working with CDS with CASE Expressions'
define view ZSP_8PM_OCDS9 as select from Mara
{
matnr,
mtart,
matkl,
meins,
case mtart
when 'ROH' then 'Raw material'
when 'HALB' then 'Semi finished product'
when 'FERT' then 'Finished product'
else 'Not classified'
end as MTYP_DES
}

ABAP Program to consume the data from ABAP CDS


REPORT zsp_8pm_occds9.
SELECT * FROM ZSP_8PM_OCDS9 INTO TABLE @DATA(IT_MARA).
CL_DEMO_OUTPUT=>DISPLAY( IT_MARA ).
Built-in Functions for arithmetic expressions
abs(al)
Returns the absolute of al abs(- 2.5) = 2.5
floor(al)
Rounds to the next lower integer floor( 2.5) = 2 floor( -2.5) = -3
ceil (al)
Rounds to the next higher integer ceil( 2.5) = 3 ceil( -2.5) -2
round(a 1,pos)
Round al to pos decimal places round(4.9514, 2) = 4.95

CAST(operand AS target type )


Converts the value of operand into target type
Target type: A predefined dictionary type e.g. abap.int4, abap.char(10), abap.dec(8, 2)
Any Dictionary data element e.g LAND1_GP

Built-in Functions for string processing


concat(al,a2)
Returns result of type CHAR or STRING
Corresponds to ABAP statement CONCATENATE without addition SEPARATED BY
Replace(al, a2, a3)
Result type depends on type of al corresponds to ABAP statement REPLACE ALL
OCCURENCES OF a2 IN al WITH a3.
substring(al,pos,len)
Result type depends on type of al
Similar to ABAP function substring( ) or direct substring access.
Length(al)
Return result of type INT4 Corresponds to ABAP function num of char( )

Built-in Functions for currency and unit processing


unit_conversion ( Quantity => al, source _unit => a2, target_unit => a3 )
Returns result of type abap.quan

Currency_conversion( amount => al, source_currency => a2, target _currency =>a3,
exchange_rate_date => a4)
Returns result of type abap.curr

Example on Artimetical and string expressions in CDS Views


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL3'
@EndUserText.label: 'Working with built-in functions'
Define view ZSP_8PM_OCDS3 as select from bsik
{
bukrs as COMP_CODE,
belnr as ACC_DOC,
gjahr as FYEAR,
bldat as DOC_DATE,
budat as POSTING_DATE,
ABS(dmbtr) as ABS_VALUE,
FLOOR(dmbtr) as LOWER_VALUE,
CEIL(dmbtr) as HIGHER_VALUE,
ROUND(dmbtr,2) as ROUND_VALUE,
dmbtr as AMOUNT_LC,
waers as CURRENCY,
currency_conversion( amount => dmbtr,
source_currency => waers,
target_currency => cast('INR' as abap.cuky( 5 )),
exchange_rate_date => cast( '20221220' as abap.dats )
) as TARGET_AMOUNT,

'INR' as TARGET_CURRENCY,
CONCAT(bukrs,belnr)as UNIQUE_ID,
LENGTH(CONCAT(bukrs,belnr)) as LENGTH,
SUBSTRING(bldat,7,2) as DOCUMENT_DAY

ABAP program to consume the data from ABAP CDS


REPORT zsp_8pm_occds3.
SELECT * FROM ZSP_8PM_OCDS3 INTO TABLE @DATA(IT_BSIK).
CL_DEMO_OUTPUT=>DISPLAY( IT_BSIK ).

Aggregations in CDS Views


MIN(operand)
Returns smallest value in operand
MAX( operand)
Returns greatest value in operand
SUM( operand)
Calculates the sum of values of operand
AVG( operand)
Calculates the average value of the values of operand
COUNT(* )
Returns the number of entries in the result set
COUNT( DISTINCT operand)
Returns number of distinct values of operand

GROUP BY
All elements not defined using aggregate expressions must be specified after GROUP BY

HAVING
The HAVING clause is used to restrict the results returned by the GROUP BY clause

Example on Aggregation functions in CDS Views


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL16'
@EndUserText.label: 'Workign with aggregate functions'
define view ZSP_8PM_OCDS16 as select from ekpo
{
matnr,
min(netpr) as MIN_VALUE,
max(netpr) as MAX_VALUE,
avg(netpr) as AVG_VALUE,
sum(netpr) as TOTAL_VALUE
}
group by matnr

ABAP program to consume the data from ABAP CDS


REPORT zsp_8pm_occds16.

SELECT * FROM ZSP_8PM_OCDS16 INTO TABLE @DATA(IT_EKPO).


CL_DEMO_OUTPUT=>DISPLAY( IT_EKPO ).

JOINS
Joins are used to fetch the data from more than one database table
There are 3 types of joins
1. Inner join
2. Left outer join
3. Right outer join

Inner join
Inner join pick the data from both the tables, if and only if there is one or more than one entry is
available in right hand side table with corresponding left hand side table
Object : Create ABAP CDS to display Customer numbers, customer names,sales.doc
numbers, doc.dates
@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL4'
@EndUserText.label: 'Working with CDS View with JOINS'
define view ZSP_8PM_OCDS4 as select
from kna1 as A inner join vbak as B
on A.kunnr = B.kunnr
{
A.kunnr,
A.name1,
B.vbeln,
B.audat
}

ABAP Program to consume data from ABAP CDS


REPORT zsp_8pm_occds4.

SELECT * FROM ZSP_8PM_OCDS4 INTO TABLE @DATA(IT_RES).

CL_SALV_TABLE=>factory(
IMPORTING
r_salv_table = DATA(OB_SALV)
CHANGING
t_table = IT_RES
).
OB_SALV->DISPLAY( ).

Object : Create ABAP CDS View to display Vendor numbers, vendor names,pur.doc
numbers, doc.dates, item numbers, quantity, uom and net price
@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL5'
@EndUserText.label: 'Working with multiple joins in ABAP CDS'
define view ZSP_8PM_OCDS5 as select
from lfa1 inner join ekko on lfa1.lifnr = ekko.lifnr
inner join ekpo on ekko.ebeln = ekpo.ebeln
{
lfa1.lifnr,
lfa1.name1,
ekko.ebeln,
ekko.bedat,
ekpo.ebelp,
ekpo.menge,
ekpo.meins,
ekpo.netpr

}
ABAP Program to consume the data from ABAP CDS
REPORT zsp_8pm_occds5.

SELECT * FROM ZSP_8PM_OCDS5 INTO TABLE @DATA(IT_RES).


CL_DEMO_OUTPUT=>DISPLAY( IT_RES ).

Left outer join

Left outer join pick the data from left hand side table, even though there is no match found in
right hand side table

Right outer join

Right outer join pick the data from right hand side table, even though there is no match found in
Left hand side table

Union and Union ALL


UNION
The combined result contains no duplicate rows
UNION All
The results are combined and duplicate rows are not removed.

Prerequisites:
Same number of elements
Elements in same position have compatible data type
Elements in same position have identical name/alias

8. Example on Union and Union all in CDS Views


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL6'
@EndUserText.label: 'Working with UNION and UNION ALL'
define view ZSP_8PM_OCDS6 as select from bsik
{
bukrs,
belnr,
gjahr,
bldat,
budat,
dmbtr
}

union // union all

select from bsak


{
bukrs,
belnr,
gjahr,
bldat,
budat,
dmbtr
}

ABAP program to consume the data from ABAP CDS


REPORT zsp_8pm_occds6.
SELECT * FROM ZSP_8PM_OCDS6 INTO TABLE @DATA(IT_RES).
CL_DEMO_OUTPUT=>DISPLAY( IT_RES ).
CDS view with input parameters
A CDS (Core Data Services) view with input parameters is a type of view in SAP HANA that
allows users to define a reusable query that can accept input parameters at runtime.
Input parameters can be used to filter data, to restrict the output of a query to a specific subset of
data that meets certain conditions.
Syntax
Define View <CDS View name>
With parameters <input parameter1> : <Data type>,
<Input parameter2> : <Data type>

Note: The data type must be either data element or dictionary predefined types(abap.char,
abap.cuky….)
Note: In the where condition, we use input field as follows
:< Input parameter1> OR $parameters. <Input parameter1>
Object : Develop a ABAP CDS to display customer numbers,customer names,cities and
countries based on given country
@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL7'
@EndUserText.label: 'Working with CDS View with iparameters'
define view ZSP_8PM_OCDS7
with parameters I_LAND1 : land1_gp
as select from kna1
{
kunnr,
name1,
ort01,
land1
}
where land1 = :I_LAND1

ABAP program to consume the data from ABAP CDS


REPORT zsp_8pm_occds7.

PARAMETERS P_LAND1 TYPE KNA1-LAND1.

SELECT * FROM zsp_8pm_ocds7( i_land1 = @P_LAND1 ) INTO TABLE


@DATA(IT_KNA1).

CL_DEMO_OUTPUT=>DISPLAY( IT_KNA1 ).

Example on ABAP CDS With input parameters


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL8'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Working with CDS View with iparameters'
define view ZSP_8PM_OCDS8
with parameters I_TCUKY : abap.cuky( 5 ),
@Environment.systemField: #SYSTEM_DATE
I_ERDATE : abap.dats
as select from bsik
{
bukrs,
belnr,
gjahr,
bldat,
budat,
dmbtr,
waers,
:I_TCUKY as TARGET_CURRECY,
currency_conversion( amount => dmbtr,
source_currency => waers,
target_currency => :I_TCUKY,
exchange_rate_date => $parameters.I_ERDATE ) as
TARGET_AMOUNT
}

ABAP Program to consume the data from ABAP CDS


REPORT zsp_8pm_occds8.

PARAMETERS : P_TCUKY TYPE BSIK-WAERS,


P_ERDATE TYPE SYDATUM.

SELECT * FROM zsp_8pm_ocds8( i_tcuky = @P_TCUKY , i_erdate = @P_ERDATE )


INTO TABLE @DATA(IT_BSIK).

CL_DEMO_OUTPUT=>DISPLAY( IT_BSIK ).

Working with associations


There are 3 types of associations
1. Ad-hoc associations
2. Exposed associations
3. Filtered associations

Ad-hoc associations
The data definition that defines the association uses an individual field of the associated table or
CDS view, are immediately translated into JOIN clause in the SQL create statement that is sent
to the database.

Example on ABAP CDS with ad-hoc associations


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL10'
@EndUserText.label: 'Working with Ad-hoc associations'
define view ZSP_8PM_OCDS10 as select
from kna1 as a association to vbak as _b
on a.kunnr = _b.kunnr
{
a.kunnr,
a.name1,
a.ort01,
_b.vbeln, //ad-hoc association
_b.audat //ad-hoc association

ABAP program to consume the data form ABAP CDS


REPORT zsp_8pm_occds10.

SELECT * FROM ZSP_8PM_OCDS10 INTO TABLE @DATA(IT_RES).


CL_DEMO_OUTPUT=>DISPLAY( IT_RES ).

Exposed associations
Instead of addressing individual fields of the associated table or CDS View, It is also possible to
add the entire association as a single element into the field list. This is exposed association.

Example on ABAP CDS With exposed association


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL11'
@EndUserText.label: 'Working with Exposed association'
define view ZSP_8PM_OCDS11 as select
from kna1 as a association to vbak as _b
on a.kunnr = _b.kunnr
{
a.kunnr,
a.name1,
a.ort01,
_b //Exposed association

ABAP Program to consume the data from cds view with exposed association
REPORT zsp_8pm_occds11.

SELECT KUNNR,
NAME1,
ORT01,
\_b-vbeln,
\_b-audat,
\_b-auart,
\_b-bukrs_vf
from ZSP_8PM_OCDS11 INTO TABLE @DATA(IT_RES).

CL_DEMO_OUTPUT=>DISPLAY( IT_RES ).

Example on consume the one CDS from some other CDS view
@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL12'
@EndUserText.label: 'Consume form exposed associaon cds view'
define view ZSP_8PM_OCDS12 as select from ZSP_8PM_OCDS11 as c
{
c.kunnr,
c.name1,
c.ort01,
c._b.vbeln, //patch expression
c._b.audat //patch expression
}

Filtered associations
A filtered association in CDS View is an association between two entities that includes a filter
condition to restrict the related records that are included in the association. This allows you to
define a relationship between two entities that only includes a subset of the related records, based
on specific criteria.

Example on filtered assoc ions in ABAP CDS


@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL13'
@EndUserText.label: 'Working with filterd associations'
define view ZSP_8PM_OCDS13 as select
from kna1 as a association to vbak as _b
on a.kunnr = _b.kunnr
{
a.kunnr,
a.name1,
a.ort01,
_b[auart = 'TA'].vbeln, //Filted association
_b.audat,
_b.auart

}
ABAP program to consume the data form cds view
REPORT zsp_8pm_occds13.

SELECT * FROM ZSP_8PM_OCDS13 INTO TABLE @DATA(IT_RES).


CL_DEMO_OUTPUT=>DISPLAY( IT_RES ).

Working with Extension of CDS View


Simple CDS view :ZSP_8PM_OCDS14

@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL14'
@EndUserText.label: 'Simple cds'
define view ZSP_8PM_OCDS14 as select from kna1
{
kunnr,
name1,
name2,
ort01,
land1
}

Example on Extension of CDS View


@AbapCatalog.sqlViewAppendName: 'ZSP_8PM_OSQL15'
@EndUserText.label: 'Extension of cds view'
extend view ZSP_8PM_OCDS14 with ZSP_8PM_OCDS15
association to vbak as _B on kna1.kunnr = _B.kunnr
{
CONCAT(name1,name2) as FULL_NAME,
UPPER(name1) as UPPER_NAME,
_B.vbeln,
_B.audat,
_B.bukrs_vf
}
ABAP program to consume the data from CDS View
REPORT zsp_8pm_occds14.

TABLES KNA1.
SELECT-OPTIONS S_LAND1 FOR KNA1-LAND1.

SELECT * FROM ZSP_8PM_OCDS14 INTO TABLE @DATA(IT_RES)


WHERE LAND1 IN @S_LAND1.

CL_DEMO_OUTPUT=>DISPLAY( IT_RES ).

You might also like