来自内部表的动态查询

时间:2017-07-01 22:10:39

标签: sap abap

我有一个要求,我想动态运行此查询。有没有人研究过这种查询?

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

2 个答案:

答案 0 :(得分:4)

READ TABLE在指定辅助密钥(here)和关键组件(here)时具有动态语法,但是以有限的方式。

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

  

动态WHERE条件cond_syntax可以指定为   字符式数据对象或具有字符行的标准表   型

虚拟代码:

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

答案 1 :(得分:2)

这是另一种可能的解决方案,基于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 '$2'.
    <fld> = string.
    ASSIGN (valname) TO <fld>. "assigning value
    string = <fs_cond>.
    REPLACE REGEX `(.*EQ\s)'(.*)('\s*\))` IN string WITH '$2'.
    <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表格上。

  1. 首先,在动态选择屏幕上,我们指定必要的选择字段(READ TABLE的键)及其值。
  2. 然后FREE_SELECTIONS_INIT FM基于这些字段构建WHERE条件。
  3. 我们解析此条件并获取填充的字段名称和值。
  4. READ TABLE语句模板中,只会忽略空字段,并且只能通过填充键读取表。