Online Tutorials & Training Materials | STechies.com
Register Login

Building CDS (Core Data Services) Views in ABAP on SAP HANA

|| || 0

Building CDS (Core Data Services) Views in ABAP on SAP HANA
Stechies

How to build Core Data Services Views in ABAP on SAP HANA and how to display it on ABAP List Viewer (ALV)?

This tutorial gives the brief explanation on how to build Core Data Services Views in ABAP on SAP HANA and also how to display it on ABAP List Viewer (ALV).

CDS (Core Data Services) is a collection of domain-specific languages and services which are used for defining and consuming semantically rich data models in SAP HANA

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,description. Choose the transport request if required and then confirm the creation of DDL source

3) The new DDL object will be available in the project explorer

4) Now here we will retrieve the data from SNWD_So table using 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 in 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 left outer join with 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 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 semantic to our CDS view definition in order to define the field Currency_Code with 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 sum of the gross amount field now will be calculated

Displaying CDS view on ALV

Execute the syntax code below in order to display the CDS view with ABAP List Viewer in 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

 

 


Related Articles

0.0137 seconds.