anthyiaconsulting

ABAP Tutorial

8 CDS Views

a Create CDS Views

Right-click on your package name, type core in the search box and select Data Definition.

(image)

Figure I.28: CDS View: Select Data Definition

Referenced Object you fill in the name of a table or the name of a CDS view.

This means you can create CDS views of CDS views.

(image)

Figure I.29: CDS View: Data Definition
b Using system variables in CDS views

The following system variables are available in CDS views.

vname

Value in Open SQL Accesses

user

Current user name, nominal value of the ABAP system field sy-uname

client

Current client. The default value is the nominal value of the ABAP system field sy-mandt. In reads with an Open SQL statement (with the statement USING CLIENT) and in calls of an AMDP method from ABAP (in whose declaration the addition AMDP OPTIONS CDS SESSION CLIENT is specified), the value specified here.

system_language

Text environment language of the current internal session, nominal value of the ABAP system field sy-langu

system_date

Current system date of the AS ABAP, nominal value of the ABAP system field sy-datum

Here an example how to use them.

1            define view Z_AWP_CDS_HSMT_MD as select from hsmt_md
2            { key mandt as Mandt,
3                 valto as Valto}
4            where valto > $session.system_date

Listing I.4: Usage of CDS view system variables
c Join on join
1
2   define     view Z_AWP_CDS_HAZ_WRONG
3   as     select   from Z_AWP_CDS_aqua as A
4   join      ScmPrd_Ndbsmatg16 as N
5   on A. Matid = N. guid
6   join Z_AWP_CDS_HSMT_MD as H
7   on N. matnr = H.Matnr
8   {...
9
Listing I.1: CDS view: join on join
d Coalesce
COALESCE( arg1, arg2 )

The coalesce function returns the value of the argument arg1 (if this is not the null value); otherwise it returns the value of the argument arg2.

1     define   view Z_AWP_CDS_LAGP as select from /scwm/ lagp
2    {    key lgnum as Lgnum,
3        coalesce   ( cast ( anzle   as abap . dec (10,2)   ) , 0) as Anzle ,
4        coalesce   ( cast ( maxle as abap . dec (10,2)     ) , 0) as Maxle
5
Listing I.2: Example of Coalesce
e Cast
CAST( operand AS dtype [PRESERVING TYPE])

The cast expression converts the value of the operand operand to the dictionary type specified by dtype. The result has the type dtype. The following can be specified for dtype:

(image)

Figure I.30: datatypes the CDS cast function can use

(image)

Figure I.31: Table of datatypes you can cast from and to

I copied the information contained in these two tables from https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abencds_f1_cast_expression.htm. It seems like a good idea, to check, whether this information has been updated.

Example of cast

1         define   view Z_AWP_CDS_LAGP as select from /scwm/ lagp
2         {    key lgnum as Lgnum,
3             coalesce   ( cast ( anzle   as abap . dec (10,2)   ) , 0) as Anzle ,
4             coalesce   ( cast ( maxle as abap . dec (10,2)     ) , 0) as Maxle
5
Listing I.3: Example of Cast