In SAP ABAP CDS Views, Select Options are a feature that allows you to create dynamic filters for your views, enabling the user to filter the data based on specific criteria at runtime. Select options are commonly used in reports and queries where users may need to specify a range or set of values to filter the data.
However, CDS views don’t directly support the creation of Select Options like traditional ABAP reports. Instead, you would handle user-defined filter criteria in the ABAP layer (where the CDS view is consumed) or implement filtering through parameters in the CDS view itself.
Below, I’ll explain two ways to handle filtering in CDS views: one using Parameters (for filtering) and another using ABAP Select Options in the ABAP program when calling the CDS view.
1. Using Parameters for Filtering in CDS Views
You can use parameters to filter data dynamically within a CDS view. These parameters can be mapped to specific fields, allowing users to provide input at runtime.
Example: Filtering by a Field Using Parameters
@AbapCatalog.sqlViewName: 'ZFLIGHT_CDS'@EndUserText.label: 'Filtered Flight Data Based on 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:
- In this example, the CDS view
ZFLIGHT_CDS
defines a parameterp_carrid
, which filters the data based on thecarrid
field. - The parameter
p_carrid
allows users to provide a value for filtering when the view is executed. - The
where
clause applies the filter conditioncarrid = :p_carrid
.
2. Using Select Options in ABAP for CDS Views
If you want to use Select Options in an ABAP report or program when consuming a CDS view, you need to handle this at the ABAP layer. Select Options can be used to allow users to input a range of values, and then the corresponding filter is applied when querying the CDS view.
Example: Using Select Options in ABAP with a CDS View
SELECT-OPTIONS: so_carrid FOR sflight-carrid. " Select Option for Carrier ID
DATA: lt_flights TYPE TABLE OF zflight_cds. " Table type for storing data from the CDS view
SELECT * FROM zflight_cds INTO TABLE lt_flights WHERE carrid IN so_carrid. " Applying the Select Option filter
Explanation:
- In this ABAP code, we define a Select Option
so_carrid
for thecarrid
field of thesflight
table. - Then, we fetch the data from the
ZFLIGHT_CDS
view using theSELECT
statement, where we apply the IN condition to thecarrid
field, filtering the data based on the values entered in the Select Optionso_carrid
.
3. Using Dynamic Filter (with Multiple Values or Ranges) in CDS Views
You can allow users to filter data using ranges or multiple values by dynamically passing parameters into the CDS view.
Example: Using a Range of Values in CDS
@AbapCatalog.sqlViewName: 'ZFLIGHT_RANGES'@EndUserText.label: 'Filtered Flight Data Based on Multiple Conditions'define view ZFLIGHT_RANGES with parameters p_minprice: abap.decimal(13,2), p_maxprice: abap.decimal(13,2) as select from sflight { key carrid, key connid, fldate, price}where price between :p_minprice and :p_maxprice
Explanation:
- In this example, we define two parameters:
p_minprice
andp_maxprice
, which allow the user to input a range for theprice
field in the CDS view. - The
where
clause filters the records to include only those where theprice
is betweenp_minprice
andp_maxprice
.
4. Using Filter Conditions in the WHERE Clause (For Multiple Filters)
Instead of relying on a single parameter, you can also combine multiple filtering conditions in the WHERE
clause to create complex filter logic.
Example: Multiple Filter Conditions in CDS
@AbapCatalog.sqlViewName: 'ZFLIGHT_CDS_FILTER'@EndUserText.label: 'Filtered Flight Data with Multiple Conditions'define view ZFLIGHT_CDS_FILTER 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 view filters data based on both
carrid
(with the parameterp_carrid
) and a price range (p_minprice
top_maxprice
).
Conclusion
- Select Options are not directly supported in CDS Views, but you can handle dynamic filtering by using parameters within the CDS views themselves or through ABAP Select Options when consuming CDS views in an ABAP program.
- Parameters in CDS views allow for filtering data by specific criteria, while ABAP Select Options can be used in the calling ABAP program to pass dynamic filter criteria to the CDS view query.
- Use parameters to pass filtering criteria directly to the CDS view, or use ABAP Select Options to filter data when querying the CDS view in ABAP reports.