matinal:S/4 hana系统中,内表Internal Table的改变

2023-10-13 16:57:23 浏览数 (2)

示例代码:

代码语言:javascript复制
"test6
"secondary key
FORM f_test6.
  DATA:lt_spfli TYPE TABLE OF spfli
        WITH UNIQUE HASHED KEY k_hash COMPONENTS carrid connid countryfr
        WITH NON-UNIQUE SORTED KEY k_sort COMPONENTS carrid connid.
  SELECT * FROM spfli INTO TABLE lt_spfli.

  "loop时使用
  LOOP AT lt_spfli INTO DATA(ls_spfli) USING KEY k_sort WHERE carrid = 'AA' AND connid = '0001'.
  ENDLOOP.
  "read时使用
  READ TABLE lt_spfli INTO ls_spfli WITH KEY k_sort COMPONENTS carrid = 'AA' connid = '0001'.
  "当使用key读出记录需要更新时,必须使用对应key
  "sy-tabix记录的值会被key影响
  "更新时需要指定transporting字段,不能更新key值字段
  MODIFY lt_spfli USING KEY k_sort FROM ls_spfli INDEX sy-tabix
        TRANSPORTING countryfr.
ENDFORM. 

1.16 Internal Table:Processing Internal Tables Using Expressions

示例代码:

代码语言:javascript复制
"test7
"表达式访问内表
FORM f_test7.
  DATA:lt_spfli TYPE TABLE OF spfli
       WITH NON-UNIQUE SORTED KEY k_sort
       COMPONENTS countryfr.
  DATA:ls_spfli LIKE LINE OF lt_spfli.
  DATA:lv_countryfr TYPE spfli-countryfr.
  SELECT * FROM spfli INTO TABLE lt_spfli.

  "line index从1开始
  ls_spfli = lt_spfli[ 1 ].
  WRITE:/ ls_spfli-carrid, ls_spfli-connid.
  "匹配多笔,只返回第一笔记录
  ls_spfli = lt_spfli[ carrid = 'AA' ].
  WRITE:/ ls_spfli-carrid, ls_spfli-connid.
  "使用key
  ls_spfli = lt_spfli[ KEY k_sort COMPONENTS countryfr = 'DE' ].
  WRITE:/ ls_spfli-carrid, ls_spfli-connid.
  "访问结构字段
  lv_countryfr = lt_spfli[ carrid = 'AA' ]-countryfr.

  "通过line index访问行
*  READ TABLE lt_spfli WITH KEY k_sort COMPONENTS countryfr = 'DE' TRANSPORTING NO FIELDS.
*  LOOP AT lt_spfli INTO ls_spfli FROM sy-tabix.
*  ENDLOOP.
  "line_index()方法
  "line_exists()方法
  "需要line_exists先判断访问数据行是否存在,
  "再使用line_index获取数据行
  IF line_exists( lt_spfli[ KEY k_sort COMPONENTS countryfr = 'DE' ] ).
    LOOP AT lt_spfli INTO ls_spfli
      FROM line_index( lt_spfli[ KEY k_sort COMPONENTS countryfr = 'DE' ] ).
    ENDLOOP.
  ENDIF.
ENDFORM.

1.17 Internal Table:Creating Comprehensions And Reductions

简化内表循环处理;

数据汇总方法;

示例代码:

