SAP ABAP 新语法补充(DATA、COND、SWITCH、VALUE、FOR、REDUCE、CONV、CORRESPONDING、GROUP BY、FELTER)
新语法 |
示例 |
等价于/说明 |
DATA |
(1)DATA(TEXT) = ‘ABCDE’. (2)LOOP AT ITAB INTO DATA(WA). …… ENDLOOP. |
(1)DATA TEXT TYPE STRING. TEXT = ‘ABCDE’. (2)DATA WA LIKE LINE OF ITAB. LOOP AT ITAB INTO WA. …… ENDLOOP. |
COND |
LV_STR = COND #(WHEN LV_STR IS INITIAL THEN ‘ABCDE’ ELSE LV_STR).
|
IF LV_STR IS INITIAL. LV_STR = ‘ABCDE’. ELSE. LV_STR = ‘ ‘. ENDIF.
|
SWITCH |
LV_STR = SWITCH #( N WHEN 1 THEN ‘ABCDE‘ WHEN 2 THEN ‘FGHTL). |
CASE N. WHEN 1. LV_STR = ‘ABCDE’. WHEN 2. LV_STR = ‘FGHTL’. WHEN OTHERS. LV_STR = ‘ ‘. ENDCASE. |
VALUE |
ITAB= VALUE #( ( ID = 1 NAME = ‘LILI’) (ID = 2 NAME = ‘LIMI)). |
TYPES:BEGIN OF TY_ITAB, ID TYPE I, NAME TYPE STRING, END OF TY_ITAB. DATA:ITAB TYPE TABLE OF TY_ITAB. 再赋值 |
FOR |
DATA(ITAB2) = VALUE ITAB2(FOR WA IN ITAB1 WHERE ( ID < 50 ) ( ID = WA-ID NAME = WA-NAME AGE = WA-AGE )). |
加强版的LOOP AT,可用于填充内表、根据条件汇总、计算等操作 |
REDUCE |
DATA( SUM ) = REDUCE I( INIT X = 0 FOR WA IN ITAB NEXT X = X+WA-AGE ). |
可用于汇总计算 |
CONV |
LV_STR = ‘001024.012’. LV_INT = CONV I( LV_STR ). |
数据类型转换,不需要中间变量 |
CORRESPONDING |
STRUCT2 = CORRESPONDING #( STRUCT1 ). |
用于实现结构和内表之间的动态赋值操作 |
GROUP BY |
LOOP AT ITAB ASSIGNING FIELD-SYMBOL(<FS_ITAB>) GROUP BY <FS_ITAB>-ID. |
相当于SQL中的GROUP BY,用于内表处理 |
FELTER |
DATA( ITAB1 ) = FILTER #( ITAB USING KEY ID)单值过滤 DATA( ITAB2 ) = FILTER #( ITAB USING KEY ID WHERE AGE = CONV #( TO_UPPER( AGE )))多值过滤 |
过滤,可以指定一个或多个值进行过滤 |