Creating a CDS View with Parameters and Executing It in ABAP

Before proceeding, ensure you have completed our previous lesson on creating basic CDS views. Additionally, you may want to explore how to publish your CDS views as an OData service.

What you will learn?
1. Create a CDS With Parameters
2. Execute the CDS using ABAP program
Prerequisites
1. SAP System Access: Developer role, SE38
2. Eclipse software

By adding “with parameters“, you can define input parameters in your CDS views. If you need multiple parameters, separate them using commas.

Check out the syntax below: Each parameter name is followed by its data type.

define view <CDS_NAME>
with parameters <param_1>: <data_type>,
                <param_2>: <data_type> as select from table

Now, follow these steps to create a CDS View with parameters and call it using an ABAP program for faster data filtering and retrieval.

Step 1. Right click your Package, select New -> Other ABAP Repository Object

Step 2. Select Core Data Service -> Data Definition and the click Next.

Step 3. Input the CDS’s name and description, click the next button.

Step 4. Select an existing transport request or create a new one, and then click the Next button.

Step 5. Select defineView and the click the Finish button.

Step 6. Write this SQL script into your CDS.

@AbapCatalog.sqlViewName: 'ZDSSFLIGHT'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS with parameters'
@Metadata.ignorePropagatedAnnotations: true
define view ZCDSFLIGHTWITHPARAM 
with parameters so_carr_id: s_carr_id,
                so_fr_fldate: s_date,
                so_to_fldate: s_date
                
              as select from scarr as s
                 inner join sflight as f
                 on s.carrid = f.carrid
{
    s.carrid,
    s.carrname,
    f.fldate,
    f.planetype,
    f.paymentsum,
    f.currency

}

where s.carrid = $parameters.so_carr_id 
  and f.fldate between $parameters.so_fr_fldate and $parameters.so_to_fldate
 

Step 7. Save and Activate the CDS.

Step 8. Test the CDS, click Run -> Run As -> ABAP Application.

Step 9. Input all the parameters. Click Open Data Preview.

Step 10. Here’s the result, As you can see, the flight date is filtered based on the specified parameters.

Step 11. Now create an ABAP Program and write below code.

REPORT abap01.
TABLES: sflight.

PARAMETERS: p_carr TYPE scarr-carrid.
SELECT-OPTIONS: s_fldate FOR sflight-fldate.

START-OF-SELECTION.

SELECT * FROM ZDSSFLIGHT(
      so_carr_id = @p_carr ,
      so_fr_fldate = @s_fldate-low,
      so_to_fldate = @s_fldate-high )
  INTO TABLE @DATA(it_sflight).

  cl_demo_output=>display( it_sflight ).

Step 12. Run the program and input all the parameters. Click the Execute Button.

Here’s the result.

Share this tutorial with Fellow Geeks!

Other ABAP Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *