2017-07-01 47 views
2

我有一個需要動態運行此查詢的需求。有沒有人有過這種查詢?來自內部表的動態查詢

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

你需要動態的部分是什麼? – szako

+0

他們的關鍵部分是我掙扎的地方 –

+0

[可以使用任意數量的關鍵字段規範動態創建READ TABLE語句嗎?](https://stackoverflow.com/questions/18869119/can-a-read -table-statement-be-created-dynamic-with-an-any-of-ke) – vwegert

回答

1

根據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語句模板中,空白字段僅被忽略,表格僅通過填充鍵讀取。
3

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.