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:

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:

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:


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:


Conclusion