This tutorial provides a brief explanation of how to build Core Data Services (CDS) Views in ABAP on SAP HANA and how to display them using the ABAP List Viewer (ALV).
What is CDS?
CDS (Core Data Services) is a collection of domain-specific languages and services that are used for defining and consuming semantically rich data models in SAP HANA
How to Build CDS in SAP ABAP?
Please follow the below steps for building CDS in ABAP:
1) Go to your ABAP project, Select the package, and create a new ABAP DDL source object using the context menu
2) Give the required information such as name, and description. Choose the transport request if required and then confirm the creation of the DDL source
3) The new DDL object will be available in the project explorer
4) Now here we will retrieve the data from the SNWD_So table using the ABAP CDS view
@AbapCatalog.sqlviewName: 'sql_view_name' // for ex. zcdsv_oia_demo//
define view zcdsv_oia_demo_01 as
select from snwd_so as so
{
key so.note_key as so_item_guid,
so.currency_code,
so.gross_amount,
}
Save and activate your DDL source. After refreshing your package in Project Explorer we will see a new view has been added to our package
We can also preview data using Context menu -> Open Data Preview
6) Now we should move on to define our CDS view by defining the left outer join with the product table and enhancing the select list with column names from the joint table
@AbapCatalog.sqlviewName: 'zcdsv_oia_demo'
define view zcdsv_oia_demo as
select from snwd_so as so
left outer join snwd_pd as pd on so.product_guid = pd.node_key
{
key so.note_key as so_item_guid,
so.currency_code,
so.gross_amount,
pd.product_id,
pd.type_code,
pd.category
}
Save and activate the CDS view and have a look at the data preview. We will see the output CDS view
7) Now we can also provide more advancement to our CDS view like shortening the product id or more explained code
@AbapCatalog.sqlviewName: 'zcdsv_oia_demo'
define view zcdsv_oia_demo as
select from snwd_so as so
left outer join snwd_pd as pd on so.product_guid = pd.node_key
{
key so.note_key as so_item_guid,
so.currency_code,
so.gross_amount,
// pd.product_id,
substring( pd.product_id, 4, 4) as short_pd_id,
case pd.type_code,
when 'PR' then 'PRODUCT'
when 'AD' then 'ADVERSTISEMENT'
else pd.type_code
end as long_ty_code,
pd.category
}
Now again when you save, execute and preview data you will see CDS view
8) In the next step we will add semantics to our CDS view definition to define the field Currency_Code as a reference field of gross_amount.
@AbapCatalog.sqlviewName: 'zcdsv_oia_demo'
define view zcdsv_oia_demo as
select from snwd_so as so
left outer join snwd_pd as pd on so.product_guid = pd.node_key
{
//key so.note_key as so_item_guid,
@Semantics.currencyCode so.currency_code,
@Semantics.amount.currencyCode: 'currency_code'
sum(so.gross_amount) as sum_gross_amount,
// pd.product_id,
substring( pd.product_id, 4, 4) as short_pd_id,
case pd.type_code,
when 'PR' then 'PRODUCT'
when 'AD' then 'ADVERSTISEMENT'
else pd.type_code
end as long_ty_code,
pd.category
}
where pd.category <> 'Software'
and pd.category <> 'Computer system accessories'
group by so.currency_code, pd.type_code, pd.product_id
Now execute and preview the data. We will see the sum of the gross amount field now calculated
Displaying CDS view on ALV
Please follow the steps below to display CDS view in ALV
Execute the syntax code below to display the CDS view with ABAP List Viewer in the new ABAP DDL source
cl_salv_gui_table_ida=>create(sql_view_name)->fullscreen()->display().
Read next: Introduction to SAP ABAP Managed Database Procedure in SAP HANA