Вы находитесь на странице: 1из 49

CODEJAM: ABAP FOR SAP HANA

3 – ADVANCED VIEW BUILDING IN SAP


NETWEAVER AS ABAP

June 2015 – SAP SE

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

A. What’s inside this exercise?


The Core Data Services (CDS) provide a collection of domain-specific languages and services for defining
and consuming semantically rich data models In SAP HANA.

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).

Core Data Services

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

ABAP for SAP HANA


3

· Working with Associations

B. Create a simple CDS View

Estimated total time: 15 minutes

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

1. Start eclipse and/or the ABAP


Development Tools

2. Open the ABAP perspective.

3. Go to the Project Explorer and


navigate to your package
TEST_A4H_EX_## (where ##
represents your group number).

4. Create a new DDL Source


entity.

Right-click your package and


select the context menu item
Other ABAP Repository
Object.

ABAP for SAP HANA


4

Explanation Screenshot

5. Choose the DDL Source entry


in the dialog.

6. In the following dialog, fill in the


name ZCDSV_1_## (where ##
represents your group number)
and the description of your new
DDL Source object.

Press Next.

ABAP for SAP HANA


5

Explanation Screenshot

7. In the following dialog press


Finish.

8. Start your Implementation:


Begin with the statement
DEFINE VIEW followed by the
name of the CDS view entity
zcdsv_simple_1_## (where
## represents your group
number) and a simple define view zcdsv_simple_1_00 as select from snwd_so as so
AS SELECT FROM statement
with the table SNWD_SO.

Specify the alias SO for the


table SNWD_SO.

9. Define the first field SO.SO_ID


of the select list between curly
brackets and mark it as key
field using the keyword KEY.

You can make use the code


completion feature
(CTRL+SPACE).

ABAP for SAP HANA


6

Explanation Screenshot

10. Enhance the select list with


following fields:
· CURRENCY_CODE define view zcdsv_simple_1_sol as select from snwd_so as
· GROSS_AMOUNT so
{
· CREATED_AT key so.so_id as sales_order_id,
so.currency_code,
Use the alias SO as prefix. so.gross_amount,
so.created_at
Remark: A separator comma }
is required between the fields

11. An error message occurs.


Hover your mouse over the
error icon in order to read the
long error message.

Remark: When defining a


CDS view, the name of the
associated SQL view must be
specified. This is done using a
specific annotation.

ABAP for SAP HANA


7

Explanation Screenshot
12. Add the missing SQL view
name to the CDS view
Definition.

Set the cursor to the


underlined statement View
(or click on the warning icon)
and use the Quick Fix feature
(CTRL+1) for the correction.

The Editor will create an


annotation with the name of
the associated SQL view.

If necessary, replace the


proposed sqlViewName with
ZCDSV_1_## (where ##
represents your group
number).

Remark: The usual ABAP


Dictionary rules apply to this
name and it is not case-
sensitive (it is transformed
internally into uppercase
letters). The associated SQL
view is created under this
name on the database.

Code Snippet: ZCDSV_SIMPLE_1_##

@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
}

13. Save your DDL Source object

14. Check and activate it

ABAP for SAP HANA


8

Explanation Screenshot
15. After the activation was
successful, an additional
ABAP Dictionary object will be
generated: It is the associated
DDL SQL view.

You can have a look at it in


the Views folder.

You may need to refresh your


package in order to see the
object. For that, select your
package and press F5.

Double-click on your view


ZCDSV_1_## (where ##
represents your group
number).

16. The ABAP Data Dictionary


(SE11) is now open and you
can see the generated
Dictionary entity.

Remark 1: The DDL SQL


view is fully managed by the
ABAP Dictionary. It is not
editable and all semantic
information maintained in the
DDL Source is not available
here.

Remark 2: The client field


MANDT was generated
automatically.

ABAP for SAP HANA


9

Explanation Screenshot

17. If you want, you can check the


generated SQL Source.

Just select menu path


Menu > Utilities >
Database Object >
Display

The definition of the database


view is displayed in the
corresponding pop-up
window.

18. Display the Data Preview.

Go back to your DDL Source


(ALT+LEFT) and right-click
your ABAP DDL Source in the
Project Explorer and select
the context menu item Open
Data Preview.

ABAP for SAP HANA


10

Explanation Screenshot

19. The data preview will look like


the screenshot below

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

ABAP for SAP HANA


11

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:

C1. Using Joins

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.

ABAP for SAP HANA


12

Explanation Screenshot

1. Create a new DDL Source.

Right-click the Dictionary


folder and select the context
menu item New > DDL
Source.

2. In the dialog, fill in the name


ZCDSV_2_## (where ##
represents your group
number) and maintain a
description (e.g. CDS View
with Join Tables) for
your new DDL Source
object.

ABAP for SAP HANA


13

Explanation Screenshot

3. In the following dialog press


Finish.

4. Start your Implementation:

Begin with the statement


DEFINE VIEW followed by the
name of the CDS view entity
zcdsv_multiple_features
_2_## (where ## represents
your group number) and a define view zcdsv_multiple_features_2_##
simple as select from snwd_so as so
AS SELECT FROM statement inner join snwd_bpa as bp
with the table SNWD_SO. on so.buyer_guid = bp.node_key
left outer join snwd_so_inv_head as so_inv
Specify the alias SO for the table on so.node_key = so_inv.so_guid
SNWD_SO.

Add the INNER JOIN and


LEFT OUTER JOIN conditions
as described below.

ABAP for SAP HANA


14

Explanation Screenshot
5. Define select list between curly
brackets after the Join
statements.

use following field


· SO_ID (mark it as key field)
· COMPANY_NAME
· CURRENCY_CODE
· GROSS_AMOUNT

Remark: The field


COMPANY_NAME is from the
table SNWD_BPA and
requires the alias BP. The
other fields are from the
table SNWD_SO and required
the alias SO.
6. Set the cursor to the underlined
view name and use the Quick
Fix feature (CTRL+1) for the
correction.

The Editor will add the required


sqlViewName annotation.

If necessary replace the


proposed sqlViewName value
with ZCDSV_2_## (where ##
represents your group
number).

Code Snippet: 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 so.so_id,
bp.company_name,
so.currency_code,
so.gross_amount
}

ABAP for SAP HANA


15

Explanation Screenshot

7. Save your DDL Source object.

8. Check and activate it.

9. After the activation was


successful, open the Data
Preview.

ABAP for SAP HANA


16

Explanation Screenshot

10. The result will look like the


screenshot below

C2. Using Aggregations

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.

ABAP for SAP HANA


17

Explanation Screenshot
1. Calculate the sum of the gross
mount from the table SNWD_SO.

In the select list, replace the


field so.gross_amount with
SUM(so.gross_amount)
and specified sum(so.gross_amount) as sum_gross_amount
sum_gross_amount as alias .

Remark: An alias is required


for the result of an aggregate
function.
2. An error is displayed in your
CDS view definition.

Hover your mouse over the


error icon in order to read the
long error message.

Remark: If you use


aggregation functions in a
CDS view definition, then all
selected fields except those
used in aggregate functions
have to be specified in the
GROUP BY clause.
3. Add the GROUP BY clause in
your coding.
group by so.so_id, bp.company_name, so.currency_code
Hint: You can use the Quick
Fix (CTRL+1) feature to create
the GROUP BY list.
4. Your CDS view should only
retrieve those sales orders
where no invoice exists.

Add a WHERE condition after


where so_inv.node_key is null
the select list and before the
GROUP BY clause in order to
exclude the sales orders with
invoices.

ABAP for SAP HANA


18

Explanation Screenshot

Code Snippet: 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 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

