Saturday 3 December 2016

ABAP on HANA – Steps to call a CDS view from another CDS View

Core Data Services (CDS) :


CDS is an infrastructure layer for defining semantically rich data models, which are represented as CDS views.

A CDS view is used to define the structure of an SQL view and represents a projection onto one or several ABAP Dictionary tables or ABAP Dictionary views.

This blog shows how to call a CDS view from another CDS view.
View (1) :

1. Create a Data Definition as shown below

ABAP on HANA – Steps to call a CDS view from another CDS View

ABAP on HANA – Steps to call a CDS view from another CDS View

2. This view displays Material Master details based on Plant

ABAP on HANA – Steps to call a CDS view from another CDS View

3. Source code

efine view Zcds_View1 
 with parameters  p_plant:WERKS_D  
 as select from mara as material_master 
--joins
inner join marc as plant_data
on material_master.matnr = plant_data.matnr
inner join mard as stloc 
on plant_data.matnr= stloc.matnr

---associations
association[1..*] to makt as material_description 
on stloc.matnr = material_description.matnr

 {
    key material_master.matnr as Material_no, 
    material_master.mtart as Material_type,
    plant_data.werks as Plant,
    stloc.pstat as Maintenance_status,
    material_description[1: ( spras = 'E' or spras = 'J' ) ].maktx as material_description,
    material_master.ersda as Date_mat
}

where plant_data.werks = $parameters.p_plant

4. Save, Activate and Execute

5. Output

ABAP on HANA – Steps to call a CDS view from another CDS View

ABAP on HANA – Steps to call a CDS view from another CDS View

6. View (1) is now used in the following view

View (2) :

7. Create a Data Definition as below

ABAP on HANA – Steps to call a CDS view from another CDS View

ABAP on HANA – Steps to call a CDS view from another CDS View

8. For an example, in this view, my requirement is to display the Material Master details not only based on plant but also Maintenance status

9. So, Here I have taken two parameters

ABAP on HANA – Steps to call a CDS view from another CDS View

10. Press ctrl + space and select the Zcds_View1 (i.e. View (1) )

ABAP on HANA – Steps to call a CDS view from another CDS View

11. Fill the input parameter as shown below  

ABAP on HANA – Steps to call a CDS view from another CDS View

12. Write a where condition to filter the view output based on our requirement as below 

ABAP on HANA – Steps to call a CDS view from another CDS View


13. Source Code

define view Zcds_View2
with parameters p_plant2: WERKS_D,
                p_main_status2: PSTAT_D
                
 as select * from  Zcds_View1( p_plant:$parameters.p_plant2) as View1
 where View1.Maintenance_status = $parameters.p_main_status2

14. Save, Activate and Execute

15. Output

ABAP on HANA – Steps to call a CDS view from another CDS View

ABAP on HANA – Steps to call a CDS view from another CDS View

No comments:

Post a Comment