Skip to Content
Technical Articles

Part#6. Parameters in a CDS view

In the previous blog post, we saw simple examples of CDS views which are a type of a select statement and you could surely add ‘WHERE’ clause to filter the data.  But this where clause is static with a non-parameterized view and may not fulfill the project requirements where a user interaction is required.  So it is absolutely possible to add ‘Parameters’ to take user input and filter out the data from the CDS view accordingly;

 

  1. Create a new CDS view;

 

You can choose the parameter template for easy code;

 

This is what we get as the default structure of the CDS view with Parameters.  Look at the ‘with parameters’ clause got added.

 

2. Add the SQL view name and table name. To the ‘with parameters’ clause, add the parameter name before colon and the data type after colon as mentioned above.  For ‘Data Type’ you can either add the data element directly as we used to do in classic ABAP to declare a variable i.e. or you can add primitive data type like abap.<data ype>(<len>) e.g.:  char(3) ;

 

Data Element – S_CURRCODE;

ABAP Premitive types – abap.char(3);

To use the parameters to filter out the data we need to apply a ‘WHERE’ clause and use the parameter with $Paramter keyword i.e. $Parameter.<parameter name> OR just the parameter name prefixed with a colon i.e.  :<parameter name> .  Let’s check how the CDS view look like now;

OR

  • Active the CDS view now and hit F8 to execute. Checkout the user input pop up to enter the currency code as we configured in the CDS view;

 

  • Add ‘AUD’ for Australian Dollar and hit ‘OK’. Check the output 😊

 

  • Now lets see how we can consume this CDS view with parameters in our ABAP program. What new code we need to write and how the parameters behave;

Create a new ABAP program ZCONSUME_CDS_PARAM and add code as below

*&---------------------------------------------------------------------*
*& Report zconsume_cds_param
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zconsume_cds_param.

data: lv_curr type S_CURRCODE.
cl_demo_input=>new(  )->add_field( changing field  = lv_curr )->request(  ).

select * from ZCDS_VIEW_PARAM( in_curr = @lv_curr ) into table @data(lt_tab).

cl_demo_output=>display( lt_tab ).

 

The ‘Enter’ button here is automatic, we don’t need to write any code for this.  Enter the currency value and hit Enter;

 

To summarize, in live projects 95% of analytics needs selection screens to get user input.  This is also very important to reduce the amount of data hit by the CDS view which subsequently improves the CDS view performance to fetch only the required data out.  In this blog post, we saw how to use the parameters to get the user input and fetch only the data needed for analytics.

 

Part# 7. How to search for SAP standard CDS Views : Find ways to search for SAP delivered CDS content.

 

Follow for upcoming blog posts: Sanjeev Kumar

Keep learning..

SK

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