You are on page 1of 11

4/15/23, 11:20 AM Part#4.

Consume CDS View in an ABAP Program | SAP Blogs

Community
Follow

Ask a Question Write a Blog Post Like Login


 RSS Feed

Technical Articles

Sanjeev Kumar
October 24, 2019 | 2 minute read

Part#4. Consume CDS View in an


ABAP Program
 13  22  36,364

This is the 4th blog post on the CDS detailed explanations after the Introduction
post. Refer to the Introduction and blog post indexes here:
https://blogs.sap.com/2019/10/21/part1.-sap-cds-views-demystification/

CDS view is also called a VDM i.e. Virtual Data Model as there is no data persistence
happening.  All the SQL like code written in a CDS view pick the data from Base
tables directly or via other CDS view at RUNTIME .  The actual data still remain in the
SAP Base tables and CDS views remains a virtual data model.  Now in a scenario, we
have created a complex CDS view which has lot of business logic implemented into it
and we want to use the data coming out of this CDS view in an ABAP program, can
this be possible?  Yes, it is possible via new ABAP syntax.  Let’s look at it.

1.  We will use the Basic CDS view we created before to be consumed via ABAP
program;

@AbapCatalog.sqlViewName: 'ZSQL_BASIC_VIEW'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS View type #BASIC'
define view ZCDS_BASIC_VIEW as select from sflight {
//sflight
https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 1/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

--key mandt,
key carrid,
seatsmax_b,
seatsocc_b,
seatsmax_f,
seatsocc_f
}

     2. Within ABAP perspective in Eclise or HANA Studio, right click on your
user under Lcal Objects within an ABAP project to create an AABAP
program

https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 2/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

1. Consume the CDS Basic view we created via this ABAP program.  Note the new
ABAP syntax used for this purpose.

*&-------------------------------------------------------------------
*& Report zconsume_cds
*&-------------------------------------------------------------------
*&
*&-------------------------------------------------------------------
REPORT zconsume_cds.

Select * from ZCDS_BASIC_VIEW into table @data(lt_itab).

cl_demo_output=>display_data( lt_itab ).

2. Hit F8 or Execute the program to check the output.


https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 3/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

In summary, there are lot of scenarios in projects where we write complex CDS views
and we need to consume them into an ABAP program for different business needs. 
In this blog post, we looked at the new ABAP syntax to consume a CDS view.

Part# 5. CDS View Extension: Learn the concept of re-usability within CDS views.

Follow for upcoming blog posts: Sanjeev Kumar

Keep learning..

SK

https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 4/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

Alert Moderator

Assigned Tags

SAP S/4HANA embedded analytics

ABAP Development

OData

#ABAP

#CDSVIEWS

#s4hanaanalytics

Similar Blog Posts 


Part#1. SAP CDS views Demystification
By Sanjeev Kumar Oct 21, 2019

Part#3. CDS View with Expressions and Session Variables


By Sanjeev Kumar Oct 23, 2019

Part#8. CDS Views - Joins and Associations


By Sanjeev Kumar Oct 30, 2019

Related Questions 
How does security work for S/4 HANA reporting?
By AJAYKUMAR SUGUMARAN Oct 05, 2019

How to consume analytics planning in S4HANA On-Premise?


By Bhargava Tanguturi Jan 03, 2020

ABAP Report and CDS View


By S S Jan 27, 2020

https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 5/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

13 Comments

You must be Logged on to comment or reply to a post.

Sebastian Dingermann
October 24, 2019 at 8:55 am

Hi Kumar,

thanks for sharing!

For just displaying the content of a CDS, you can also use Integrated Data Access (IDA).

So, there is no need for a select statement.

cl_salv_gui_table_ida=>create_for_cds_view('ZCDS_BASIC_VIEW')->fullscree( )->display( ).

Like 10 | Share

Sanjeev Kumar | Blog Post Author


October 24, 2019 at 1:38 pm

Hi Sebastian,

Thanks for your input!

Regards

Sanjeev.

Like 1 | Share

https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 6/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

Jelena Perfiljeva
October 25, 2019 at 9:35 pm

FYI - better use "Reply" button when replying to someone's comment. If you do that then the other
person will be notified of your reply. When we use "add comment" then only the blog author (i.e.
yourself in this case) gets notified.

Like 3 | Share

Vishal Agarwal
October 23, 2020 at 7:09 pm

Sir,

Very good document. Really appreciate all the effort you put. I was able to follow the whole thing.
Thank you so much.

Regards,

Vishal Agrawal

Like 0 | Share

Ramin Shafai
March 30, 2021 at 9:55 pm

But how do you follow the association from Abap?

Selecting from CDS view is the same as selecting from a DDIC object, nothing new here. The tricky part would
be how to navigate through an association from Abap.

Like 0 | Share

Pradeep Alex
February 22, 2022 at 10:42 am

Is that possible !!. to drill down from ALV reports, making use of associations.

Like 0 | Share

Ramin Shafai
February 22, 2022 at 1:10 pm

I have not found a way.

https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 7/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

And no one seems to know.

Like 0 | Share

Namasivayam Naveen G
May 24, 2022 at 12:00 pm

To navigate to an association, in ABAP program/ Class where CDS view need to be


consumed

In the select statement, use below syntax

For single level association,

select

\_association-field_name from CDS_VIEW_NAME

for two level association,

Select

\_association1\_association2-field_name from CDS_VIEW_NAME

Like 1 | Share

Ramin Shafai
May 26, 2022 at 11:45 am

Thanks. That works!

select
Order_Type,
\_txt-spras as lang

from zrsh_test_cds
into table @data(lt_data).

https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 8/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

The Cds view:

define view zrsh_test_cds as

select from t003o


association[1..*] to t003p as _txt on _txt.auart = $projec
{
auart as Order_Type,
stsma as Order_Profile,
_txt
}
where auart like 'LM%'

Like 0 | Share

Dipak Patel
April 12, 2021 at 11:05 am

Good One Sanjeev, Thanks for the document.

Like 0 | Share

Patrick Rohner
January 11, 2022 at 3:58 pm

Hi,

Is it possible to extract the data from the CDS view by adding a where clause?

Let's assume we have cds view A_COSTCENTER, but I only want to extract certain cost centers. Is that
possible?

Regards,

Patrick
https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 9/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

Like 0 | Share

Ramin Shafai
February 22, 2022 at 1:15 pm

Of course, why not. It's just like any other SQL statement:

select * from A_COSTCENTER


where cost_center = @lv_cc
into table @data(lt_costcenters).

What I don't know is how to go down an association path. That seems not possible.

Like 1 | Share

Muhammed Hassin Khan


October 11, 2022 at 1:07 pm

Hallo Kumar,

I have a problem with accumulation in CDS-Views so I posted a question.

https://answers.sap.com/questions/13733584/cds-countingsum-value-from-column.html

If i dont find a solution in CDS I would like to solve it via ABAP.

My question is, can I consume abap program in cds view?

Regards

Hassin

Like 0 | Share

https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 10/11
4/15/23, 11:20 AM Part#4. Consume CDS View in an ABAP Program | SAP Blogs

Find us on

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Cookie Preferences

Newsletter Support

https://blogs.sap.com/2019/10/24/part4.-consume-cds-view-in-an-abap-program/ 11/11

You might also like