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:
with parameters p_carrid: s_carrid
: This defines a parameterp_carrid
of types_carrid
(which matches the type of thecarrid
field in thesflight
table).where carrid = :p_carrid
: The parameterp_carrid
is used to filter the results of the view based on thecarrid
field. The colon (:
) in front ofp_carrid
is used to reference the parameter.
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:
- This CDS view has three parameters:
p_carrid
: A parameter for filtering by carrier ID (carrid
).p_minprice
: A parameter for the minimum price.p_maxprice
: A parameter for the maximum price.- The
WHERE
clause filters the data based on thecarrid
and theprice
range (p_minprice
top_maxprice
).
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_enddategroup by carrid
Explanation:
- This view calculates the total price per carrier within a specific date range. The parameters
p_carrid
,p_startdate
, andp_enddate
are used to filter the results dynamically. sum(price) as total_price
: This aggregates the total price for each carrier.- The
WHERE
clause uses the parameters to filter bycarrid
and the date range (fldate
betweenp_startdate
andp_enddate
).
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 ABAP code:
- We first define the internal table
lt_flights
to store the result of the CDS view. - We define local variables
lv_carrid
,lv_minprice
, andlv_maxprice
to hold the values for the parameters. - Then, we use
SELECT
to query thezflight_filtered
CDS view, passing the values oflv_carrid
,lv_minprice
, andlv_maxprice
as filters.
In this way, the CDS view is filtered dynamically based on the values provided by the ABAP program at runtime.
Key Points to Remember:
- CDS Parameters: Use the
WITH PARAMETERS
clause to define parameters for dynamic filtering in a CDS view. - Filtering: You can use these parameters in the
WHERE
clause to filter data based on user input. - ABAP Layer Integration: When consuming CDS views in ABAP, parameters can be passed and used for filtering the results directly in the ABAP code.
- 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.