CDS Views Notes
CDS Views Notes
•SQL
•Structured Query Language
•There are three sublanguages of SQL:
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
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)
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
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
TABLES BSIK.
SELECT-OPTIONS S_BUKRS FOR BSIK-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
}
Currency_conversion( amount => al, source_currency => a2, target _currency =>a3,
exchange_rate_date => a4)
Returns result of type abap.curr
'INR' as TARGET_CURRENCY,
CONCAT(bukrs,belnr)as UNIQUE_ID,
LENGTH(CONCAT(bukrs,belnr)) as LENGTH,
SUBSTRING(bldat,7,2) as DOCUMENT_DAY
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
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
}
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.
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 pick the data from right hand side table, even though there is no match found in
Left hand side table
Prerequisites:
Same number of elements
Elements in same position have compatible data type
Elements in same position have identical name/alias
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
CL_DEMO_OUTPUT=>DISPLAY( IT_KNA1 ).
CL_DEMO_OUTPUT=>DISPLAY( IT_BSIK ).
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.
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.
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.
}
ABAP program to consume the data form cds view
REPORT zsp_8pm_occds13.
@AbapCatalog.sqlViewName: 'ZSP_8PM_OSQL14'
@EndUserText.label: 'Simple cds'
define view ZSP_8PM_OCDS14 as select from kna1
{
kunnr,
name1,
name2,
ort01,
land1
}
TABLES KNA1.
SELECT-OPTIONS S_LAND1 FOR KNA1-LAND1.
CL_DEMO_OUTPUT=>DISPLAY( IT_RES ).