1. VALUE type( ... ),DATA(WA)
临时定义类型并且赋值给构造
TYPES:BEGIN OF TY_TAB,ZCHAR1 TYPE C LENGTH 10,ZCHAR2 TYPE C LENGTH 10,ZCHAR3 TYPE C LENGTH 10,ZCHAR4 TYPE C LENGTH 10,END OF TY_TAB.DATA(WA) = VALUE TY_TAB( ZCHAR1 = '1' ZCHAR2 = '2' ZCHAR3 = '3' ZCHAR4 = '4' ).CL_DEMO_OUTPUT=>DISPLAY( WA ).
表赋值
DATA:LT_TAB TYPE TT_TAB.LT_TAB = VALUE #( ( ZCHAR1 = '1' ZCHAR2 = '1' ZCHAR3 = '3' ZCHAR4 = '4' )( ZCHAR1 = '1' ZCHAR2 = '1' ZCHAR3 = '3' ZCHAR4 = '4' )( ZCHAR1 = '1' ZCHAR2 = '1' ZCHAR3 = '3' ZCHAR4 = '4' )( ZCHAR1 = '1' ZCHAR2 = '1' ZCHAR3 = '3' ZCHAR4 = '4' )ZCHAR1 = '1' ( ZCHAR2 = '2' ZCHAR3 = '6' ZCHAR4 = '6' )( ZCHAR2 = '2' ZCHAR3 = '7' ZCHAR4 = '7' )ZCHAR1 = '1' ZCHAR2 = '3' ( ZCHAR3 = '6' ZCHAR4 = '6' )( ZCHAR3 = '7' ZCHAR4 = '7' )ZCHAR1 = '1' ZCHAR2 = '4' ( ZCHAR3 = '6' ZCHAR4 = '6' )( ZCHAR3 = '7' ZCHAR4 = '7' )).CL_DEMO_OUTPUT=>DISPLAY( LT_TAB ).
测试发现 括号的变量可以变少,但是不能变少之后再变多
内表追加数据
内表操作指定行赋值(不会改变系统变量SY-TABIX的值)
DATA(WA1) = LT_TAB[ 5 ].CL_DEMO_OUTPUT=>DISPLAY( WA1 ).
字符拼接
指针:
ASSIGN COMPONENT L_I OF STRUCTURE WA1 TO FIELD-SYMBOL(<FS_TS>).ASSIGN LT_TAB[ 5 ] TO FIELD-SYMBOL(<FS>).READ TABLE LT_TAB ASSIGNING FIELD-SYMBOL(<FS_READ>) WITH KEY ZCHAR1 = '3'.
内表数据读取(读取不到会抛异常,所以以下两种方法执行避免一种DUMP)
TRY .DATA(WA3) = LT_TAB1[ ZCHAR1 = '1' ].CATCH CX_SY_ITAB_LINE_NOT_FOUND INTO DATA(oref) .DATA(STR11) = oref->GET_TEXT( ).WRITE:/ STR11.ENDTRY.IF line_exists( LT_TAB1[ ZCHAR1 = '1' ] ).DATA(WA2) = LT_TAB1[ ZCHAR1 = '1' ].ENDIF.
1.MOVE-CORRESPONDING
表或者字段之间相同字段赋值
* MOVE-CORRESPONDING LT_TAB TO LT_TAB1.LT_TAB1 = CORRESPONDING #( LT_TAB ).CL_DEMO_OUTPUT=>DISPLAY( LT_TAB1 ).
2.FOR 写法及相关应用
TYPES:BEGIN OF LINE,COL1 TYPE I,COL2 TYPE I,COL3 TYPE I,END OF LINE,ITAB TYPE STANDARD TABLE OF LINE WITH EMPTY KEY.DATA(ITAB) = VALUE ITAB(FOR J = 11 THEN J * 10 UNTIL J > 4000( COL1 = J COL2 = J + 1 COL3 = J + 2 )).* 返回一个值DATA(TOTAL) = REDUCE I( INIT RES = 0 FOR WA IN ITAB WHERE ( COL1 < 1000 ) NEXT RES = RES + WA-COL1 ).* 返回一个结构DATA(RESULT) = REDUCE LINE( INIT RES1 = VALUE LINE( COL2 = 0 COL3 = 9999999 )FOR WA IN ITAB WHERE ( COL1 IS NOT INITIAL )NEXT RES1-COL1 = RES1-COL1 + 1RES1-COL2 = NMAX( VAL1 = RES1-COL1 VAL2 = WA-COL2 )RES1-COL3 = NMIN( VAL1 = RES1-COL1 VAL2 = WA-COL3 )).CL_DEMO_OUTPUT=>DISPLAY( ITAB ).CL_DEMO_OUTPUT=>DISPLAY( TOTAL ).CL_DEMO_OUTPUT=>DISPLAY( RESULT ).
内表中满足条件的数据放到另一内表
DATA(ITAB2) = VALUE ITAB( FOR WA IN ITAB WHERE ( COL1 < 1000 )( COL1 = WA-COL1 COL2 = WA-COL2 COL3 = WA-COL3 )).CL_DEMO_OUTPUT=>DISPLAY( ITAB2 ).
LOOP GROUP BY
TYPES:BEGIN OF TY_LINE,C1 TYPE C LENGTH 20,C2 TYPE C LENGTH 20,C3 TYPE C LENGTH 20,C4 TYPE I ,END OF TY_LINE,ITAB TYPE STANDARD TABLE OF TY_LINE WITH EMPTY KEY.DATA(ITAB) = VALUE ITAB(C1 = 'A' ( C2 = 'A' C3 = 'A' C4 = 1 )( C2 = 'B' C3 = 'B' C4 = 2 )( C2 = 'C' C3 = 'C' C4 = 3 )C1 = 'B' ( C2 = 'A' C3 = 'A' C4 = 1 )( C2 = 'B' C3 = 'B' C4 = 2 )( C2 = 'C' C3 = 'C' C4 = 3 )C1 = 'C' ( C2 = 'A' C3 = 'A' C4 = 1 )( C2 = 'B' C3 = 'B' C4 = 2 )( C2 = 'C' C3 = 'C' C4 = 3 )) .DATA(ITAB3) = VALUE ITAB( ).LOOP AT ITAB INTO DATA(WA) GROUP BY WA-C1.DATA(ITAB1) = VALUE ITAB( ).LOOP AT GROUP WA INTO DATA(WA1). "遍历分组后得结果机ITAB1 = VALUE ITAB( BASE ITAB1 ( WA1 ) ). "将结果集追加进表 BASE得用法ENDLOOP.* 对分组后得数据进行累加DATA(SUM) = REDUCE I( INIT RES = 0 FOR WA2 IN ITAB1NEXT RES = RES + WA2-C4) .IF ITAB1 IS NOT INITIAL.DATA(LS_ITAB) = ITAB1[ 1 ].LS_ITAB-C4 = SUM.ITAB3 = VALUE ITAB( BASE ITAB3 ( LS_ITAB ) ).ENDIF.ENDLOOP.CL_DEMO_OUTPUT=>DISPLAY( ITAB3 ).
自定义分组
DATA(MEMBERS) = VALUE ITAB( ).LOOP AT ITAB ASSIGNING FIELD-SYMBOL(<FS_AB>)GROUP BY COND STRING(WHEN <FS_AB>-C1 = 'A' THEN |START|WHEN <FS_AB>-C1 = 'B' THEN |MIDDLE|ELSE |END|)INTO DATA(GROUP).IF GROUP = 'START'.MEMBERS = VALUE #( FOR G1 IN GROUP GROUP ( G1 ) ).ENDIF.ENDLOOP.CL_DEMO_OUTPUT=>DISPLAY( MEMBERS ).
FILTER过滤器的试用(需要排序表或者哈希表)
EXCEPT如果不指定则表示满足条件的找出来,如果指定则表示不满足条件的找出来
DATA(ITAB5) = FILTER ITAB1( ITBA4 IN MEMBERS1WHERE C1 = C1).CL_DEMO_OUTPUT=>DISPLAY( ITAB5 ).DATA(ITAB6) = FILTER ITAB1( ITBA4WHERE C1 = CONV #( 'C' )).CL_DEMO_OUTPUT=>DISPLAY( ITAB6 ).
S/4对象创建
DATA(OREF) = NEW CL_TOOLS_LOCAL( ).OREF->H_TOOLS_BT_DATES_001(EXPORTING U_BEGDA = '20181011' U_ENDDA = '20181101'IMPORTING E_DAY = DATA(L_DAY1)).