Currently Being Moderated

Hi folks,

 

It's quiet a time that I have published my last ABAP Geek but yesterday I stumbled across a feature that I don't want to hold back.

You might be well aware of the so called "Dynamic Selections" that are offered at the selection screens of good old logical databases. They allow you to define selections for nodes of the logical database that are not offered statically. But did you know how easy it is to utilize these dynamic selections within any program? Well, I didn't.

All you have to do is to call the two function modules FREE_SELECTIONS_INIT and FREE_SELECTIONS_DIALOG. The following example source code is a kind of fully fledged data browser for arbitrary database tables that allows you to freely define the selection conditions:

 

PARAMETERS dbtab TYPE tabname DEFAULT 'SPFLI'.

TYPE-POOLS rsds.

DATA tadir_wa  TYPE tadir.
DATA selid     TYPE  rsdynsel-selid.
DATA field_tab TYPE TABLE OF rsdsfields.
DATA table_tab TYPE TABLE OF rsdstabs.
DATA table     LIKE LINE OF table_tab.
DATA cond_tab  TYPE  rsds_twhere.
DATA cond      LIKE LINE OF cond_tab.
DATA dref      TYPE REF TO data.
DATA alv       TYPE REF TO cl_salv_table.

FIELD-SYMBOLS 

 TYPE STANDARD TABLE.

SELECT SINGLE *
       FROM tadir
       INTO tadir_wa
       WHERE pgmid = 'R3TR' AND
             object = 'TABL' AND
             obj_name = dbtab.
IF sy-subrc <> 0.
  MESSAGE 'Database not found' TYPE 'I' DISPLAY LIKE 'E'.
  LEAVE PROGRAM.
ENDIF.

table-prim_tab = dbtab.
APPEND table TO table_tab.
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.

READ TABLE cond_tab WITH KEY tablename = dbtab INTO cond.
IF sy-subrc <> 0.
  MESSAGE 'Error in condition' TYPE 'I' DISPLAY LIKE 'E'.
  LEAVE PROGRAM.
ENDIF.

CREATE DATA dref TYPE TABLE OF (dbtab).
ASSIGN dref->* TO .

TRY.
    SELECT *
           FROM (dbtab)
           INTO TABLE 
           WHERE (cond-where_tab).
  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 = alv
      CHANGING  t_table      =  ).
    alv->display( ).
  CATCH cx_salv_msg.
    MESSAGE 'Error in ALV display' TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.  

 

Passing kind "T" to function module FREE_SELECTIONS_INIT prepares a dynamic selection for a database table entered via a parameter. Passing the result of FREE_SELECTIONS_INIT to FREE_SELECTIONS_DIALOG calls a special selection screen, where you can select the database fields and the respective selections.

After selecting Save, you receive a ready to use dynamic WHERE-condition via parameter WHERE_CLAUSES (another parameter FIELD_RANGES would return a respective RANGES table).

The rest is history: a SELECT statement with dynamic tokens, a target table that is created dynamiclally, and some ALV display.

Cheers!

 

Horst

 

PS: Of course, this program is part of the ABAP Example Library now. 

 

 

 

 

 

 

 

Comments

Actions

Filter Blog

By date:
By tag: