Skip to Content
Technical Articles
Author's profile photo Sanjeev Kumar

Part#4. Consume CDS View in an ABAP Program

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

 

 

  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.

 

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

 

Assigned tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sebastian Dingermann
      Sebastian Dingermann

      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( ).

       

      Author's profile photo Sanjeev Kumar
      Sanjeev Kumar
      Blog Post Author

      Hi Sebastian,

       

      Thanks for your input!

       

      Regards

      Sanjeev.

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      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.

      Author's profile photo Vishal Agarwal
      Vishal Agarwal

      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

       

      Author's profile photo Ramin Shafai
      Ramin Shafai

      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.

      Author's profile photo Dipak Patel
      Dipak Patel

      Good One Sanjeev, Thanks for the document.