codejam exercise 3 cds

Upload: gurushantha-doddamani

Post on 06-Jul-2018

222 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/17/2019 CodeJam Exercise 3 CDS

    1/34

    CODEJAM: ABAP FOR SAP HANA

    3 – ADVANCED VIEW BUILDING IN SAPNETWEAVER AS ABAP

    February 2014 – SAP AG

    This document outlines our general product direction and should not be relied on in making a purchase decision. Thisdocument is not subject to your license agreement or any other agreement with SAP. SAP has no obligation to pursueany course of business outlined in this document or to develop or release any functionality mentioned in thisdocument. This document and SAP's strategy and possible future developments are subject to change and may bechanged 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 particularpurpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this document, except if suchdamages were caused by SAP intentionally or grossly negligent.

  • 8/17/2019 CodeJam Exercise 3 CDS

    2/34

    Advanced View Building in SAP NetWeaver AS ABAP 2

    CodeJam: ABAP for SAP HANA

    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 definingand consuming semantically rich data models In SAP HANA.

    The domain-specific languages (DSLs) are based on entity-relationship models for defining and accessingcommon core models and mainly comprise a Data Definition Language (DDL), a Query Language (QL), aData Manipulating Language (DML) and a Data Control Language (DCL).

    From an ABAP developer’s point of view you may now ask, what’s in there for us? So, the basic Idea behindthe integration of CDS into the ABAP server is to support code pushdown to the database layer and tosimplifying 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 viewsin 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

    Core Data Services

    DDL QL

    DML DCL

  • 8/17/2019 CodeJam Exercise 3 CDS

    3/34

    Advanced View Building in SAP NetWeaver AS ABAP 3

    CodeJam: ABAP for SAP HANA

    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 editorand the DDL syntax for creating CDS views.

    Explanation Screenshot

    1. Start the SAP HANA Studio ABAP Development Tools viathe desktop shortcut (or via thehdbstudio.exe in yourinstallation folder)

    2. Open the ABAP perspective.

    3. Go to the Project Explorer andnavigate to your packageTEST_A4H_102_EX_## (where## represents your groupnumber).

    4. Create a new DDL Sourceentity.

    Right-click your package and

    select the context menu itemOther ABAP RepositoryObject .

  • 8/17/2019 CodeJam Exercise 3 CDS

    4/34

    Advanced View Building in SAP NetWeaver AS ABAP 4

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    5. Choose the DDL Source entryin the dialog.

    6. In the following dialog, fill in thename ZCDSV_1_## (where ## represents your group number)and the description of your newDDL Source object.

    Press Next .

    7. In the following dialog pressFinish .

  • 8/17/2019 CodeJam Exercise 3 CDS

    5/34

    Advanced View Building in SAP NetWeaver AS ABAP 5

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    8. Start your Implementation:

    Begin with the statementDEFINE VIEW followed by the

    name of the CDS view entityzcdsv_simple_1_## (where## represents your groupnumber) and a simpleAS SELECT FROM statementwith the table SNWD_SO.

    Specify the alias SO for thetable SNWD_SO.

    define view zcdsv_simple_1_00 as select from snwd_so as so

    9. Define the first field SO.SO_ID of the select list between curlybrackets and mark it as keyfield using the keyword KEY.

    You can make use the codecompletion feature(CTRL+SPACE ).

    10. Enhance the select list withfollowing fields: CURRENCY_CODE GROSS_AMOUNT CREATED_AT

    Use the alias SO as prefix.

    Remark : A separator commais required between the fields

    define view zcdsv_simple_1_00 as select from snwd_so as so{

    key so . so_id , so . currency_code , so . gross_amount , so . created_at

    }

    11. An error message occurs.Hover your mouse over theerror icon in order to read thelong error message.

    Remark : When defining aCDS view, the name of theassociated SQL view must bespecified. This is done using aspecific annotation.

  • 8/17/2019 CodeJam Exercise 3 CDS

    6/34

    Advanced View Building in SAP NetWeaver AS ABAP 6

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    12. Add the missing SQL viewname to the CDS viewDefinition.

    Set the cursor to theunderlined statement View (or click on the warning icon)

    and use the Quick Fix feature(CTRL+1 ) for the correction.

    The Editor will create anannotation with the name ofthe associated SQL view.

    If necessary, replace the

    proposed sqlViewName withZCDSV_1_## (where ## represents your groupnumber).

    Remark : The usual ABAPDictionary rules apply to thisname and it is not case-sensitive (it is transformedinternally into uppercaseletters). The associated SQLview is created under thisname 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

  • 8/17/2019 CodeJam Exercise 3 CDS

    7/34

    Advanced View Building in SAP NetWeaver AS ABAP 7

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    15. After the activation wassuccessful, an additional

    ABAP Dictionary object will begenerated: It is the associatedDDL SQL view.

    You can have a look at it inthe Views folder.

    You may need to refresh yourpackage in order to see theobject. For that, select yourpackage and press F5 .

    Double-click on your view

    ZCDSV_1_## (where ##represents your groupnumber).

    16. The ABAP Data Dictionary(SE11 ) is now open and youcan see the generatedDictionary entity.

    Remark 1: The DDL SQL

    view is fully managed by the ABAP Dictionary. It is noteditable and all semanticinformation maintained in theDDL Source is not availablehere.

    Remark 2: The client fieldMANDT was generatedautomatically.

  • 8/17/2019 CodeJam Exercise 3 CDS

    8/34

    Advanced View Building in SAP NetWeaver AS ABAP 8

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    17. If you want, you can check thegenerated SQL Source.

    Just select menu pathMenu > Utilities >Database Object >Display

    The definition of the databaseview is displayed in thecorresponding pop-upwindow.

    18. Display the Data Preview.

    Go back to your DDL Source(ALT+LEFT) and right-clickyour ABAP DDL Source in theProject Explorer and selectthe context menu item OpenData Preview .

  • 8/17/2019 CodeJam Exercise 3 CDS

    9/34

    Advanced View Building in SAP NetWeaver AS ABAP 9

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    19. The data preview will look likethe 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

  • 8/17/2019 CodeJam Exercise 3 CDS

    10/34

    Advanced View Building in SAP NetWeaver AS ABAP 10

    CodeJam: ABAP for SAP HANA

    C. Create a CDS View using Aggregations, built-in Functions and CASE ExpressionsThis 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 andbusiness 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 theBusiness Partner table ( SNWD_BPA) for the information about the company names. We will join both tableson 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 SalesOrder Invoice Header table ( SNWD_SO_INV_HEAD). We will join the tables on field NODE_KEY from theSales Order table and the field SO_GUID from the Invoice Header table.

    Explanation Screenshot

    1. Create a new DDL Source.

    Right-click the Dictionaryfolder and select the contextmenu item New > DDLSource .

  • 8/17/2019 CodeJam Exercise 3 CDS

    11/34

  • 8/17/2019 CodeJam Exercise 3 CDS

    12/34

    Advanced View Building in SAP NetWeaver AS ABAP 12

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    5. Define select list between curlybrackets after the Joinstatements.

    use following field SO_ID (mark it as key field) COMPANY_NAME CURRENCY_CODE GROSS_AMOUNT

    Remark: The fieldCOMPANY_NAMEis from the

    table SNWD_BPA andrequires the alias BP. Theother fields are from thetable SNWD_SO and requiredthe alias SO.

    6. Set the cursor to the underlinedview name and use the QuickFix feature ( CTRL+1 ) for thecorrection.

    The Editor will add the requiredsqlViewName annotation.

    If necessary replace theproposed sqlViewName valuewith ZCDSV_2_## (where ## represents your groupnumber).

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

    }

  • 8/17/2019 CodeJam Exercise 3 CDS

    13/34

    Advanced View Building in SAP NetWeaver AS ABAP 13

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    7. Save your DDL Source object.

    8. Check and activate it.

    9. After the activation wassuccessful, open the DataPreview.

  • 8/17/2019 CodeJam Exercise 3 CDS

    14/34

    Advanced View Building in SAP NetWeaver AS ABAP 14

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    10. The result will look like thescreenshot 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 groupnumber) created in the exercise C1.

  • 8/17/2019 CodeJam Exercise 3 CDS

    15/34

    Advanced View Building in SAP NetWeaver AS ABAP 15

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    1. Calculate the sum of the grossmount from the table SNWD_SO.

    In the select list, replace thefield so.gross_amount withSUM(so.gross_amount)and specifiedsum_gross_amount as alias .

    Remark : An alias is requiredfor the result of an aggregatefunction.

    sum( so .gross_amount ) as sum_gross_amount

    2. An error is displayed in yourCDS view definition.

    Hover your mouse over theerror icon in order to read thelong error message.

    Remark : If you useaggregation functions in aCDS view definition, then allselected fields except thoseused in aggregate functionshave to be specified in theGROUP BY clause.

    3. Add the GROUP BY clause inyour coding.

    Hint : You can use the QuickFix ( CTRL+1 ) feature to createthe GROUP BY list.

    group by so . so_id , bp.company_name , so . currency_code

    4. Your CDS view should onlyretrieve those sales orderswhere no invoice exists.

    Add a WHERE condition afterthe select list and before theGROUP BY clause in order toexclude the sales orders withinvoices.

    where so_inv . node_key is null

  • 8/17/2019 CodeJam Exercise 3 CDS

    16/34

    Advanced View Building in SAP NetWeaver AS ABAP 16

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    Code Snippet: ZCDSV_MULTIPLE_FEATURES_##

    @AbapCatalog.sqlViewName: 'ZCDSV_2_##'

    define view zcdsv_multiple_features_2_## as select from snwd_so as soinner join snwd_bpa as bp on so . buyer_guid = bp . node_keyleft 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 wassuccessful, open the Data

    Preview

  • 8/17/2019 CodeJam Exercise 3 CDS

    17/34

    Advanced View Building in SAP NetWeaver AS ABAP 17

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    8. The result will look like thescreenshot below.

    Remark: As you might see inthe data preview, it does notmake sense to group over thesales order id which is a uniquekey. Instead, we will group theresult by the company name inorder to have the sum of allsales 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_keyleft 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 Sourceobject.

    11. Check and activate it.

  • 8/17/2019 CodeJam Exercise 3 CDS

    18/34

    Advanced View Building in SAP NetWeaver AS ABAP 18

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    12. After the activation wassuccessful, open the DataPreview.

    13. The result will now look like thescreenshot.

  • 8/17/2019 CodeJam Exercise 3 CDS

    19/34

    Advanced View Building in SAP NetWeaver AS ABAP 19

    CodeJam: ABAP for SAP HANA

    C3. Using Built-In FunctionsThe CDS DDL syntax provides the built-in string functions SUBSTRING and LDAP and the built-in arithmeticfunctions CEIL and MOD.

    In this exercise, we will enhance the select list of the CDS view defined in the exercise C2 with a 10character short name of the company name.

    Explanation Screenshot

    1. Add the SUBSTRING statement to your CDS viewdefinition.

    Specified the aliasSHORT_NAME for the result ofthe built-in function.

    Remark: Similarly to

    aggregate functions, an aliasis required for the result of abuilt-in function.

    substring ( bp . company_name , 0 , 10 ) as short_name

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

    15. Check and activate it.

  • 8/17/2019 CodeJam Exercise 3 CDS

    20/34

    Advanced View Building in SAP NetWeaver AS ABAP 20

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    16. After the activation wassuccessful, open the DataPreview.

    17. The result will look like thescreenshot.

  • 8/17/2019 CodeJam Exercise 3 CDS

    21/34

    Advanced View Building in SAP NetWeaver AS ABAP 21

    CodeJam: ABAP for SAP HANA

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

    Explanation Screenshot

    1. Add the CASE expressionshown in the Screenshot.

    You can insert it at the endof the select list.

    Conditions:- If the delivery status equal

    SPACE, then we set thevalue of result field to“OPEN“

    - If the delivery status is “ D”,then we set the value ofresult field to “ DELIVERED“

    - In other cases, then wereturn the value of the fieldSO.DELIVERY_STATUS .

    Remark : An alias is requiredfor the result of a CASE

    expression.

    case so . delivery_statuswhen ' ' then 'OPEN' when 'D' then 'DELIVERED' else so . delivery_status

    end as delivery_status ,

    2. Save your DDL Sourceobject.

    3. Check and activate it.

  • 8/17/2019 CodeJam Exercise 3 CDS

    22/34

    Advanced View Building in SAP NetWeaver AS ABAP 22

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    Code Snippet : ZCDSV_MULTIPLE_FEATURES_##

    @AbapCatalog.sqlViewName: 'ZCDSV_2_##'

    define view zcdsv_multiple_features_2_## as select from snwd_so as soinner join snwd_bpa as bp on so . buyer_guid = bp . node_keyleft 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_statuswhen ' ' 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 wassuccessful, open the DataPreview.

  • 8/17/2019 CodeJam Exercise 3 CDS

    23/34

    Advanced View Building in SAP NetWeaver AS ABAP 23

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    5. The final result will look like thescreenshot.

    Summary:

    In this exercise, you learned how to use Core Data Services features like aggregations, built-in functionsand CASE expressions that are provided by the CDS DDL syntax.

  • 8/17/2019 CodeJam Exercise 3 CDS

    24/34

    Advanced View Building in SAP NetWeaver AS ABAP 24

    CodeJam: ABAP for SAP HANA

    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 areconsumed similarly to ABAP Dictionary views.

    Explanation Screenshot

    1. Open reportZR_CDSV_OUTPUT_## (where ## represents yourgroup number) in yourpackage.

    Code : ZR_CDSV_OUTPUT_##

    REPORT ZR_CDSV_OUTPUT_##.

    * *** Declare your variable *** * e.g DATA: lt_data TYPE table of zcdsv_simple_##.

    * *** Implement your SQL Query *** * e.g.Select * from zcdsv_simple_##

    into table @lt_data.

    Try. *** Display the data *** * cl_demo_output=>display( EXPORTING data = lt_data * name = 'Output of CDSView'). ENDTRY.

    2. Replace the data descriptionwith your CDS Viewzcdsv_multiple_features_2_## (where ## represents yourgroup number).

    * *** Declare your variable *** DATA: lt_data TYPE

    table of zcdsv_multiple_features_2_## .

    3. Replace the select statementwith your CDS View:zcdsv_multiple_features_2_## (where ## represents yourgroup number)

    Remark: You may wonderabout the “ @” in the INTOTABLE clause… stay tuned forthe OpenSQL CodeJamexercise.

    * *** Implement your SQL Query *** Select * from zcdsv_multiple_features_2_##

    into table @ lt_data .

  • 8/17/2019 CodeJam Exercise 3 CDS

    25/34

    Advanced View Building in SAP NetWeaver AS ABAP 25

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    4. Remove the comment for

    display the data.

    TRY. **** Display the data ***

    cl_demo_output =>display (EXPORTING data = lt_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_dataname = 'Output of CDS View' ).

    ENDTRY.

    5. Save your ABAP Report.

    6. Check and activate it.

    7. Run the report ( F8 ) andcheck the result.

  • 8/17/2019 CodeJam Exercise 3 CDS

    26/34

  • 8/17/2019 CodeJam Exercise 3 CDS

    27/34

  • 8/17/2019 CodeJam Exercise 3 CDS

    28/34

    Advanced View Building in SAP NetWeaver AS ABAP 28

    CodeJam: ABAP for SAP HANA

    F. Associations

    In this exercise, we will use CDS views with associations. For a better understanding, we will explain theCDS view before you start with your exercise. You can find the CDS views in the solution packageTEST_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 anunderlying data model instead of writing a complex join every time. Also the reuse of a DDL view makes thelife 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.

  • 8/17/2019 CodeJam Exercise 3 CDS

    29/34

    Advanced View Building in SAP NetWeaver AS ABAP 29

    CodeJam: ABAP for SAP HANA

    Explanation

    Code Snippet : ZCDSV_OIA_ASSOC_02_ITEM @AbapCatalog.sqlViewName: ‘ ZVOIA_ASS02’ define view zcdsv_oia_assoc_02_item as

    select from snwd_so_iassociation [ 0 ..*] to zcdsv_oia_assoc_01_note as noteson $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 withCDS 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 thiscase 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) forthe column NOTE_GUID.

    association [ 0 ..*] to zcdsv_oia_assoc_01_note as noteson $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 noteson $projection. note_key = notes . note_key

    { ...notes

    }

  • 8/17/2019 CodeJam Exercise 3 CDS

    30/34

    Advanced View Building in SAP NetWeaver AS ABAP 30

    CodeJam: ABAP for SAP HANA

    Explanation

    In the CDS view zcdsv_oia_assoc_02_item the sales order item table ( SNWD_SO_I ) is joined withCDS 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 thiscase 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) forthe column NOTE_GUID.

    association [ 0 ..*] to zcdsv_oia_assoc_01_note as noteson $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 noteson $projection. note_key = notes . note_key

    { ...notes

    }

  • 8/17/2019 CodeJam Exercise 3 CDS

    31/34

    Advanced View Building in SAP NetWeaver AS ABAP 31

    CodeJam: ABAP for SAP HANA

    Explanation

    Code Snippet : ZCDSV_OIA_ASSOC_03_SO @AbapCatalog.sqlViewName: 'ZVOIA_ASS03' define view zcdsv_oia_assoc_03_so as

    select from snwd_soassociation [ 0 ..*] to zcdsv_oia_assoc_02_item as itemson node_key = items . sales_order_key

    association [ 0 ..*] to zcdsv_oia_assoc_01_note as so_noteson $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 hereitems . 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 andzcdsv_oia_assoc_01_note .

    With the path expression items.notes[ language = 'E' ]. text i t is possible to filter the data tothe text with language English.

    By activating the CDS view, the path expression is converted to a join expression.{

    ...// association can also be used hereitems . notes [ language = 'E' ]. text as english_so_item_notes , ...

    }

    Remark: By activating the CDS view the associations will be converted into join conditions.

  • 8/17/2019 CodeJam Exercise 3 CDS

    32/34

    Advanced View Building in SAP NetWeaver AS ABAP 32

    CodeJam: ABAP for SAP HANA

    In the following exercise we use the CDS View zcdsv_oia_assoc_03_so and access to the fields fromthe underlying view. You will see in which way you can access to the field of the associations that wedescribed above.

    Explanation Screenshot

    1. In the following exercise we use theCDS Viewzcdsv_oia_assoc_03_so andaccess to the fields from theunderlying view. You will see inwhich way you can access to thefield of the associations that wedescribed above.

    Code Snippet : ZCDSV_ASSOCIATION_##

    @AbapCatalog.sqlViewName: 'ZCDSV_4_##' define view zcdsv_association_##

    as select from zcdsv_oia_assoc_03_so{

    note_key}

    2. Add the fielditems.notes.langu_dep_note_key to your select list.

    Similarly, add the fieldenglish_so_item_notes to yourselect list. Use the Code completionfeature ( 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

  • 8/17/2019 CodeJam Exercise 3 CDS

    33/34

    Advanced View Building in SAP NetWeaver AS ABAP 33

    CodeJam: ABAP for SAP HANA

    Explanation Screenshot

    5. After the activation was successful,open the Data Preview.

    For that open the context menu ofyour DDL Source object and click onOpen Data Preview .

    6. The final result will look like thescreenshot.

    Summary:

    After this exercise, you are now familiarized with associations in CDS Views. You know where thedifferences to the Joins are and how to use the association in a CDS View.

  • 8/17/2019 CodeJam Exercise 3 CDS

    34/34

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