来自内部 table 的动态查询

Dynamic query from internal table

我有一个要求,我想动态地 运行 这个查询。有没有人处理过这种查询?

READ TABLE table_name TRANSPORTING feild_name INTO table_name
              WITH KEY key_feild1 = value1,
                       key_feild2 = value2,
                       key_feild3 = value3.

READ TABLE 在指定辅助键 (here) and key components (here) 时具有动态语法,但方式有限。

我认为 LOOP AT 是您正在寻找的命令。有关用法和示例的更多详细信息 here。看第 4 部分:

Dynamic WHERE Condition cond_syntax can be specified as a character-like data object or standard table with character-like row type

虚拟代码:

LOOP AT table INTO line WHERE (condition).
  EXIT. --> Exit at first found line.
ENDLOOP.
IF sy-subrc = 0.
...
ENDIF.

这是另一种可能的解决方案,基于 this answer。虽然有点难看,但确实有用

DATA: table_tab TYPE TABLE OF rsdstabs,
      selid     TYPE  rsdynsel-selid,
      cond_tab  TYPE rsds_twhere,
      field_tab TYPE TABLE OF rsdsfields,
      string    TYPE string.
FIELD-SYMBOLS <cond>  LIKE LINE OF cond_tab.

table_tab = VALUE #( ( prim_tab = 'USR02') ).

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.

FIELD-SYMBOLS: <fld> TYPE any.
DATA: field1  TYPE c LENGTH 10, value1 TYPE string,
      field2  TYPE c LENGTH 10, value2 TYPE string,
      field3  TYPE c LENGTH 10, value3 TYPE string,
      field4  TYPE c LENGTH 10, value4 TYPE string,
      field5  TYPE c LENGTH 10, value5 TYPE string,
      field6  TYPE c LENGTH 10, value6 TYPE string,
      field7  TYPE c LENGTH 10, value7 TYPE string,
      field8  TYPE c LENGTH 10, value8 TYPE string,
      field9  TYPE c LENGTH 10, value9 TYPE string,
      num1(1) TYPE n,
      fldname TYPE fieldname,
      valname TYPE fieldname.

ASSIGN cond_tab[ tablename = 'USR02' ] TO <cond>.

IF sy-subrc = 0.
  LOOP AT <cond>-where_tab ASSIGNING FIELD-SYMBOL(<fs_cond>).
    num1 = sy-tabix.
    CONCATENATE 'field' num1 INTO fldname.
    CONCATENATE 'value' num1 INTO valname.
    ASSIGN (fldname) TO <fld>. "assigning field name
    string = <fs_cond>.
    REPLACE REGEX `(^.*\(\s)(.*)(EQ.*)` IN string WITH ''.
    <fld> = string.
    ASSIGN (valname) TO <fld>. "assigning value
    string = <fs_cond>.
    REPLACE REGEX `(.*EQ\s)'(.*)('\s*\))` IN string WITH ''.
    <fld> = string.
  ENDLOOP.
ENDIF.

SELECT * 
  FROM usr02
INTO TABLE @DATA(lt_usr).

READ TABLE lt_usr ASSIGNING FIELD-SYMBOL(<fs_usr>)
WITH  KEY  (field1) = value1
           (field2) = value2
           (field3) = value3
           (field4) = value4
           (field5) = value5
           (field6) = value6
           (field7) = value7
           (field8) = value8
           (field9) = value9.

示例基于 USR02 table。

  1. 首先,在动态选择屏幕上,我们指定必要的选择字段(READ TABLE 的键)及其值。
  2. 然后FREE_SELECTIONS_INITFM根据这些字段构建WHERE条件
  3. 我们解析此条件并获取填充的字段名称和值。
  4. READ TABLE 语句模板中,空字段将被简单地忽略,并且 table 仅由填充的键读取。