In SAP ABAP CDS Views, you can use parameters to allow dynamic filtering of data based on user input at runtime. These parameters can be defined directly in the CDS view and can be used to filter the data queried by the CDS view.

How to Use Parameters in CDS Views:

Parameters in CDS views are defined using the with parameters clause. This allows you to pass values into the CDS view when it is called or executed, and these values can be used to filter the data.

Basic Syntax for Defining Parameters in CDS Views:

@AbapCatalog.sqlViewName: 'ZFLIGHT_CDS'
@EndUserText.label: 'Flight Data Filtered by Carrier'
define view ZFLIGHT_CDS with parameters p_carrid: s_carrid as select from sflight {
key carrid,
key connid,
fldate,
price
}
where carrid = :p_carrid

Explanation:

When the CDS view is executed, the user must provide a value for the parameter p_carrid.


Example: Filtering by Multiple Parameters

You can also use multiple parameters in your CDS view to create more complex filtering logic.

@AbapCatalog.sqlViewName: 'ZFLIGHT_FILTERED'
@EndUserText.label: 'Filtered Flight Data Based on Multiple Parameters'
define view ZFLIGHT_FILTERED with parameters p_carrid: s_carrid, p_minprice: abap.decimal(13,2), p_maxprice: abap.decimal(13,2) as select from sflight {
key carrid,
key connid,
fldate,
price
}
where carrid = :p_carrid
and price between :p_minprice and :p_maxprice

Explanation:


Using Parameters with Aggregate Functions

You can also use parameters in conjunction with aggregate functions such as SUM, COUNT, etc., in the CDS view.

@AbapCatalog.sqlViewName: 'ZFLIGHT_AGGREGATED'
@EndUserText.label: 'Total Price per Carrier Filtered by Date'
define view ZFLIGHT_AGGREGATED with parameters p_carrid: s_carrid, p_startdate: abap.dats, p_enddate: abap.dats as select from sflight {
key carrid,
sum(price) as total_price
}
where carrid = :p_carrid
and fldate between :p_startdate and :p_enddate
group by carrid

Explanation:


Using Parameters in ABAP to Call CDS Views

Once you define a CDS view with parameters, you can use it in your ABAP code and pass values for those parameters when calling the view.

Example: Calling a CDS View with Parameters in ABAP

DATA: lt_flights TYPE TABLE OF zflight_cds,
lv_carrid TYPE s_carrid,
lv_minprice TYPE abap.decimal(13,2),
lv_maxprice TYPE abap.decimal(13,2).
lv_carrid = 'LH'.
lv_minprice = 100.
lv_maxprice = 500.
SELECT * FROM zflight_filtered
INTO TABLE lt_flights
WHERE carrid = @lv_carrid
AND price BETWEEN @lv_minprice AND @lv_maxprice.

Explanation:

In this way, the CDS view is filtered dynamically based on the values provided by the ABAP program at runtime.


Key Points to Remember:

  1. CDS Parameters: Use the WITH PARAMETERS clause to define parameters for dynamic filtering in a CDS view.
  2. Filtering: You can use these parameters in the WHERE clause to filter data based on user input.
  3. ABAP Layer Integration: When consuming CDS views in ABAP, parameters can be passed and used for filtering the results directly in the ABAP code.
  4. Multiple Parameters: You can use multiple parameters together to create complex filters (e.g., range filters, multiple conditions).

By using parameters effectively, you can create flexible and reusable CDS views that adapt to different data filtering scenarios.