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.
Advanced View Building in SAP NetWeaver AS ABAP 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 ............................... 10
C1. Using Joins ........................................................................................................................................... 10
C2. Using Aggregations .............................................................................................................................. 14
C3. Using Built-In Functions ........................................................................................................................ 19
C4. Using CASE Expressions ..................................................................................................................... 21
D. Using CDS Views in ABAP programs ..................................................................................................... 24
E. Annotations ............................................................................................................................................. 26
F. Associations ............................................................................................................................................ 28
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
Working with Associations
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
@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
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
@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
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.
@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
}
where so_inv.node_key is null
group by bp.company_name, so.currency_code
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, 0, 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 so.currency_code, bp.company_name, 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_CDSV_OUTPUT_##.
Explanation Screenshot
TRY.
**** Display the data ***
cl_demo_output=>display(
4. Remove the comment for
EXPORTING data = lt_data
display the data.
name = 'Output of CDS View'
).
ENDTRY.
Code Snippet: ZR_CDSV_OUTPUT_##
REPORT ZR_CDSV_OUTPUT_##.
TRY.
**** Display the data ***
cl_demo_output=>display( EXPORTING data = lt_data
name = 'Output of CDS View' ).
ENDTRY.
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_annotation_## as select from
@AbapCatalog.buffering. snwd_so
type: ‘#SINGLE’ {
key snwd_so.node_key
};
Explanation Screenshot
F. Associations
In this exercise, we will use CDS views with associations. For a better understanding, we will explain the
CDS view before you start with your exercise. You can find the CDS views in the solution package
TEST_A4H_102_SOLUTIONS.
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.
Explanation
@AbapCatalog.sqlViewName: 'ZVOIA_ASS01'
define view zcdsv_oia_assoc_01_note as select from snwd_texts
{
key node_key as langu_dep_note_key,
parent_key as note_key,
language,
text
}
Explanation
In the CDS view zcdsv_oia_assoc_02_item the sales order item table (SNWD_SO_I) is joined with
CDS view zcdsv_oia_assoc_01_note using the key word
association
You can define also the cardinality of the target data source of a CDS view within the defined association.
association [0..*]
After specifying the association with its cardinality the next is to specify the join to the target source in this
case the DDL View zcdsv_oia_assoc_01_note and
association [0..*] to zcdsv_oia_assoc_01_note as notes
The prefix $projection is used here because an alias name is used in the select list (projection list) for
the column NOTE_GUID.
association [0..*] to zcdsv_oia_assoc_01_note as notes
on $projection.note_key = notes.note_key
{
...
note_guid as note_key,
...
}
The field NOTES in the select list publishes the select list from the view zcdsv_oia_assoc_01_note.
This allows any consumer access to the projection list of this (zcdsv_oia_assoc_01_note) view.
association [0..*] to zcdsv_oia_assoc_01_note as notes
on $projection.note_key = notes.note_key
{
...
notes
}
Explanation
In the CDS view zcdsv_oia_assoc_02_item the sales order item table (SNWD_SO_I) is joined with
CDS view zcdsv_oia_assoc_01_note using the key word
association
You can define also the cardinality of the target data source of a CDS view within the defined association.
association [0..*]
After specifying the association with its cardinality the next is to specify the join to the target source in this
case the DDL View zcdsv_oia_assoc_01_note and
association [0..*] to zcdsv_oia_assoc_01_note as notes
The prefix $projection is used here because an alias name is used in the select list (projection list) for
the column NOTE_GUID.
association [0..*] to zcdsv_oia_assoc_01_note as notes
on $projection.note_key = notes.note_key
{
...
note_guid as note_key,
...
}
The field NOTES in the select list publishes the select list from the view zcdsv_oia_assoc_01_note.
This allows any consumer access to the projection list of this (zcdsv_oia_assoc_01_note) view.
association [0..*] to zcdsv_oia_assoc_01_note as notes
on $projection.note_key = notes.note_key
{
...
notes
}
Explanation
// public association items ( can be used in other CDS views on this view)
items,
}
The CDS view zcdsv_oia_assoc_03_so uses CDS Views zcdsv_oia_assoc_02_item and
zcdsv_oia_assoc_01_note.
With the path expression items.notes[ language = 'E' ].text it is possible to filter the data to
the text with language English.
By activating the CDS view, the path expression is converted to a join expression.
{
...
// association can also be used here
items.notes[ language = 'E' ].text as english_so_item_notes,
...
}
Remark: By activating the CDS view the associations will be converted into join conditions.
In the following exercise we use the CDS View zcdsv_oia_assoc_03_so and access to the fields from
the underlying view. You will see in which way you can access to the field of the associations that we
described above.
Explanation Screenshot
Explanation Screenshot
Summary:
After this exercise, you are now familiarized with associations in CDS Views. You know where the
differences to the Joins are and how to use the association in a CDS View.