Skip to Content
Author's profile photo Carine Tchoutouo Djomo

Spotlight on parameterized ABAP CDS Views

Hi everyone!

I just want to put the light on a feature – in the context of advanced view building with ABAP Core Data Services (CDS) – that is available starting with AS ABAP 7.4 SP8: CDS views with input parameters.
Yes, as a developer, you can now parameterize your CDS views. It means you can define one general views which produce context-specific result sets by using parameter values passed at execution time. This means that you do no longer need to create a view for each context!

For example:

  1. Language filter in combination with associations
    Instead of creating a a CDS view for each language, only one parameterized view is now needed. The specific language key value is passed at execution time.
  2. Calculation of sales tax
    Instead of creating a a CDS view for each country with the specific value-added tax, only one parameterized view is now needed. The country-specific value-added tax is passed at execution time and used in the related arithmetic expressions.

Watch the compact video tutorial below. It shows how to create and call parameterized views in ABAP.

Note that CDS views with parameters are not supported on all SAP certified databases – At least SAP HANA does 🙂 . However, the DDL of the ABAP CDS allows creating and accessing CDS views with parameters independent of the database system.

In case an Open SQL statement SELECT is access such a parameterized view or a view that contains such a view as data source, but the accessed database system does not support them, a non-handleable exception of the class CX_SY_SQL_UNSUPPORTED_FEATURE is raised.

The recommended approach when using a database-specific features (e.g. views with parameters) in ABAP programs is to use the method USE_FEATURES of the class CL_ABAP_DBFEATURES to check whether the accessed database system supports it.**

(**) Starting with AS ABAP 7.50, CDS views with parameters are supported on any DB. Thus, it is no longer needed to check whether the feature is supported or not, i.e. an alternative (fallback) implementation is no longer needed.

Example:

IF abap_true = cl_abap_dbfeatures=>use_features(
       requested_features =

         VALUE #( ( cl_abap_dbfeatures=>views_with_parameters ) ) ).
   “Call of CDS view with input parameters
     SELECT *
       FROM demo_iparameter_02( p_langu       = @sy-langu,
                                p_saving_rate = ‘0.04’,
                                p_lc_status   = @p_status )
       INTO TABLE @DATA(lt_data).

  cl_demo_output=>display_data(
    EXPORTING
      value = lt_data
      name  = ‘Demo: CDS View with Input Parameters’  ).
ELSE.
   “alternative implementation
cl_demo_output=>display( ‘Database system does not support views with parameters’ ).
ENDIF.

Find more detailed information on this topic in the ABAP Keyword Documentation.

Assigned tags

      10 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Timothy Muchena
      Timothy Muchena

      Hi Carine Tchoutouo Djomo

      Thanks for the blog

      Is it possible to have select options on CDS views

      Kind regards

      Author's profile photo Thomas Gauweiler
      Thomas Gauweiler

      You can use the usual WHERE conditions including select options when selecting from a view with parameters. But you cannot pass select options as parameters.

      Regards, Thomas

      Author's profile photo Bhuvanesh Raju
      Bhuvanesh Raju

      could you please elaborate on using where condition with IN/ select options for CDS view with parameters

      Author's profile photo Sigurður Njarðvík
      Sigurður Njarðvík

      I did try to create the following Parameterized view on my system

      @AbapCatalog.sqlViewName: 'zddl_table_info'

      @AbapCatalog.compiler.CompareFilter: true

      @AccessControl.authorizationCheck: #CHECK

      @EndUserText.label: 'System Dictionary info for transparent tables'

      define view Zcds_Table_Info

          with parameters p_tabname:tabname //Data element from dictionary

      as

        select from dd03l as l  left outer join dd04t as t

            on l.rollname = t.rollname and t.ddlanguage = 'E'

        left outer join dd03t as f  on l.fieldname = f.fieldname and f.tabname = l.tabname

        {

           key l.tabname as Tablename,

           key l.fieldname as Fieldname,

           l.position as Position,

           l.keyflag as Iskey,  

           l.rollname as Dataelement,

           l.domname as Domainname,

           l.datatype as Datatype,

           l.leng as Length,

           COALESCE ( t.ddtext, f.ddtext) as Description 

        }

         where l.tabname = :p_tabname

      But I cant run it I always get the following error

      SQL error "SQL code: 215 Parameters supplied for object

        'ZDDL_TABLE_INFO' which is not a function. If the parameters are intended as

        a table hint, a WITH keyword is required.

      any idea on what is wrong here, the demo view demo_cds_parameters works fine on my system
      /wp-content/uploads/2016/01/cds_error_874213.png

      Author's profile photo Sigurður Njarðvík
      Sigurður Njarðvík

      Fixed it !

      I had to delete the view and create again with a new name, then it worked with unchanged code.

      Probably because it was first activated without a parameter defined

      Author's profile photo Simon Perstorfer
      Simon Perstorfer

      Hi,

      is it possible to convert an string input parameter to upper case?

      Regards

      Simon

      Author's profile photo Former Member
      Former Member

      Hi,

      Can you please let us know how to use an Select-Option and also using For ALL entries in the Where condition.

      Appreciate your inputs.

      Thanks,

      Suresh

      Author's profile photo Thomas Gauweiler
      Thomas Gauweiler

      Select options and FOR ALL Entries are features of Open SQL and can only be used in a SELECT statement in ABAP code. It can not be used within a CDS view itself and you cannot pass them as parameters into a CDS view.

      You can use Select options and FOR ALL Entries only in the WHERE condition when selecting from a CDS view with parameters:

      SELECT * FROM cds_view( p = @simple_scalar_param )

      FOR ALL ENTRIES OF @itab

      WHERE f1 = @itab-f1

      AND f2 in @seltab_for_f2

      INTO ...

      Regards, Thomas

      Author's profile photo Arnaud Chatueng
      Arnaud Chatueng

      Hello everyone,

      i have 2 specifics questions related to cds views with parameters and hope you can help there:

      • Is it also possible to set parameters like plant, material, sales organisation etc as optional. If yes the how can i do it?
      • Is it possible to set and display the parameters as interval so that an interval can be selected (multiple selection) by users i.e. matnr : 00101 - 00152

      I would appreciate your help.

      Thx in Advance

      Author's profile photo Carine Tchoutouo Djomo
      Carine Tchoutouo Djomo
      Blog Post Author

      Hello Arnaud,

      regarding your two questions:

      1. CDS view parameters cannot be optional.
        The only exception made here concerns CDS view parameters for which an @Environment annotation is specified. Such CDS view parameters are treated as optional parameters when the CDS view is read using ABAP SQL (fka open SQL).
        When using @Environment annotations, a view parameter is automatically filled by the system with the value from the corresponding system variable at runtime. However, if such a CDS view is read from another CDS view – i.e. view on view –, then the view parameters are no longer optional and must be passed.
        Read more about the existing @Environment annotations in ABAP CDS: https://help.sap.com/doc/abapdocu_754_index_htm/7.54/en-US/index.htm?file=abencds_f1_parameter_annotations.htm
      2. The described requirement can be achieved using the WHERE BETWEEN clause in the corresponding CDS view.

        You may have a look at the example provided in the ABAP Keyword documentation:
        https://help.sap.com/doc/abapdocu_754_index_htm/7.54/en-US/index.htm?file=abenselect_cds_para_abexa.htm

      I hope this helps.

      Kind regards,
      Carine