首页 文章

使用动态密钥规范读取itab

提问于
浏览
7

我试图用一把钥匙读它(例如 mandt ) . 问题是我得到错误,“指定的类型没有结构,因此没有名为MANDT的组件” .

我已经调试了,我可以看到已经成功填充并且表(字段名称)的结构是正确的 . 当我尝试将表读入工作区时,问题出现了 . 也许我做错了,但似乎应该有可能做的事情,我有一种感觉我错过了一些小事 .

我尝试这个的原因是我发现程序中发生了相同的选择,并希望缓冲内存中的记录并从那里读取它们以避免数据库访问 . 这很容易实现,但是当我尝试优化的OPEN SQL语句的表, where 子句和 into 子句是动态的时,我还没有这样做 .

干杯 .

DATA: t681_rep TYPE TABLE OF t681 , wa_681 LIKE LINE OF t681_rep,
      tabref TYPE REF TO data , waref TYPE REF TO data.

FIELD-SYMBOLS: <any_tab> TYPE ANY TABLE,
               <any_wa> TYPE ANY,
               <var1> TYPE ANY.
"fill t681_rep
SELECT *
  FROM t681
  INTO TABLE t681_rep
   UP TO 1 ROWS WHERE kotab = 'A002'.

READ TABLE t681_rep INTO wa_681 WITH KEY kotab = 'A002'.
IF sy-subrc = 0.

  "if A002 is found create a table of that type and fill it
  CREATE DATA tabref TYPE TABLE OF (wa_681-kotab).
  ASSIGN tabref->* TO <any_tab>.
  SELECT * UP TO 10 ROWS
    FROM (wa_681-kotab)
    INTO TABLE <any_tab>.

ENDIF.

CREATE DATA waref TYPE a002.
ASSIGN waref->* TO <any_wa>.

READ TABLE <any_tab> ASSIGNING <any_wa> WITH KEY mandt = '800'. <- problem area
IF sy-subrc = 0.
  "do stuff with <any_wa>...
ENDIF.

5 回答

  • 4

    您只需将字段名称放在括号中即可 .

    data: field type string.
    field = 'MANDT'.
    READ TABLE <any_tab> ASSIGNING <any_wa> WITH KEY (field) = '800'. 
    IF sy-subrc = 0.
      "do stuff with <any_wa>...
    ENDIF.
    
  • 1

    你试图在效率上击败数据库,这是一场失败的战斗 .

    只需转到SE11,选择您的表格,转到技术设置并更改技术设置(缓冲和缓冲类型),您不需要对象修改密钥 . 您还可以确保尺寸类别正确 .

  • 0

    AFAIK,你必须做'很长的路':

    FIELD-SYMBOLS: <any_field> TYPE any.    
    LOOP AT <any_tab> ASSIGNING <any_wa>.
      ASSIGN COMPONENT 'MANDT' OF STRUCTURE <any_wa> TO <any_field>.
      IF <any_field> <> 800.
       CONTINUE.
      ENDIF.
      " do stuff with <any_wa> - you will have to assign <any_field> again to access fields.
    ENDLOOP.
    
  • 2

    您可以使用RTTS获取表键 .

    data table_name type string.
    table_name = 'A002'.
    
    " Dynamically create the table type
    data the_table type ref to data.
    create data the_table type table of (table_name).
    
    " Use RTTS to get table keys
    data typedescription type ref to cl_abap_tabledescr.
    typedescription ?= cl_abap_tabledescr=>describe_by_data_ref( the_table ).
    data keys type abap_table_keydescr_tab.
    keys = typedescription->get_keys( ).
    
  • 1
    REPORT  y_test_dynamic_table.
    DATA: table_name TYPE string,
     typedescription TYPE REF TO cl_abap_tabledescr,
     keys TYPE abap_keydescr_tab,
     ls_key TYPE abap_keyname.
    
    table_name = 'ZYFRM_STG'.
    
    " Dynamically create the table type
    DATA the_table TYPE REF TO data.
    CREATE DATA the_table TYPE TABLE OF (table_name).
    
    " Use RTTS to get table keys
    
    typedescription ?= cl_abap_tabledescr=>describe_by_data_ref( the_table ).
    
    keys = typedescription->KEY.
    
    loop at keys   INTO ls_key.
    ***
    ENDLOOP.
    

相关问题