5. Save your DDL Source object.

6. Check and activate it.

7. After the activation was


successful, open the Data
Preview

ABAP for SAP HANA


19

Explanation Screenshot

8. The result will look like the


screenshot below.

Remark: As you might see in


the data preview, it does not
make sense to group over the
sales order id which is a unique
key. Instead, we will group the
result by the company name in
order to have the sum of all
sales orders from a customer.

9. Delete the key field SO_ID from your select list and from the GROUP BY clause.

Mark COMPANY_NAME as key field.

Code Snippet: 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
}
where so_inv.node_key is null
group by bp.company_name, so.currency_code

10. Save your DDL Source


object.

11. Check and activate it.

ABAP for SAP HANA


20

Explanation Screenshot

12. After the activation was


successful, open the Data
Preview.

13. The result will now look like the


screenshot.

ABAP for SAP HANA


21

C3. Using Built-In Functions


The CDS DDL syntax provides the built-in string functions SUBSTRING and LDAP and the built-in arithmetic
functions CEIL and MOD.
In this exercise, we will enhance the select list of the CDS view defined in the exercise C2 with a 10
character short name of the company name.

Explanation Screenshot

1. Add the SUBSTRING


statement to your CDS view
definition.

Specified the alias


SHORT_NAME for the result of
the built-in function. substring( bp.company_name, 1, 10 ) as short_name

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

14. Save your DDL Source


object.

15. Check and activate it.

ABAP for SAP HANA


22

Explanation Screenshot

16. After the activation was


successful, open the Data
Preview.

17. The result will look like the


screenshot.

ABAP for SAP HANA


23

C4. Using CASE Expressions


The CDS DDL syntax supports CASE expressions. In this exercise, we will use this feature to provide better
readable information about the delivery status of a sale order. The delivery status is defined in the Data
Dictionary as character field with length 1.

Explanation Screenshot

1. Add the CASE expression


shown in the Screenshot.

You can insert it at the end


of the select list.

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.

Remark: An alias is required


for the result of a CASE
expression.

2. Save your DDL Source


object.

3. Check and activate it.

ABAP for SAP HANA


24

Explanation Screenshot

Code Snippet: 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,

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

4. After the activation was


successful, open the Data
Preview.

ABAP for SAP HANA


25

Explanation Screenshot

5. The final result will look like the


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.

ABAP for SAP HANA


26

D. Using CDS Views in ABAP programs

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.

* *** Implement your SQL Query ***


* SELECT *
* FROM
1. Open report * INTO
ZR_CDSV_OUTPUT_##
(where ## represents your
group number) in your ** *** Write and display the data ***
package. *cl_demo_output=>write_data( name = 'Exercise D -
Using CDS Views in ABAP Programs'
* value = lt_data ).
cl_demo_output=>display( ).

2. Replace the select statement


with your CDS View:
zcdsv_multiple_features_2_##
(where ## represents your
group number) * *** Implement your SQL Query ***
SELECT *
Remark: You may wonder FROM zcdsv_multiple_features_2_sol
about the “@” in the INTO INTO TABLE @DATA(lt_data).
TABLE clause… stay tuned for
the OpenSQL CodeJam
exercise.

** *** Write and display the data ***


3. Remove the comment for cl_demo_output=>write_data( name = 'Exercise D - Using
display the data. CDS Views in ABAP Programs'
value = lt_data ).
cl_demo_output=>display( ).

ABAP for SAP HANA


27

Explanation Screenshot

Code Snippet: ZR_CDSV_OUTPUT_##

REPORT zr_cds_output_sol.

* *** Implement your SQL Query ***


SELECT *
FROM zcdsv_multiple_features_2_sol
INTO TABLE @DATA(lt_data).

** *** Write and display the data ***


cl_demo_output=>write_data( name = 'Exercise D - Using CDS Views in ABAP Programs'
value = lt_data ).
cl_demo_output=>display( ).

4. Save your ABAP Report.

5. Check and activate it.

6. Run the report (F8) and


check the result.

E. Annotations

Annotations are used to enrich data models with additional metadata. They start with @ in the CDS view
definition.

ABAP for SAP HANA


28

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.

Estimated time: 10 minutes

Explanation Screenshot

1. Open the CDS View: Code: ZCDSV_3_ANNOTATION_##


ZCDSV_ANNOTATION_##
(where ## represents your @AbapCatalog.sqlViewName: 'ZCDSV_3_##'
group number) in your define view zcdsv_3_annotation_## as select from
package. snwd_so
{
Remark: For using the table key snwd_so.node_key
buffer, we have to select the };
key field of the database table.

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
};

3. Save your DDL Source


object.

4. Check and activate it

ABAP for SAP HANA


29

Explanation Screenshot

5. Now, you can navigate to the


ABAP dictionary and check
the buffering value of the
associated SQL view
ZCDSV_3_##.

Put the cursor on the


sqlViewName and press F3 to
navigate to the view definition in
SE11 or open the view in the
folder Views by double-clicking

After that, navigate to the


technical settings via Menu >
GoTo > Technical
Settings and check the
buffering settings on the
General Properties panel.

ABAP for SAP HANA


30

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.

ABAP for SAP HANA


31

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.

Estimated time: 10 minutes

Explanation Screenshot

11. Create a new DDL Source.

Right-click the package


TEST_A4H_EX_XX and
select the context menu item
New > Other ABAP
Repository Object.

12. A dialog for ABAP


Repository Object comes up.
Type filter text ‘DDL’ in the
filter text area. This shows
the dictionary folder and
the DDL Source. Select
DDL Source and click on
Next

ABAP for SAP HANA


32

Explanation Screenshot

13. In the dialog, fill in the name


ZCDSV_PROD_## (where ##
represents your group
number) and maintain a
description (e.g. Test CDS
View for Association)
for your new DDL Source
object.

14. In the following dialog select


the template for the Define
View with Parameters
and press Finish.

15. Change the template variable


sql_view_name to
ZVPROD_XX.

ABAP for SAP HANA


33

Explanation Screenshot

16. Specify the name for the


data_source_name as
sepm_sddl_product.
sepm_sddl_product is view
with a number of associations
defined which we will use
throughout this exercise.

17. Select sepm_sddl_product


in the source code and press
F2. This shows the description
of the CDS view
sepm_sddl_product. You
can see the attributes and
associations available in this
view.
We will use the association
product_name and
supplier in this exercise.

ABAP for SAP HANA


34

Explanation Screenshot

18. Now let us try to add some


attributes to the projection list
of our new CDS view
ZCDSV_PROD_##. Inside the
curly braces enter ‘pro’ and
hit CTRL+SPACE BAR for
content assist. Then select
product_id.

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
}

20. Save the CDS DDL source


(CTRL+S), check (CTRL+F2)
and activate (CTRL + F3).

ABAP for SAP HANA


35

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
}

22. In this step we will access the


available association supplier
from the CDS view
sepm_sddl_product. In the
projection list type ‘sup’ and
hit CTRL+SPACE BAR. Select the
association supplier.

ABAP for SAP HANA


36

Explanation Screenshot

23. Then enter a ‘.’ and


again invoke content assist by
CTRL+SPACE BAR, select
company_name
attribute/column.

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
}

25. Save the CDS DDL Code: ZCDSV_PROD_##


source (CTRL+S), check
(CTRL+F2) and activate
@AbapCatalog.sqlViewName: 'ZVPROD_00'
(CTRL + F3). At this point we @ClientDependent: true
have successfully consumed @AbapCatalog.compiler.CompareFilter: true
the association supplier @EndUserText.label: 'Test CDS View for Association'
through this path expression: define view zcdsv_prod_00 as select from sepm_sddl_product
supplier.company_name. {
sepm_sddl_product.product_id,
Note: the company_name sepm_sddl_product.price,
attribute comes from the sepm_sddl_product.currency_code,
association supplier defined in sepm_sddl_product.supplier.company_name
CDS view }
sepm_sddl_product.

