How to create ABAP CDS Views with Parameters

1
3688

Hello everyone,

In this ABAP for HANA tutorial, you will learn How to create ABAP CDS Views with parameters in ABAP for HANA. Input parameters are used to restrict the data from CDS Views. Please click here to access all tutorials related to ABAP CDS Views. Lets get started.

Prerequisites

  • You have access to minimum ABAP Netweaver 7.4 system.
  • You have installed Eclipse IDE( Kepler/Juno version ) on your local machine.Click here to know more about.
  • You have installed ABAP Development Tools in Eclipse IDE.
  • You have created ABAP Project in eclipse to connect to ABAP Netweaver 7.4 system.Click here to know how to create ABAP Project.
  • You have basic understanding of ABAP CDS Views.

Step-by-Step Procedure

1. Choose the package in which you want to create ABAP CDS Views. Right-click on the package → New → Other ABAP Repository Object.New ABAP Repository Object

2. In the New ABAP Repository Object window, search for DDL source object by typing in search field.Select the DDL Source and hit Next.

Search for DDL Source

3. In the New DDL Source window, enter Name and Description of the CDS View and hit Finish.

4. A new ABAP CDS view editor opens up like below and paste the below code.

@AbapCatalog.sqlViewName: 'Z_CDS_PARAMS'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'CDS View with Parameters'

define view Z_Cds_With_Params 
       with parameters p_billing_status  :SNWD_SO_CF_STATUS_CODE, 
                       p_delivery_status :SNWD_SO_OR_STATUS_CODE                
       as select from snwd_so 
       join snwd_so_i on snwd_so.node_key = snwd_so_i.parent_key{
            snwd_so.so_id             as orderno,
            snwd_so_i.so_item_pos     as itemno, 
            snwd_so_i.currency_code   as currency,
            snwd_so_i.gross_amount    as grossamount,
            snwd_so_i.net_amount      as netamount,
            snwd_so_i.tax_amount      as taxamount
                         
        } where snwd_so.billing_status  = :p_billing_status and
                snwd_so.delivery_status = $parameters.p_delivery_status;

 

5. Lets observe the code

Line 8-9: We can provide the parameters to the ABAP CDS Views by adding the syntax WITH PARAMETERS p1, p2… Data Dictionary ABAP data types can be used while defining the parameters to the ABAP CDS Views.

Line 19-20: We can use the parameters p1, p2.. in CDS Views using the syntax :p1 or $parameters:p1

6. Now we will see how to call the ABAP CDS views with input parameters in an ABAP program using Open SQL statement. Create an ABAP program in eclipse ADT or SE38 transaction. Below is the code snippet to call the ABAP CDS Views with input parameters.

*-----------------------------------------------------------------*
* Use Open SQL statement to get the data from the ABAP CDS views  *
* with Input Parameters                                           *
*-----------------------------------------------------------------*
SELECT orderno,
       itemno,
       currency,
       grossamount,
       netamount,
       bill_status
       FROM z_cds_params( p_billing_status  = 'P',
                          p_delivery_status = 'D' )
       INTO TABLE @DATA(lt_result).
  
* Display    
 cl_demo_output=>display( lt_result ).

 

Input parameters can be used in different places in the SELECT of the ABAP CDS view. Below are ABAP CDS views examples in different forms.

1Parameters used in SELECT list

In the below CDS view parameter “p_billing_status” is used in the SELECT list

@AbapCatalog.sqlViewName: 'z_cds_params'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'cds view with parameters'

define view z_cds_with_params
       with parameters p_billing_status  :snwd_so_cf_status_code,
                       p_delivery_status :snwd_so_or_status_code
       as select from snwd_so
       join snwd_so_i on snwd_so.node_key = snwd_so_i.parent_key{
            snwd_so.so_id             as orderno,
            snwd_so_i.so_item_pos     as itemno,
            snwd_so_i.currency_code   as currency,
            snwd_so_i.gross_amount    as grossamount,
            snwd_so_i.net_amount      as netamount,
            snwd_so_i.tax_amount      as taxamount,

            //Parameters used in SELECT list
            :p_billing_status         as billing_status

        } where snwd_so.billing_status  = :p_billing_status and
                snwd_so.delivery_status = $parameters.p_delivery_status;

 

2Parameters used in Arithmetic Expressions

In below CDS view code parameter “p_discount_percentage” is used in the arithmetic expressions to calculate the discount amount.

@AbapCatalog.sqlViewName: 'z_cds_params'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'cds view with parameters'

define view z_cds_with_params
       with parameters p_billing_status      :snwd_so_cf_status_code,
                       p_discount_percentage :int4   
       as select from snwd_so
       join snwd_so_i on snwd_so.node_key = snwd_so_i.parent_key{
            snwd_so.so_id             as orderno,
            snwd_so_i.so_item_pos     as itemno,
            snwd_so_i.currency_code   as currency,
            snwd_so_i.gross_amount    as grossamount,
            snwd_so_i.net_amount      as netamount,
            
            // Parameter used in Arthimetic expressions
            (snwd_so_i.net_amount * :p_discount_percentage) as discount_amt

        } where snwd_so.billing_status  = :p_billing_status;

 

3Parameters used in CASE statement

In the below CDS view code the input parameter “p_billing_status” is used in CASE distinction.

@AbapCatalog.sqlViewName: 'z_cds_params'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'cds view with parameters'

define view z_cds_with_params
       with parameters p_billing_status      :snwd_so_cf_status_code
       as select from snwd_so
       join snwd_so_i on snwd_so.node_key = snwd_so_i.parent_key{
            snwd_so.so_id             as orderno,
            snwd_so_i.so_item_pos     as itemno,
            snwd_so_i.currency_code   as currency,
            snwd_so_i.gross_amount    as grossamount,
            snwd_so_i.net_amount      as netamount,
            
            // Parameter used in CASE distinction
            case :p_billing_status
                when 'P' then 'Paid'
                else 'Un Paid'
            end                       as bill_status
                                              
        } where snwd_so.billing_status  = :p_billing_status;

 

Congrats! You have successfully created ABAP CDS Views with Input Parameters. Please stay tuned for ABAP CDS Views tutorials. Leave a comment in the below comment section and let us know your feedback.

  • selcuk

    perfect :)) We want more tutorial for CDS .