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

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

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Vishal Agarwal
      Vishal Agarwal

      Great document again Sir. Thank you so much.

      Author's profile photo Thiyagarajan Balasubramani
      Thiyagarajan Balasubramani

      Hi Sanjeev,

      Could you please explain how to extend a standard CDS view which has input parameters.

      Need to change the parameter default value in the extend view.

      Thanks

      Thiyagarajan

      Author's profile photo Kanishak Gupta
      Kanishak Gupta

      Hi Sanjeev,

       

      Thanks, Can we also use select options as input to CDS views so that we can also utilize the select options and not the parameters?

       

      regards

      Kanishak

      Author's profile photo Lakshminarasimhan Narasimhamurthy
      Lakshminarasimhan Narasimhamurthy

      How can we add search help/F4 help to the added parameters ?

      Author's profile photo Vijay Reddy Kuntla
      Vijay Reddy Kuntla

       

      Hi Sanjeev,

      How can we make this parameter as optional, by default it is mandatory.

      I couldn't found exact solution for making parameter as optional.

      I am using the cds view [ with parameters ] as a datasource in SAP BW system.

      Thanks,

      Vijay Reddy