ABAP for SAP HANA


37

Explanation Screenshot

26. You can quickly navigate


to the source CDS view
sepm_sddl_product by
selecting the view name in
define statement and pressing
F3. You can then inspect the
definition of the association
supplier

27. Check the definition of


the associations’ supplier and
product_name defined in the
CDS view
sepm_sddl_product.

Note the cardinality for the


association. For supplier the
cardinality of the association
is [1], which means for a
product there can be relation
with one supplier. Likewise
for the association
product_name the cardinality
for the association is [1..*],
which means for a product
there can be one or more than
one product name in different
languages.

Press ALT + Left Arrow Key


to navigate back to CDS view
definition.

ABAP for SAP HANA


38

Explanation Screenshot

28. You have now reached the


first milestone in this
exercise successfully
consuming the association
supplier with the path
expression
supplier.company_name.

Let us preview the CDS view


again. Save , check and
activate. Then press ALT+F8
or Open Data Preview.

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
}

ABAP for SAP HANA


39

Explanation Screenshot

30. A warning is shown by the


compiler due to the
cardinality of the association
which is one to many [1..*].

31. Ignore the warning, save and


activate. Then Open Data
Preview or ALT+F8.

Note that there are duplicate


entries for the same product
where only the text differs in
language.

32. As we saw earlier CDS view


sepm_sddl_product has an
association with another
CDS view
sepm_sddl_text_data
defined by association
product_name . We will now
define a filter for the
association procuct_name
by restricting the association
on sepm_sddl_text_data by
its field language

ABAP for SAP HANA


40

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

sepm_sddl_product.product_name[ language = 'E' ].text


// from association product_name
}

34. Now that you have restricted


the association
product_name cardinality you
can remove the warning by
specifically indicating the
complier that this association
is one to one association by
specifying 1: before filter.

35. Save, activate and Open


Data Preview or ALT + F8.

You can now see that the


duplicates have been
eliminated based on the
association filter for
language. You can also see
that some of the rows do not
have text defined for
language English.

You have now reached the


second milestone in this
exercise where you have
successfully consumed an
association using path
expression and you also
know how to use filter in the
association.

ABAP for SAP HANA


41

Explanation Screenshot

36. Let us add more fields as


fallback_text and Code: ZCDSV_PROD_##
product_text. For this we
will use the coalesce @AbapCatalog.sqlViewName: 'ZVPROD_00'
function in the CDS view to @ClientDependent: true
provide the product_text as @AbapCatalog.compiler.CompareFilter: true
shown. @EndUserText.label: 'Test CDS View for Association'
define view zcdsv_prod_00 as select from sepm_sddl_product
{
The coalesce function product_id,
returns the value of the price,
argument arg1 (if this is not currency_code,
the null value); otherwise it supplier.company_name, // from association supplier
returns the value of the
argument arg2. In the product_name[1: language = 'E' ].text // from
example shown if the association product_name
association with filter as product_name[1: language = 'D' ].text as
language = 'E' returns fallback_text,
nothing then the coalesce(
product_name[1: language = 'E' ].text ,
fallback_text gets the value
product_name[1: language = 'D' ].text ) as
from association where
product_text
language = 'D'
}

37. Save, activate and Open


Data Preview or ALT + F8.

You can see wherever text is


empty it product_text is
filled with the German
translation of the text if it
exist.

You have now reached the


third milestone of the
exercise. Now you know how
to use coalesce function in
the CDS views.

ABAP for SAP HANA


42

Explanation Screenshot

38. In this step we will use the


view parameters which is
supported in CDS view to
dynamically pass the filter
language for the association
product_name.

