In this post, you will learn to create and consume a CDS View with a parameter.

A CDS View with a parameter is created to pass input to CDS View. You can compare this parameter to a selection screen parameter. Multiple parameters, however there is no option similar to select-options on selection screen.

1. Create new Data Definition using template ‘Define View with Parameters’

CDS View with parameters

@AbapCatalog.sqlViewName: 'ZJP_PARAMETER'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Parameters'
define view zjp_cds_parameters
    with parameters iv_country_from : abap.char(3),
                    iv_country_to   : land1,
                    @Environment.systemField: #SYSTEM_DATE
                    iv_date         : abap.dats
as select from spfli {
    key carrid  as Carrid,
    key connid  as Connid,
    :iv_date    as CurrentDate,
    countryfr   as Countryfr,
    cityfrom    as Cityfrom,
    countryto   as Countryto,
    cityto      as Cityto,
    fltime      as Fltime
} where countryfr = $parameters.iv_country_from
  and   countryto = :iv_country_to

Notes

  • After the view name, ‘with parameters‘ key words are added
  • Parameters are defined as a comma separated list
  • Parameter type can be either predefined ABAP types or a data element. Predefined ABAP types for CDS are not same as ABAP data dictionary and input help can be used (Ctrl + Space)
  • Default value can be assigned with annotation – @Environment.systemField: #SYSTEM_DATE
  • Parameters are optional only when default value is assigned. We can only use system fields such as #SYSTEM_DATE. Possible values are as below.
  • Parameters can be used in the select field list as it is or for calculation, and using alias for such fields is mandatory
  • Parameters can be used in where condition
  • Parameters are addressed as :<parameter_name>or $parameters.<parameter_name>

Output

When a view with parameter is executed, a pop up appears where parameters can be entered. All parameters will be mandatory unless assigned with default values.

Enter parameters and click OK to get the output.

How to consume CDS with parameters from ABAP?

FROM <CDSViewName>( <parameter1> = 'Value1' ,
                    <parameter1> = 'Value2' , 
                    ... )

Pass the parameters after the CDS View name in the FROM clause in closed round brackets with a comma separated list.

SELECT * FROM zjp_cds_parameters( iv_country_from = 'US',
                                  iv_country_to   = 'DE',
                                  iv_date         = '20201201' )
 INTO TABLE @DATA(flights).
IF sy-subrc EQ 0.
    cl_demo_output=>display( flights ).
ENDIF.

The result matches with the direct execution of CDS view using same parameter. The date is used different than system date to demonstrate that the parameter can be written to output as well.

Visit ABAP on HANA series for Tutorials on CDS, AMDP, Eclipse, and ALV IDA.


If you like the content, please subscribe…

Join 4,016 other subscribers

Discovering ABAP YouTube Channel