Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
This document outlines our general product direction and should not be relied on in making a purchase decision. This
document is not subject to your license agreement or any other agreement with SAP. SAP has no obligation to pursue
any course of business outlined in this document or to develop or release any functionality mentioned in this
document. This document and SAP's strategy and possible future developments are subject to change and may be
changed by SAP at any time for any reason without notice. This document is provided without a warranty of any kind,
either express or implied, including but not limited to, the implied warranties of merchantability, fitness for a particular
purpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this document, except if such
damages were caused by SAP intentionally or grossly negligent.
2
Table of Contents
A. What’s inside this exercise? ...................................................................................................................... 2
B. Create a simple CDS View........................................................................................................................ 3
C. Create a CDS View using Aggregations, built-in Functions and CASE Expressions ............................... 11
C1. Using Joins ........................................................................................................................................... 11
C2. Using Aggregations .............................................................................................................................. 16
C3. Using Built-In Functions ........................................................................................................................ 21
C4. Using CASE Expressions ..................................................................................................................... 23
D. Using CDS Views in ABAP programs ..................................................................................................... 26
E. Annotations............................................................................................................................................. 27
F. Associations ............................................................................................................................................ 30
The domain-specific languages (DSLs) are based on entity-relationship models for defining and accessing
common core models and mainly comprise a Data Definition Language (DDL), a Query Language (QL), a
Data Manipulating Language (DML) and a Data Control Language (DCL).
DDL QL
DML DCL
From an ABAP developer’s point of view you may now ask, what’s in there for us? So, the basic Idea behind
the integration of CDS into the ABAP server is to support code pushdown to the database layer and to
simplifying the consumption of relational data models by means of view entities/DDL Sources objects in
ABAP.
In this exercise you will familiarize yourself with the advanced view building techniques using the CDS views
in the AS ABAP. The exercise consists of the following steps
· Create a simple CDS view
· Extend a simple CDS view with built-in functions and expressions
· Working with Annotations
In the first exercise you will create a simple CDS View in order to familiarize yourself with the CDS editor
and the DDL syntax for creating CDS views.
Explanation Screenshot
Explanation Screenshot
Press Next.
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
12. Add the missing SQL view
name to the CDS view
Definition.
@AbapCatalog.sqlViewName: 'ZCDSV_1_##'
define view zcdsv_simple_1_## as select from snwd_so as so
{
key so.so_id as sales_order_id,
so.currency_code,
so.gross_amount,
so.created_at
}
Explanation Screenshot
15. After the activation was
successful, an additional
ABAP Dictionary object will be
generated: It is the associated
DDL SQL view.
Explanation Screenshot
Explanation Screenshot
Summary:
You have familiarized yourself with the ABAP DDL Source Editor:
· You’ve created your own Core Data Services view in ABAP
· You know that the DDL Source Editor supports the Quick Fix and the Code Completion features
· You know that an ABAP Dictionary View generates if you create an CDS view
C. Create a CDS View using Aggregations, built-in Functions and CASE Expressions
This exercise consists of four major sections and we will cover the following features:
· Joins
· Aggregate functions
· Built-in string and arithmetic functions
· Expressions
The CDS view defined in the exercise includes the joins on the Sale Orders, Sales Order Header and
business partner tables as depicted below:
In this exercise we will need the Sales Order table (SNWD_SO) for the determination of the amount and the
Business Partner table (SNWD_BPA) for the information about the company names. We will join both tables
on field BUYER_GUID from the Sales Order table and field NODE_KEY from the Business Partner table.
In addition we will check whether the sales orders are not billed. For that we will a third table: The Sales
Order Invoice Header table (SNWD_SO_INV_HEAD). We will join the tables on field NODE_KEY from the
Sales Order table and the field SO_GUID from the Invoice Header table.
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
5. Define select list between curly
brackets after the Join
statements.
@AbapCatalog.sqlViewName: 'ZCDSV_2_##'
define view zcdsv_multiple_features_2_## as select from snwd_so as so
inner join snwd_bpa as bp on so.buyer_guid = bp.node_key
left outer join snwd_so_inv_head as so_inv on so.node_key = so_inv.so_guid
{
key so.so_id,
bp.company_name,
so.currency_code,
so.gross_amount
}
Explanation Screenshot
Explanation Screenshot
The CDS DDL syntax provides the aggregate functions AVG, MAX, SUM, MIN and COUNT.
In this exercise we will use the aggregate function SUM to calculate the sum gross amount of sales orders.
We will work on the DDL Sources ZCDSV_MULTIPLE_FEATURES_## (where ## represents your group
number) created in the exercise C1.
Explanation Screenshot
1. Calculate the sum of the gross
mount from the table SNWD_SO.
Explanation Screenshot
@AbapCatalog.sqlViewName: 'ZCDSV_2_##'
define view zcdsv_multiple_features_2_## as select from snwd_so as so
inner join snwd_bpa as bp on so.buyer_guid = bp.node_key
left outer join snwd_so_inv_head as so_inv on so.node_key =
so_inv.so_guid
{
key so.so_id,
bp.company_name,
so.currency_code,
sum(so.gross_amount) as sum_gross_amount
}
where so_inv.node_key is null
group by so.so_id, bp.company_name, so.currency_code
Explanation Screenshot
9. Delete the key field SO_ID from your select list and from the GROUP BY clause.
Explanation Screenshot
Explanation Screenshot
Remark: Similarly to
aggregate functions, an alias
is required for the result of a
built-in function.
Code: ZCDSV_MULTIPLE_FEATURES_##
@AbapCatalog.sqlViewName: 'ZCDSV_2_##'
define view zcdsv_multiple_features_2_## as select from snwd_so as so
inner join snwd_bpa as bp on so.buyer_guid = bp.node_key
left outer join snwd_so_inv_head as so_inv on so.node_key = so_inv.so_guid
{
key bp.company_name,
so.currency_code,
sum(so.gross_amount) as sum_gross_amount,
substring( bp.company_name, 1, 10 ) as short_name
}
where so_inv.node_key is null
group by bp.company_name, so.currency_code
Explanation Screenshot
Explanation Screenshot
Conditions:
- If the delivery status equal
SPACE, then we set the case so.delivery_status
value of result field to when '' then 'OPEN'
“OPEN“ when 'D' then 'DELIVERED'
- If the delivery status is “D”, else so.delivery_status
then we set the value of end as delivery_status,
result field to “DELIVERED“
- In other cases, then we
return the value of the field
SO.DELIVERY_STATUS.
Explanation Screenshot
@AbapCatalog.sqlViewName: 'ZCDSV_2_##'
define view zcdsv_multiple_features_2_## as select from snwd_so as so
inner join snwd_bpa as bp on so.buyer_guid = bp.node_key
left outer join snwd_so_inv_head as so_inv on so.node_key =
so_inv.so_guid
{
key bp.company_name,
so.currency_code,
sum(so.gross_amount) as sum_gross_amount,
case so.delivery_status
when ' ' then 'OPEN'
when 'D' then 'DELIVERED'
else so.delivery_status
end as delivery_status
}
where so_inv.node_key is null
group by bp.company_name, so.currency_code, so.delivery_status
Explanation Screenshot
Summary:
In this exercise, you learned how to use Core Data Services features like aggregations, built-in functions
and CASE expressions that are provided by the CDS DDL syntax.
In this exercise, we will use the CDS view defined in Exercise C in an ABAP program.
As seen in the previous exercises, whenever you create a CDS view, an associated SQL view is generated
and managed by the ABAP Dictionary in addition. CDS view entities are supported in Open SQL and are
consumed similarly to ABAP Dictionary views.
Explanation Screenshot
Code: ZR_CDSV_OUTPUT_##
REPORT zr_cds_output_tpl.
Explanation Screenshot
REPORT zr_cds_output_sol.
E. Annotations
Annotations are used to enrich data models with additional metadata. They start with @ in the CDS view
definition.
The annotation @AbapCatalog.sqlViewName must be specified before the view itself is defined using
DEFINE VIEW. Further annotations can also optionally be specified.
There are two types of annotations: The first type of annotations provides metadata which applies to the
whole CDS view entity (e.g. specifying the table buffer settings of a view). Such annotations are listed before
the statement DEFINE VIEW. The second type of annotations is used to have influence of a single element
from the select list (e.g. specifying a field as currency code with @Semantics.currencyCode or as unit of
measure with @Semantics.unitOfMeasure). Annotations of this type are specified directly before the
specific field.
In this exercise, we will use annotations for specifying the table buffer settings of a view. We will switch on
the table buffer (status: ACTIVE) and specify the buffering type as Single Records Buffering.
Explanation Screenshot
Code: ZCDSV_ANNOTATION_##
2. Add both annotations to your
coding:
@AbapCatalog.sqlViewName: 'ZCDSV_3_##'
@AbapCatalog.buffering. @AbapCatalog.buffering.status: '#ACTIVE'
status: ‘#ACTIVE’ @AbapCatalog.buffering.type: '#SINGLE'
define view zcdsv_3_annotation_## as select from
@AbapCatalog.buffering. snwd_so
type: ‘#SINGLE’ {
key snwd_so.node_key
};
Explanation Screenshot
Explanation Screenshot
For more information about the annotations, you can put the cursor on an annotation field and
press the key F1 for opening the online help. You will get more information about this and other
possible annotations.
F. Associations
An association joins the first elementary data source entity specified as the initial data source
(after FROM using the ON condition cond_exp) with the data source entity specified as the target data source
(in the definition of the association). A data source entity can be a database table defined in ABAP
Dictionary or a view. In the latter case, the view can be an external view or another CDS view.
Associations on a conceptual level are used for replacing joins with simple path expressions in queries.
Associations define relationships between CDS entities. It is much more convenient to access data from an
underlying data model instead of writing a complex join every time. Also the reuse of a DDL view makes the
life much easier. When a CDS view is activated with path expressions, the specified associations are
converted to join expressions.
In this exercise we will learn how to create CDS views by using path expressions for the existing
association. We will reuse existing CDS view sepm_sddl_product which implements multiple
associations. The focus of this exercise will be consumption of the associations in CDS view via path
expressions and an introduction to CDS view parameters.
On a database that supports this (not all up to now but HANA supports it) you can add importing parameter
to a view that can be used in the SELECT statement of the view. These parameters can be supplied while
using the CDS view in Open SQL.
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
Code: ZCDSV_PROD_##
@AbapCatalog.sqlViewName: 'ZVPROD_00'
@ClientDependent: true
19. Code Snippet @AbapCatalog.compiler.CompareFilter: true
@EndUserText.label: 'Test CDS View for Association'
define view zcdsv_prod_00 as select from sepm_sddl_product
{
sepm_sddl_product.product_id
}
Explanation Screenshot
We have not consumed any
association in this CDS view
yet but let us still see the
preview. Press ALT+F8 from
the source editor or select
Open Data Preview from
context menu in project
explorer.
Code: ZCDSV_PROD_##
@AbapCatalog.sqlViewName: 'ZVPROD_00'
@ClientDependent: true
21. In this step add more fields to @AbapCatalog.compiler.CompareFilter: true
the projection list in a similar @EndUserText.label: 'Test CDS View for Association'
way. define view zcdsv_prod_00 as select from sepm_sddl_product
{
sepm_sddl_product.product_id,
sepm_sddl_product.price,
sepm_sddl_product.currency_code
}
Explanation Screenshot
Code: ZCDSV_PROD_##
@AbapCatalog.sqlViewName: 'ZVPROD_00'
@ClientDependent: true
@AbapCatalog.compiler.CompareFilter: true
@EndUserText.label: 'Test CDS View for Association'
24. Code Snippet using define view zcdsv_prod_00 as select from sepm_sddl_product
association supplier {
sepm_sddl_product.product_id,
sepm_sddl_product.price,
sepm_sddl_product.currency_code,
sepm_sddl_product.supplier.company_name
}
Explanation Screenshot
Explanation Screenshot
Code: ZCDSV_PROD_##
@AbapCatalog.sqlViewName: 'ZVPROD_00'
@ClientDependent: true
29. Let us now add field @AbapCatalog.compiler.CompareFilter: true
product_name.text from the @EndUserText.label: 'Test CDS View for Association'
association product_name define view zcdsv_prod_00 as select from sepm_sddl_product
{
which has the cardinality
sepm_sddl_product.product_id,
[1..*] (one to many).
sepm_sddl_product.price,
sepm_sddl_product.currency_code,
sepm_sddl_product.supplier.company_name
}
Explanation Screenshot
Explanation Screenshot
Code: ZCDSV_PROD_##
@AbapCatalog.sqlViewName: 'ZVPROD_00'
@ClientDependent: true
@AbapCatalog.compiler.CompareFilter: true
33. Let us now define the filter in @EndUserText.label: 'Test CDS View for Association'
the path expression for the define view zcdsv_prod_00 as select from sepm_sddl_product
association product_name. {
[ language = 'E' ]. sepm_sddl_product.product_id,
Specifies the filter for the sepm_sddl_product.price,
sepm_sddl_product.currency_code,
association on the field
sepm_sddl_product.supplier.company_name, // from
language of the target view.
association supplier
Explanation Screenshot
Explanation Screenshot
b. Define a parameter
‘langu’ with type
abap.lang
language = 'E'
with
$parameters.langu
Explanation Screenshot
Code: ZCDSV_PROD_##
@AbapCatalog.sqlViewName: 'ZVPROD_00'
@ClientDependent: true
@AbapCatalog.compiler.compareFilter: true
@EndUserText.label: 'Test CDS View for Association'
define view zcdsv_prod_00
with parameters langu: abap.lang
as select from sepm_sddl_product {
product_id,
price,
currency_code,
39. Note that the CDS view input
supplier.company_name, // from association supplier
parameters can be accessed
via $parameter or ‘:’. product_name[1: language = $parameters.langu ].text,
// from association product_name
Save and activate. Open
Data Preview or ALT+F8 product_name[1: language = 'D' ].text as
fallback_text,
coalesce(
product_name[1: language = $parameters.langu ].text,
product_name[1: language = 'D' ].text ) as
product_text
}
Explanation Screenshot
Explanation Screenshot
Code: ZR_CDS_WITH_PARAM_##
REPORT zr_cds_with_param_tpl.
42. Complete the report with * SELECT * FROM zcdsv_prod_## ( langu = @langu
your CDS View entity. )
* INTO TABLE @DATA(lt_data).
cl_demo_output=>display( ).
Explanation Screenshot
Code: ZR_CDS_WITH_PARAM_##
REPORT zr_cds_with_param_##.
IF cl_abap_dbfeatures=>use_features(
EXPORTING
47. The report will run in the requested_features =
VALUE #( (
current state only for HANA cl_abap_dbfeatures=>views_with_parameters ) ) ).
Database. Hence as the last
step let us add a check for SELECT * FROM zcdsv_prod_sol( langu = @langu )
the CDS View Parameter list ##DB_FEATURE_MODE[VIEWS_WITH_PARAMETERS]
support.
INTO TABLE @lt_data .
Save and Activate.
TRY.
* ** Display the data ***
cl_demo_output=>display( EXPORTING data =
lt_data
name = 'Output of CDS
View').
ENDTRY.
ELSE.
WRITE: 'Database does not support CDS View with
parameter' .
ENDIF.
Explanation Screenshot
Step in AiE
Explanation Screenshot
Summary:
1. How to access the target fields of the CDS View association using path expressions.
5. Consume the CDS View from Open SQL by passing the host variables values to the CDS view
parameters.
6. How to find if the underlying database supports CDS View import parameter feature.