ABAP SELECT-OPTIONS: Defining Complex Selection Criteria

Category
ABAP-Statements
Published
Author
Johannes

The SELECT-OPTIONS statement is a special ABAP keyword that you use exclusively in the declaration part of executable programs (REPORT), function groups, or module pools. It serves to define complex selection criteria on the standard selection screen (Dynpro 1000) of a program.

Unlike simple PARAMETERS, which only generate a single input field, SELECT-OPTIONS allow the user at runtime to enter:

  • Single values
  • Intervals (from-to ranges)
  • Multiple single values and intervals (via the multiple selection button)
  • Exclusions of values or intervals
  • Pattern matches (e.g., ‘ABC*’)

The defined selection options are typically used in the WHERE clause of SELECT statements to dynamically and flexibly filter database queries.

Syntax

SELECT-OPTIONS <selcrit> FOR <field>
[DEFAULT <value> [TO <value2>] [OPTION <op>] [SIGN <sg>]] " Default values
[OBLIGATORY] " Input required
[NO-EXTENSION] " No multiple selection (only one row)
[NO INTERVALS] " No interval input (only single values/patterns)
[LOWER CASE] " Allow/preserve lowercase
[MEMORY ID <pid>] " SAP Memory ID for pre-filling
[VISIBLE LENGTH <vlen>] " Visible length of fields on screen
[MODIF ID <mid>]. " Modification group for dynamic screen adjustment
" [MATCHCODE OBJECT ...] " Deprecated for F4 help, better via Dictionary reference in FOR
" [NO DATABASE SELECTION] " Usually for SAP internal purposes
  • <selcrit>: The name of the selection criterion (e.g., s_kunnr, so_matnr). Internally, a ranges table is created under this name.
  • FOR <field>: Very important! Specifies the data object that the criterion refers to and whose data type it inherits. This is often a database table field (e.g., mara-matnr) or a local variable (my_variable). The type determines the properties of the LOW and HIGH fields of the ranges table and influences input checks as well as F4 help.

How It Works (What Happens Behind the Scenes?)

The statement SELECT-OPTIONS <selcrit> FOR ... implicitly declares an internal table named <selcrit>. This table has a fixed structure (ranges structure) with four columns:

  1. SIGN (Character 1): Contains ‘I’ for Inclusive (Include) or ‘E’ for Exclusive (Exclude).
  2. OPTION (Character 2): Contains the comparison operator. Common values are:
    • EQ: Equals
    • NE: Not Equal
    • BT: Between
    • NB: Not Between
    • CP: Contains Pattern (e.g., A*)
    • NP: Not contains Pattern
    • GT: Greater Than
    • GE: Greater Equal
    • LT: Less Than
    • LE: Less Equal
  3. LOW: The lower limit or the single value. The data type is inherited from the field in the FOR clause.
  4. HIGH: The upper limit (only relevant with OPTION = BT or NB). Same data type as LOW.

On the selection screen, the user enters values, which the system then writes to this internal table <selcrit> when executing the report.

Important Additions (Options) Explained

  • DEFAULT <value> [TO <value2>] ...: Pre-fills the criterion on the screen with one or more default values.
  • OBLIGATORY: Makes at least one entry in this criterion mandatory. The program cannot be started without input.
  • NO-EXTENSION: Hides the “multiple selection” button (to the right of the input fields). The user can only enter the directly visible row (one value or one interval).
  • NO INTERVALS: Hides the “to” field (HIGH field). The user can only enter single values (EQ, NE, GT, etc.) or patterns (CP, NP), not ranges (BT, NB).
  • LOWER CASE: Allows entering lowercase letters and prevents automatic conversion to uppercase.

Usage in ABAP SQL (Database Queries)

The ranges table <selcrit> filled by the user is used in the WHERE clause of a SELECT statement with the IN operator to filter the database selection:

SELECT carrid, connid, fldate
FROM spfli
WHERE carrid IN @s_carrid " s_carrid is the SELECT-OPTIONS
AND connid IN @s_connid " s_connid is another SELECT-OPTIONS
INTO TABLE @DATA(lt_flights).

The database then selects only the rows whose field values meet the conditions defined in the ranges table.

Comparison with PARAMETERS

  • PARAMETERS: Defines a single input field for exactly one value on the selection screen.
  • SELECT-OPTIONS: Defines more complex input options (ranges, multiple values, exclusions, patterns) and internally creates a table for them.

Examples

1. Standard Select-Option for Material Number

REPORT z_demo_selopt.
TABLES: mara. " For FOR clause (or declare local variable)
SELECT-OPTIONS s_matnr FOR mara-matnr.
START-OF-SELECTION.
SELECT matnr, mtart, matkl
FROM mara
WHERE matnr IN @s_matnr " Filter by material numbers
INTO TABLE @DATA(lt_mara).
cl_demo_output=>display( lt_mara ).

(Creates input fields for material from/to on the selection screen and the multiple selection button)

2. Date Range with Default Value (today) and Mandatory Input

SELECT-OPTIONS s_date FOR sy-datum OBLIGATORY DEFAULT sy-datum.

(Creates a date from/to field that is pre-filled with today’s date and must be filled in)

3. Only Single Airlines (No Ranges, No Multiple Selection)

SELECT-OPTIONS s_carrid FOR spfli-carrid NO INTERVALS NO-EXTENSION.

(Creates only a single input field for the airline ID, without “to” field and without multiple selection button)

Best Practices

  • SELECT-OPTIONS are defined in the declaration part of programs that should have a selection screen.
  • The FOR clause is crucial for type, checks, and F4 help. It should reference a Dictionary field or a variable with a matching type.
  • The generated ranges table is used with the IN operator in the WHERE clause.
  • Use PARAMETERS for simple input values and SELECT-OPTIONS for more complex selection criteria.