代码语言:javascript复制
"test8
"Creating Comprehensions And Reductions
FORM f_test8.
  DATA:lt_spfli TYPE TABLE OF spfli
       WITH NON-UNIQUE SORTED KEY s_sort COMPONENTS countryfr.
  DATA:lt_spfli2 TYPE TABLE OF spfli.
  DATA:ls_spfli LIKE LINE OF lt_spfli.
  TYPES:BEGIN OF s_ty1,
        carrid TYPE spfli-carrid,
        connid TYPE spfli-connid,

        END OF s_ty1.
  DATA:lt_ty1 TYPE TABLE OF s_ty1.
  DATA:ls_ty1 LIKE LINE OF lt_ty1.

  SELECT * FROM spfli INTO TABLE lt_spfli.
  "使用MOVE-CORRESPONDING进行内表赋值
  MOVE-CORRESPONDING lt_spfli TO lt_ty1.
  "保留lt_ty1已有记录
  "MOVE-CORRESPONDING lt_spfli TO lt_ty1 KEEPING TARGET LINES.
  "如果有嵌套结构赋值
  "MOVE-CORRESPONDING lt_spfli TO lt_ty1 EXPANDING NESTED TABLES.

  "不使用Loop,获取内表部分行
  lt_spfli2 = VALUE #( FOR wa IN lt_spfli
                             WHERE ( carrid = 'AA' )
                            ( wa ) ).
  "结合动态where条件,注意where两边括号不能有空格
  DATA:lv_carrid TYPE string VALUE 'AZ'.
  DATA:lv_connid TYPE string VALUE '0788'.
  DATA:lv_where TYPE string.
  lv_where = | CARRID = '{ lv_carrid }' AND CONNID = '{ lv_connid }' |.
  WRITE:/ lv_where.
  lt_spfli2 = VALUE #( FOR wa IN lt_spfli
                             WHERE (lv_where)
                            ( wa ) ).
  LOOP AT lt_spfli2 INTO DATA(ls_spfli2).
    WRITE:/ ls_spfli2-carrid,ls_spfli2-connid.
  ENDLOOP.

  "通过REDUCE,进行数据汇总
  DATA:lt_sflight TYPE TABLE OF sflight.
  DATA:ls_sflight LIKE LINE OF lt_sflight.
  TYPES:BEGIN OF s_ty2,
        carrid TYPE sflight-carrid,
        connid TYPE sflight-connid,
        sum_occ TYPE i,
        END OF s_ty2.
  DATA:lt_sflight2 TYPE TABLE OF s_ty2.
  DATA:lt_sflight3 TYPE TABLE OF s_ty2.
  DATA:lv_occ_total TYPE I.
  SELECT * FROM sflight INTO TABLE lt_sflight.
  "加总所有seatsocc
  lv_occ_total = REDUCE i( INIT i = 0
                   FOR ls_sflight2 IN lt_sflight
                   NEXT i = i   ls_sflight2-seatsocc
                  ).

  "根据carrid,connid分组汇总seatsocc
  LOOP AT lt_sflight INTO ls_sflight
    GROUP BY ( carrid = ls_sflight-carrid
               connid = ls_sflight-connid
               size = GROUP SIZE )
    ASCENDING ASSIGNING FIELD-SYMBOL(<fs_sflight>).
    lt_sflight2 = VALUE #( (
      carrid = <fs_sflight>-carrid
      connid = <fs_sflight>-connid
      sum_occ = REDUCE i( INIT i = 0
                          FOR line IN GROUP <fs_sflight>
                          NEXT i = i   line-seatsocc ) )
    ).
    MOVE-CORRESPONDING lt_sflight2 TO lt_sflight3 KEEPING TARGET LINES.
  ENDLOOP.
  LOOP AT lt_sflight3 ASSIGNING FIELD-SYMBOL(<fs_sflight3>).
    WRITE:/ <fs_sflight3>-carrid,<fs_sflight3>-connid,<fs_sflight3>-sum_occ.
  ENDLOOP.
ENDFORM. 

1.18 Internal Table:Defining and Using Meshes

通过定义Table之间外键关系,实现多表连接,减少访问时循环次数。

但是查询时,会将多表数据同时查询出来,是否会出现内存溢出?

示例:

代码语言:javascript复制
"test9
"Defining and Using Meshes
"语法:
*TYPES BEGIN OF MESH mesh_type.
* node { TYPE {[REF TO] table_type}|ref_type }
*           | { LIKE {[REF TO] itab      }|dref     }
*                [association1],
*                [association2],
*                ...
*TYPES END OF MESH mesh_type.
"示例:DEMO_MESH*

