Anda di halaman 1dari 34

CODEJAM: ABAP FOR SAP HANA

3 – ADVANCED VIEW BUILDING IN SAP


NETWEAVER AS ABAP

February 2014 – SAP AG

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

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
 Working with Associations

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 3

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 the SAP HANA Studio


ABAP Development Tools via
the desktop shortcut (or via the
hdbstudio.exe in your
installation folder)

2. Open the ABAP perspective.

3. Go to the Project Explorer and


navigate to your package
TEST_A4H_102_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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 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.

7. In the following dialog press


Finish.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 5

Explanation Screenshot

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

10. Enhance the select list with


following fields:
 CURRENCY_CODE define view zcdsv_simple_1_00 as select from snwd_so as so
 GROSS_AMOUNT {
 CREATED_AT key so.so_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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 6

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

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 7

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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 8

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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 9

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

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 10

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.

Explanation Screenshot

1. Create a new DDL Source.

Right-click the Dictionary


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

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 11

Explanation Screenshot

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.

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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 12

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
}

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 13

Explanation Screenshot

7. Save your DDL Source object.

8. Check and activate it.

9. After the activation was


successful, open the Data
Preview.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 14

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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 15

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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 16

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

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 17

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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 18

Explanation Screenshot

12. After the activation was


successful, open the Data
Preview.

13. The result will now look like the


screenshot.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 19

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, 0, 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, 0, 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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 20

Explanation Screenshot

16. After the activation was


successful, open the Data
Preview.

17. The result will look like the


screenshot.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 21

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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 22

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, 0, 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 so.currency_code, bp.company_name, so.delivery_status

4. After the activation was


successful, open the Data
Preview.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 23

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.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 24

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_CDSV_OUTPUT_##.

* *** Declare your variable ***


* e.g DATA: lt_data TYPE table of zcdsv_simple_##.
1. Open report
ZR_CDSV_OUTPUT_## * *** Implement your SQL Query ***
(where ## represents your * e.g.Select * from zcdsv_simple_##
group number) in your into table @lt_data.
package.
Try.
*** Display the data ***
* cl_demo_output=>display( EXPORTING data = lt_data
* name = 'Output of CDS
View').
ENDTRY.

2. Replace the data description


with your CDS View * *** Declare your variable ***
zcdsv_multiple_features_2_## DATA: lt_data TYPE
(where ## represents your table of zcdsv_multiple_features_2_##.
group number).

3. Replace the select statement


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

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 25

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_##.

* *** Declare your variable ***


DATA: lt_data TYPE table of zcdsv_multiple_features_2_##.

* *** Implement your SQL Query ***


Select * from zcdsv_multiple_features_2_## into table @lt_data.

TRY.
**** Display the data ***
cl_demo_output=>display( EXPORTING data = lt_data
name = 'Output of CDS View' ).

ENDTRY.

5. Save your ABAP Report.

6. Check and activate it.

7. Run the report (F8) and


check the result.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 26

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.

Estimated time: 10 minutes

Explanation Screenshot

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


ZCDSV_ANNOTATION_##
(where ## represents your @AbapCatalog.sqlViewName: 'ZCDSV_3_##'
group number) in your define view zcdsv_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_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

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 27

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

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 28

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.

Estimated time: 10 minutes

Explanation

Code Snippet: ZCDSV_OIA_ASSOC_01_NOTE

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

The CDS View zcdsv_oia_assoc_01_note is a simple view of the table snwd_texts.

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 29

Explanation

Code Snippet: ZCDSV_OIA_ASSOC_02_ITEM


@AbapCatalog.sqlViewName: ‘ZVOIA_ASS02’
define view zcdsv_oia_assoc_02_item as
select from snwd_so_i
association [0..*] to zcdsv_oia_assoc_01_note as notes
on $projection.note_key = notes.note_key
{
key node_key as sales_order_item_key,
parent_key as sales_order_key,
note_guid as note_key,
notes
}

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
}

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 30

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
}

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 31

Explanation

Code Snippet: ZCDSV_OIA_ASSOC_03_SO


@AbapCatalog.sqlViewName: 'ZVOIA_ASS03'
define view zcdsv_oia_assoc_03_so as
select from snwd_so
association [0..*] to zcdsv_oia_assoc_02_item as items
on node_key = items.sales_order_key
association [0..*] to zcdsv_oia_assoc_01_note as so_notes
on $projection.note_key = so_notes.note_key
{
node_key as sales_order_guid,

// public association items ( can be used in other CDS views on this view)
items,

// association can also be used here


items.notes[ language = 'E' ].text as english_so_item_notes,

so_notes[ language = 'E' ].text as english_so_note_text,

// must be included for the association to note


snwd_so.note_guid as note_key

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

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 32

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

Code Snippet: ZCDSV_ASSOCIATION_##


1. In the following exercise we use the
CDS View
zcdsv_oia_assoc_03_so and @AbapCatalog.sqlViewName: 'ZCDSV_4_##'
access to the fields from the define view zcdsv_association_##
underlying view. You will see in as select from zcdsv_oia_assoc_03_so
which way you can access to the {
field of the associations that we note_key
described above. }

2. Add the field


items.notes.langu_dep_note_
key to your select list.

Similarly, add the field


english_so_item_notes to your
select list. Use the Code completion
feature (CTRL+SPACE).

Code Snippet: ZCDSV_ASSOCIATION_##

@AbapCatalog.sqlViewName: ' ZCDSV_4_##'


define view zcdsv_association_## as select from zcdsv_oia_assoc_03_so
{
note_key,
items.notes.langu_dep_note_key,
zcdsv_oia_assoc_03_so.english_so_item_notes
}

3. Save your DDL Source object.

4. Check and activate it

CodeJam: ABAP for SAP HANA


Advanced View Building in SAP NetWeaver AS ABAP 33

Explanation Screenshot

5. After the activation was successful,


open the Data Preview.

For that open the context menu of


your DDL Source object and click on
Open Data Preview.

6. The final result will look like the


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.

CodeJam: ABAP for SAP HANA


© 2013 by SAP AG. All rights reserved.
SAP and the SAP logo are registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo are trademarks or
registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company.Sybase and the Sybase logo are registered trademarks of Sybase Inc.
Sybase is an SAP company such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

Anda mungkin juga menyukai