a. Type ‘with’ after


view name and
invoke content assist
( CTRL+spacebar )
select with
parameters and
complete the code as
shown in the code
snippet

b. Define a parameter
‘langu’ with type
abap.lang

c. Replace the filter


value

language = 'E'

with

$parameters.langu

ABAP for SAP HANA


43

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
}

ABAP for SAP HANA


44

Explanation Screenshot

40. The dialog for input


parameters for CDS view
appears. Enter the value ‘E’
and press enter twice. The
data preview is display.

41. You have reached the fourth


milestone in the exercise
and you know how to define
and use an input parameter
in a CDS view.

Now that you have the CDS


view with input parameter let
us consume this from the
Open SQL. Expand the
exercise package for your
group TEST_A4H_EX_##. Open
the report
ZR_CDS_WITH_PARAM_##. You
could as well use the
shortcut CTRL+SHIFT+A to
open the report
ZR_CDS_WITH_PARAM_##.

ABAP for SAP HANA


45

Explanation Screenshot

Code: ZR_CDS_WITH_PARAM_##

REPORT zr_cds_with_param_tpl.

PARAMETERS : langu TYPE spras.

* *** Implement your SQL Query ***

42. Complete the report with * SELECT * FROM zcdsv_prod_## ( langu = @langu
your CDS View entity. )
* INTO TABLE @DATA(lt_data).

** *** Write and display the data ***


*cl_demo_output=>write_data( name = 'Exercise D -
Using CDS Views with Parameter!'
* value = lt_data ).

cl_demo_output=>display( ).

43. Once you finish changing the


code with your entities. Save
and Check

A warning marker appears at


the left margin of the editor.
Hover over the warning for
details.

Note: host variables can be


passed to importing
parameters using @ variable
name.

44. Check the problems view for


the same warning.

Select the problem view


entry for this warning, then
do right click, then select the
context menu option Show
ABAP Problem Help.

ABAP for SAP HANA


46

Explanation Screenshot

45. The ABAP Problem Help


view is rendered. Read
through and we get a hint of
how to
check(cl_abap_dbfeatur
es=>use_features )
whether the parameter for
the CDS Views is supported
for the DB underneath
application server( AS ABAP
).

46. The warning can be


suppressed by using the
pragma
##DB_FEATURE_MODE[VIE
WS_WITH_PARAMETERS]

Code: ZR_CDS_WITH_PARAM_##

REPORT zr_cds_with_param_##.

* *** Declare your variable ***


DATA: lt_data TYPE TABLE OF zcdsv_prod_sol.

PARAMETERS : langu TYPE spras .

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.

ABAP for SAP HANA


47

Explanation Screenshot

Step in AiE

48. Optional Step: We will define


the text element for the
selection parameter langu.
Select langu in the editor Step in SAP GUI integrated in AiE
and do Right Click to
launch context menu. Go to
Open Othersè Text
Elements. This will
open the SAP GUI
editor for Text
Element. Maintain a text
that will be shown when the
report is run . Save and
Activate.

ABAP for SAP HANA


48

Explanation Screenshot

49. Now we are ready to test the


consumption of the CDS
View with parameter via this
this report
ZTEST_CDS_PARAM_##.
Open the report and run via
ALT+F8. The following
screen is rendered. Select
the country as DE and test.

50. The result of the report is as


shown. You have now
successfully consumed CDS
view with importing
parameters in Open SQL.

You have now reached the


final milestone in this
exercise and you know how
to use a CDS view with
parameters in Open SQL.

Summary:

In this exercise, you learned:

1. How to access the target fields of the CDS View association using path expressions.

2. How to specify filter values for the associations in path expressions.

3. How to use coalesce function in CDS view.

4. How to define and access input parameters in a CDS view.

ABAP for SAP HANA


49

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.

ABAP for SAP HANA

Вам также может понравиться