"Mesh Node
"语法:
"{mesh-|<mesh>-|mesh_ref->}rnode_associ[ ... ]_assoc1[ ... ]_assoc2[ ... ]
"1.{mesh-|<mesh>-|mesh_ref->}rnode
"rnode是mesh中定义的node,可以作为开始node或结束node;
"2. _associ[ source [cond] ]
"通过association获取对应结果node数据,可以指定开始node数据筛选条件;
"3._assoc1[ [cond] ]_assoc2[ [cond] ]
"可以链式使用association访问数据;

"Mesh Association
"语法:
  "_assoc[ |*][ ... ]
  "| _assoc~node[ |*][ ... ]
  "| ^_assoc~node[ |*][ ... ] ...
"示例:DEMO_MESH_REFLEXIVE_ASSOC_SNGL
" ,*主要用于自关联表树状关系
" ,获取开始node下所有子孙节点数据;
"*,包含开始node匹配数据,获取开始node下所有子孙节点数据;
"例如:
"课程ID 上阶课程 描述
"ID   PID  DESC
"1     0    t1
"2     1    t2
"3     2    t3
"4     0    t4
"5     1    t5
*TYPES:BEGIN OF line,
*    id  TYPE i,
*    pid TYPE i,
*    desc TYPE string,
*  END OF line.
*TYPES:t_itab TYPE SORTED TABLE OF line WITH UNIQUE KEY id
*             WITH NON-UNIQUE SORTED KEY by_parent
*             COMPONENTS pid,
*TYPES:BEGIN OF MESH t_mesh,
*        node TYPE t_itab
*        ASSOCIATION _node TO node ON pid = id
*        USING KEY by_parent,
*      END OF MESH t_mesh.
"需要查找所有课程ID为1的下阶课程
"mesh-node_node[ mesh-node[ id = id ] ]
"返回:t2,t5两笔记录
"mesh-node_node [ mesh-node[ id = id ] ]
"返回:t2,t5,t3, t3返回因为他是孙子课程
"mesh-node_node*[ mesh-node[ id = id ] ]
"返回:t2,t5,t3,t1  t1返回因为它符合初始条件

"Mesh Condition
"语法:
"{ col1 = val1 col2 = val2 ... }
"| { [USING KEY key] [WHERE log_exp] }
"设置筛选条件
"[USING KEY key] [WHERE log_exp]
"可以在下面语法中使用:
"LOOP AT mesh_path ...
"FOR  ... IN mesh_path ...
"INSERT ... INTO TABLE mesh_path ...
"MODIFY mesh_path.
"DELETE mesh_path.
"SET ASSOCIATION mesh_path ...

