Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  User Dialogs →  Selection Screens →  Dynamic Selections 

Dynamic Selections

This example demonstrates how a free selection is used in a program.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA selid         TYPE  rsdynsel-selid.
    DATA field_tab     TYPE TABLE OF rsdsfields.
    DATA table_tab     TYPE TABLE OF rsdstabs.
    DATA cond_tab      TYPE rsds_twhere.
    DATA dref          TYPE REF TO data.

    FIELD-SYMBOLS <table> TYPE STANDARD TABLE.
    FIELD-SYMBOLS <cond>  LIKE LINE OF cond_tab.

    DATA(checked_dbtab) = demo=>check_existence_and_authority( ).

    table_tab = VALUE #( ( prim_tab = checked_dbtab ) ).
    CALL FUNCTION 'FREE_SELECTIONS_INIT'
      EXPORTING
        kind         = 'T'
      IMPORTING
        selection_id = selid
      TABLES
        tables_tab   = table_tab
      EXCEPTIONS
        OTHERS       = 4.
    IF sy-subrc <> 0.
      MESSAGE 'Error in initialization' TYPE 'I' DISPLAY LIKE 'E'.
      LEAVE PROGRAM.
    ENDIF.

    CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
      EXPORTING
        selection_id  = selid
        title         = 'Free Selection'
        as_window     = ' '
      IMPORTING
        where_clauses = cond_tab
      TABLES
        fields_tab    = field_tab
      EXCEPTIONS
        OTHERS        = 4.
    IF sy-subrc <> 0.
      MESSAGE 'No free selection created' TYPE 'I'.
      LEAVE PROGRAM.
    ENDIF.

    ASSIGN cond_tab[ tablename = checked_dbtab ] TO <cond>.
    IF sy-subrc <> 0.
      MESSAGE 'Error in condition' TYPE 'I' DISPLAY LIKE 'E'.
      LEAVE PROGRAM.
    ENDIF.

    CREATE DATA dref TYPE TABLE OF (checked_dbtab).
    ASSIGN dref->* TO <table>.

    TRY.
        SELECT *
               FROM (checked_dbtab)
               WHERE (<cond>-where_tab)
               INTO TABLE @<table>.
      CATCH cx_sy_dynamic_osql_error.
        MESSAGE 'Error in dynamic Open SQL' TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
    ENDTRY.

    TRY.
        cl_salv_table=>factory(
          IMPORTING r_salv_table = DATA(alv)
          CHANGING  t_table      = <table> ).
        alv->display( ).
      CATCH cx_salv_msg.
        MESSAGE 'Error in ALV display' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.

Description

This example shows the simplest way to use a free selection in a program. When the value "T" of the parameter KIND is passed, the function module FREE_SELECTIONS_INIT is configured so that free selections are prepared for database tables in ABAP Dictionary. The names of the database tables (here only one freely selectable table) are passed to the table parameter tables_tab.

The result of FREE_SELECTIONS_INIT is passed to the function module FREE_SELECTIONS_DIALOG, which displays a selection screen for entering free selections for the database table. The user can select which database fields are used for free selections, and can then make these selections.

Once the user confirms the selected free selections by choosing Save, the program inherits them as a dynamic WHERE clause and then uses this clause in a dynamic SELECT statement to read the data accordingly. The result is displayed in an ALV list.

The method CHECK_TABLE_NAME_STR of the class CL_ABAP_DYN_PRG checks whether the database table specified exists and can be used.