FORM f_test9.
  "定义MESH
  "不能使用standard table
  "TYPES:tt_customers TYPE TABLE OF scustom.
  "TYPES:tt_customers TYPE STANDARD TABLE OF scustom.
  "TYPES:tt_customers TYPE STANDARD TABLE OF scustom
  "      WITH NON-UNIQUE SORTED KEY k_sort COMPONENTS id name.
  "需要使用sorted table,hash table
  TYPES:tt_customers TYPE SORTED TABLE OF scustom WITH NON-UNIQUE KEY id name.
  TYPES:tt_books TYPE HASHED TABLE OF sbook WITH UNIQUE KEY carrid connid fldate bookid.
  TYPES:wa_books TYPE LINE OF tt_books.
  "定义mesh
  TYPES:BEGIN OF MESH m_custom_books,
        customers TYPE tt_customers ASSOCIATION _sbook TO books ON customid = id,
        books TYPE tt_books ASSOCIATION _scutomer TO customers ON id = customid,
       END OF MESH m_custom_books.

  "也可以定义多种key
  "SORT,HASH的UNIQUE KEY必须要指定
  TYPES:tt_scarr TYPE HASHED TABLE OF scarr
        WITH UNIQUE KEY carrid.
  TYPES:tt_spfli TYPE HASHED TABLE OF spfli
        WITH UNIQUE KEY carrid connid
        WITH NON-UNIQUE SORTED KEY k_sort_spfli COMPONENTS carrid.
  TYPES:tt_sflight TYPE HASHED TABLE OF sflight
        WITH UNIQUE KEY carrid connid fldate
        WITH NON-UNIQUE SORTED KEY k_sort_sflight COMPONENTS carrid connid.
  TYPES:tt_sairport TYPE HASHED TABLE OF sairport
        WITH UNIQUE KEY id.

  "定义mesh
  TYPES:BEGIN OF MESH m_flights_info,
    scarrs TYPE tt_scarr
      ASSOCIATION _spfli TO spflis
      ON carrid = carrid USING KEY k_sort_spfli,
    spflis TYPE tt_spfli
      "和sflight直接association
      ASSOCIATION _sflight TO sflights
      ON carrid = carrid
      AND connid = connid USING KEY k_sort_sflight
      "和airport之间association
      ASSOCIATION _sairport TO sairports
                   ON id = airpto,
                   "USING KEY primary_key,
    sflights TYPE tt_sflight,
    sairports TYPE tt_sairport,
  END OF MESH m_flights_info.
  "指针方式
  DATA:lm_custom_books3_ref TYPE REF TO DATA.
  FIELD-SYMBOLS:<fs_custom_books> TYPE m_custom_books.


  "创建mesh对象
  "方式1
  DATA:lm_custom_books TYPE m_custom_books.
  DATA:lm_flights_info TYPE m_flights_info.
  ASSIGN lm_custom_books TO <fs_custom_books>.
  "方式2
  DATA(lm_custom_books2) = NEW m_custom_books( ).
  DATA(lm_custom_books2_ref) = NEW m_custom_books( VALUE m_custom_books( ) ).
  lm_custom_books3_ref = REF #( lm_custom_books2 ).


  "填充mesh数据,这样数据过多同样会有内存限制?
  SELECT * FROM scustom INTO TABLE lm_custom_books-customers.
  SELECT * FROM sbook INTO TABLE @lm_custom_books-books.
  SELECT * FROM scarr INTO TABLE @lm_flights_info-scarrs.
  SELECT * FROM spfli INTO TABLE @lm_flights_info-spflis.
  SELECT * FROM sflight INTO TABLE @lm_flights_info-sflights.
  SELECT * FROM sairport INTO TABLE @lm_flights_info-sairports.

  "正反向数据访问
  "正向Forward associations,root node作为开始node;
  "反向Inverse associations,root node作为结束node;
  "正向数据访问
  "cutomers_sbook
  "lt_result1数据类型和最后一个association对应表一致
  "根据customer表查询book表信息
  "lm_custom_books-customers_sbook
  IF line_exists( lm_custom_books-customers_sbook[
                  lm_custom_books-customers[ id = '00001134' ] ] ).
    DATA(lt_result1) = VALUE tt_books( ( lm_custom_books-customers_sbook[
                      lm_custom_books-customers[ id = '00001134' ]
    ] ) ).
    "输出显示
    LOOP AT lt_result1 ASSIGNING FIELD-SYMBOL(<fs_result1>).
      WRITE:/ 'result1:',<fs_result1>-passname,<fs_result1>-order_date.
    ENDLOOP.
  ENDIF.

  "反向数据访问
  "lt_result2数据类型和~后的表类型一致
  "根据book表信息查询customer信息
  "lm_custom_books-books^_sbook~customers
  IF line_exists( lm_custom_books-books^_sbook~customers[
                        lm_custom_books-books[ customid = '00001134' ] ] ).
    DATA(lt_result2) = VALUE tt_customers( ( lm_custom_books-books^_sbook~customers[
                        lm_custom_books-books[ customid = '00001134' ]
     ] ) ).
    "输出显示
    LOOP AT lt_result2 ASSIGNING FIELD-SYMBOL(<fs_result2>).
      WRITE:/ 'result2:',<fs_result2>-name,<fs_result2>-city.
    ENDLOOP.
  ENDIF.


  "根据customer表信息查询book信息
  "lm_custom_books-customers^_scutomer~books
  IF line_exists( lm_custom_books-customers^_scutomer~books[
                        lm_custom_books-customers[ id = '00001134' ] ] ).
    "VALUE中使用()表示返回table类型,
    "注意:如果匹配多笔,也只返回单笔记录!
    DATA(lt_result3) = VALUE tt_books( ( lm_custom_books-customers^_scutomer~books[
                        lm_custom_books-customers[ id = '00001134' ] ] ) ).

    "VALUE中不使用(),表示返回工作区,单笔记录
    DATA(lt_result4) = VALUE wa_books( lm_custom_books-customers^_scutomer~books[
                        lm_custom_books-customers[ id = '00001134' ] ] ).

  ENDIF.
  LOOP AT lt_result3 ASSIGNING FIELD-SYMBOL(<fs_result3>).
    WRITE:/ 'result3:',<fs_result3>-passname,<fs_result3>-order_date.
  ENDLOOP.
  WRITE:/ 'result4:',lt_result4-passname,lt_result4-order_date.


  "多层级正向association
  "lm_flights_info-scarrs_spfli[]_sflight[ ]
  IF line_exists( lm_flights_info-scarrs_spfli[
                  lm_flights_info-scarrs[ carrid = 'AA' ] ]_sflight[ ] ).
    "有多笔sflight记录,只返回单笔
    DATA(lt_result5) = VALUE tt_sflight( ( lm_flights_info-scarrs_spfli[
                  lm_flights_info-scarrs[ carrid = 'AA' ] ]_sflight[ ] ) ).

    "返回多笔记录
    DATA(lt_result6) = VALUE tt_sflight( FOR <fs_result6> IN lm_flights_info-scarrs_spfli[
                  lm_flights_info-scarrs[ carrid = 'AA' ] ]_sflight[ ]
                  ( <fs_result6> )
                  ).
  ENDIF.
  LOOP AT lt_result5 ASSIGNING FIELD-SYMBOL(<fs_result5>).
    WRITE:/ 'result5:',<fs_result5>-carrid,<fs_result5>-connid,<fs_result5>-fldate.
  ENDLOOP.
  "因为上面<fs_result6>已经被定义使用,
  "这里不能使用field symbol(<fs_result6>)
  "也不能直接使用fs_result6?
  "LOOP AT lt_result6 ASSIGNING FIELD-SYMBOL(<fs_result6>).
  "LOOP AT lt_result6 ASSIGNING <fs_result6>.
  LOOP AT lt_result6 ASSIGNING FIELD-SYMBOL(<fs_result61>).
    WRITE:/ 'result6:',<fs_result61>-carrid,<fs_result61>-connid,<fs_result61>-fldate.
  ENDLOOP.

  "多层级反向association
  "lm_flights_info-sflights^_sflight~spflis[]^_spfli~scarrs[]
  IF line_exists( lm_flights_info-sflights^_sflight~spflis[
                  lm_flights_info-sflights[ carrid = 'AA' ] ]^_spfli~scarrs[ ] ).
    DATA(lt_result7) = VALUE tt_scarr( FOR <fs_result7> IN lm_flights_info-sflights^_sflight~spflis[
                  lm_flights_info-sflights[ carrid = 'AA' ] ]^_spfli~scarrs[ ]
                  ( <fs_result7> ) ).

    "使用field symbol保存结果
    "注意:这里返回是工作区类型
    ASSIGN lm_flights_info-sflights^_sflight~spflis[
           lm_flights_info-sflights[ carrid = 'AA' ] ]^_spfli~scarrs[ ]
    TO FIELD-SYMBOL(<fs_result72>).
  ENDIF.
  LOOP AT lt_result7 ASSIGNING FIELD-SYMBOL(<fs_result71>).
    WRITE:/ 'result7:',<fs_result71>-carrid,<fs_result71>-carrname,<fs_result71>-url.
  ENDLOOP.
  WRITE:/ 'result72:',<fs_result72>-carrid,<fs_result72>-carrname,<fs_result72>-url.


  "Loop AT 语句
  "LOOP AT mesh_path result.
  "...
  "ENDLOOP.
  LOOP AT lm_flights_info-spflis_sflight[
          lm_flights_info-spflis[ carrid = 'AA' connid = '0064' ]
          WHERE fldate = '20220423'
          "AND currency = 'USD'
          ]
    INTO DATA(ls_sflight).
    WRITE:/ 'Loop at:',ls_sflight-connid,ls_sflight-fldate,ls_sflight-price.
  ENDLOOP.
  "注意:
  "lm_flights_info-spflis[ carrid = 'AA' ]时
  "table:spfli有两笔匹配记录,connid = '0017' or '0064'
  "但只会返回第一笔数据carrid = 'AA' connid = '0017'
  "然后额外条件WHERE connid = '0064'时不会返回任何记录;
  LOOP AT lm_flights_info-spflis_sflight[
          lm_flights_info-spflis[ carrid = 'AA' connid = '0064' ]
          "lm_flights_info-spflis[ carrid = 'AA' ]
          "WHERE  connid = '0064'
          ]
    INTO DATA(ls_sflight1).
    WRITE:/ 'Loop at1:',ls_sflight1-connid,ls_sflight1-fldate,ls_sflight1-price.
  ENDLOOP.
  "lm_flights_info-scarrs_spfli[]_sairport[]
  LOOP AT lm_flights_info-scarrs_spfli[
          lm_flights_info-scarrs[ carrname = 'Alitalia' ]
          WHERE countryto = 'JP'
          ]_sairport[ USING KEY primary_key ]
       INTO DATA(ls_sairport).
        "INTO DATA(ls_spfli).
    WRITE:/ 'Loop at2:',ls_sairport-id,ls_sairport-name.
    "WRITE:/ 'Loop at2:',ls_spfli-carrid,ls_spfli-connid.
  ENDLOOP.


  "FOR IN语句
  "FOR wa|<fs> IN mesh_path [let_exp]
  DATA:lt_sflights TYPE tt_sflight.
  lt_sflights = VALUE tt_sflight(
    FOR ls_sflights IN lm_flights_info-scarrs_spfli[
        lm_flights_info-scarrs[ carrname = 'American Airlines' ] ]_sflight[ ]
    ( ls_sflights )
  ).
  "输出结果
  LOOP AT lt_sflights INTO DATA(ls_sflights2).
    WRITE:/ 'FOR IN:',ls_sflights2-carrid,ls_sflights2-connid,ls_sflights2-fldate.
  ENDLOOP.
  "结合REDUCE汇总
  DATA(lv_sum) = REDUCE sflight-paymentsum(
    INIT v_sum TYPE sflight-paymentsum
    FOR ls_sflights3 IN lm_flights_info-scarrs_spfli[
        lm_flights_info-scarrs[ carrname = 'American Airlines' ] ]_sflight[ ]
    NEXT v_sum = v_sum   ls_sflights3-paymentsum
  ).
  WRITE:/ 'FOR IN2:',lv_sum.



  "定义mesh
  TYPES:BEGIN OF s_line1,
        id TYPE i,
        desc TYPE string,
        END OF s_line1.
  TYPES:BEGIN OF s_line2,
        id TYPE i,
        name TYPE string,
        desc TYPE string,
        END OF s_line2.
  TYPES:tt_line1 TYPE SORTED TABLE OF s_line1
        WITH NON-UNIQUE KEY id.
  TYPES:tt_line2 TYPE SORTED TABLE OF s_line2
        WITH NON-UNIQUE KEY id
        WITH NON-UNIQUE SORTED KEY k_1 COMPONENTS id name.
  TYPES:BEGIN OF MESH m_line,
        line1 TYPE tt_line1
        ASSOCIATION _line2 TO line2 ON id = id,
        line2 TYPE tt_line2,
        END OF MESH m_line.
  "INSERT语句
  "INSERT line_spec INTO TABLE mesh_path result.
  "插入时,如果UNIQUE Key不能插入重复值
  DATA:lm_line TYPE m_line.
  lm_line-line1 = VALUE #(
    ( id = 1 desc = 'line11' )
    ( id = 2 desc = 'line12' )
    ( id = 3 desc = 'line13' )
  ).
  lm_line-line2 = VALUE #(
    ( id = 1 desc = 'line21' )
    ( id = 2 desc = 'line22' )
    ( id = 2 desc = 'line222' )
  ).
  INSERT VALUE s_line1( desc = 'line14' ) INTO TABLE lm_line-line1.
  INSERT LINES OF VALUE tt_line1(
    ( id = 5 desc = 'line15' )
    ( desc = 'line16' )
   ) INTO TABLE lm_line-line1.
  INSERT INITIAL LINE INTO TABLE lm_line-line1.
  DATA:ls_line1 LIKE LINE OF lm_line-line1.
  LOOP AT lm_line-line1 INTO ls_line1.
    WRITE:/ 'Insert:',ls_line1-id,ls_line1-desc.
  ENDLOOP.

  INSERT VALUE s_line2( name = 'name1' desc = 'line24' ) INTO TABLE lm_line-line1_line2[
  lm_line-line1[ 1 ] ].
  DATA:ls_line2 LIKE LINE OF lm_line-line2.
  LOOP AT lm_line-line2 INTO ls_line2.
    WRITE:/ 'Insert:',ls_line2-id,ls_line2-name,ls_line2-desc.
  ENDLOOP.

  "Modify语句
  "MODIFY { TABLE mesh_path [USING KEY keyname] FROM wa
  "     [TRANSPORTING comp1 comp2 ...] result }
  " | { mesh_path FROM wa [TRANSPORTING comp1 comp2 ...] }.
  "MODIFY TABLE只修改符合条件单笔
  MODIFY TABLE lm_line-line1_line2[
  lm_line-line1[ id = 2 ] ]
  FROM VALUE s_line2( name = 'modify1' desc = 'line66' ).
  MODIFY TABLE lm_line-line1_line2[
  lm_line-line1[ id = 0 ] ]
  FROM VALUE s_line2( name = 'modify2' )
  TRANSPORTING name.
  "输出显示
  LOOP AT lm_line-line2 INTO ls_line2.
    WRITE:/ 'Modify:',ls_line2-id,ls_line2-name,ls_line2-desc.
  ENDLOOP.
  "MODIFY 修改符合条件多笔
  MODIFY lm_line-line1_line2[
  lm_line-line1[ id = 2 ] ]
  FROM VALUE s_line2( name = 'modify multi' )
  TRANSPORTING name.
  "输出显示
  LOOP AT lm_line-line2 INTO ls_line2.
    WRITE:/ 'Modify:',ls_line2-id,ls_line2-name,ls_line2-desc.
  ENDLOOP.


  "Delete语句
  "DELETE { TABLE mesh_path table_key } | { mesh_path }.
*  "删除单笔记录
*  "1.删除line1中第4行id=1对应line2中id=1单行数据
*  DELETE TABLE lm_line-line1_line2[
*  lm_line-line1[ 4 ] ].
*  "输出显示
*  LOOP AT lm_line-line2 INTO ls_line2.
*    WRITE:/ 'DELETE:',ls_line2-id,ls_line2-name,ls_line2-desc.
*  ENDLOOP.
*  "2.使用TABLE KEY删除
*  DELETE TABLE lm_line-line1_line2[
*  lm_line-line1[ 5 ] ]
*  WITH TABLE KEY k_1 COMPONENTS name = 'modify multi'.
*  "输出显示
*  LOOP AT lm_line-line2 INTO ls_line2.
*    WRITE:/ 'DELETE1:',ls_line2-id,ls_line2-name,ls_line2-desc.
*  ENDLOOP.
*  "3.使用VALUE指定行域值删除
*  DELETE TABLE lm_line-line1_line2[
*  lm_line-line1[ 5 ] ]
*  FROM VALUE s_line2( name = 'modify multi' )
*  USING KEY k_1.

  "删除多笔记录
  "1.line1第5行id = 2,删除line2中所有id=2记录
*  DELETE lm_line-line1_line2[
*  lm_line-line1[ 5 ] ].
  "2.where条件删除
  DELETE lm_line-line1_line2[
  lm_line-line1[ 5 ]
  WHERE desc = 'line22' ].
  "通过sy-subrc判断是否删除成功
  IF sy-subrc = 0.
    WRITE:/ 'delete success'.
  ELSE.
    WRITE:/ 'delete failed'.
  ENDIF.



  "定义mesh
  TYPES:BEGIN OF s_1,
        col1 TYPE i,
        col2 TYPE i,
        END OF s_1.
  TYPES:BEGIN OF s_2,
        col1 TYPE i,
        col2 TYPE i,
        col3 TYPE i,
        col4 TYPE i,
        END OF s_2.
  TYPES:BEGIN OF s_3,
        col3 TYPE i,
        col4 TYPE i,
        END OF s_3.
  TYPES:tt_1 TYPE SORTED TABLE OF s_1
        WITH NON-UNIQUE KEY col1 col2.
  TYPES:tt_2 TYPE SORTED TABLE OF s_2
        WITH NON-UNIQUE KEY col1 col2.
  TYPES:tt_3 TYPE SORTED TABLE OF s_3
        WITH NON-UNIQUE KEY col3.
  TYPES:BEGIN OF MESH m_1,
        t1 TYPE tt_1
        ASSOCIATION _t2 TO t2 ON col1 = col1 AND col2 = col2,
        t2 TYPE tt_2
        ASSOCIATION _t3 TO t3 ON col3 = col3 AND col4 = col4,
        t3 TYPE tt_3,
        END OF MESH m_1.
  "SET ASSOCIATION语句
  "SET ASSOCIATION mesh_path { = wa    } | { LIKE wa } | { INITIAL }.
  "只会将association对应域值赋值给前置表对应字段
  "如果col4没有在association条件,则值不会写入
  DATA:lm_1 TYPE m_1.
  lm_1-t1 = VALUE #(
      ( col1 = 1 col2 = 11 )
      ( col1 = 2 col2 = 12 ) ).
  lm_1-t2 = VALUE #(
      ( col1 = 1 col2 = 11 )
      ( col1 = 2 col2 = 222 ) ).
  lm_1-t3 = VALUE #(
      ( col3 = 31 col4 = 331 )
      ( col3 = 32 col4 = 332 ) ).
  "set association
  "方式1:根据assiciation:_t3设置t2的col3,col4
  "这句设置不成功?
  "只能初始化设置值然后需要带出association值时使用?
  "DATA(ls_2) = VALUE s_2( col1 = 1 col2 = 11 ).
  "SET ASSOCIATION lm_1-t2_t3[ ls_2 ] = lm_1-t3[ 2 ].
  INSERT INITIAL LINE INTO TABLE
  lm_1-t1_t2[ VALUE s_1( col1 = 1 col2 = 12 ) ]
  ASSIGNING FIELD-SYMBOL(<line2>).
  SET ASSOCIATION lm_1-t2_t3[ <line2> ] = lm_1-t3[ 1 ].
  "方式2:从节点1开始,实现相同效果
  "根据assiciation:_t3设置t2的col3,col4
  SET ASSOCIATION lm_1-t1_t2[ VALUE s_1( col1 = 1 col2 = 11 ) ]_t3[ ] = lm_1-t3[ 2 ].

  "SET ASSOCIATION mesh_path LIKE wa
  INSERT INITIAL LINE INTO TABLE
  lm_1-t1_t2[ VALUE s_1( col1 = 1 col2 = 13 ) ]
  ASSIGNING FIELD-SYMBOL(<line22>).
  SET ASSOCIATION lm_1-t2_t3[ <line22> ] LIKE VALUE s_2( col3 = 333 col4 = 3334 ).

ENDFORM.

0 人点赞