IF line_exists( gt_data[ field1 = ' ' field2 = ' ' ] ). ENDIF.
使用此语法,可判断内表中行是否有满足条件的,无需LOOP判断,省去好多代码。
3. Select 内表
select 内表,可以i对内表使用sql的语法。例如:取内表某一列的最大值,就可以直接MAX,无需排序后,读第一行,
需要append a表 to B表时,但A表字段与B表字段数量不一致时,可以使用
SELECT FROM @a表 AS a
FIELDS field1
APPENDING CORRESPONDING FIELDS OF TABLE @b表.
lv_field = |{ lv_field ALPHA = OUT }|.//去前导0
lv_field = |{ lv_field ALPHA = IN }|.//加前导0
lr_data[] = VALUE #( sign = 'I' option = 'BT' ( low = 1 high = 2 )
( low = 3 high = 4 )
( low = 4 high = 5 )
option = 'EQ' ( low = 6 ) ).
对range表赋值,非常简便。
lt_data[] = value #( for wa in gt_data
where ( field1 = '01' and field2 = '02 ) ( wa ) ).
赋值指定条件的数据
6.Group 分组循环
loop at lt_data into data(ls_data) group by ( field = ls_data-field )
ascending assigning field-symbol(<group>).
loop at group <group> assigning field-symbol(<fs>).
end loop.
endloop.
动态条件分组
DATA: lv_field TYPE c VALUE 'B'.
LOOP AT ASSIGNING GROUP BY SWITCH string( lv_field WHEN 'A'
THEN | BELNR = -BELNR|
WHEN 'B'
THEN | BELNR = -BELNR WAERS = -WAERS | ) ASSIGNING FIELD-SYMBOL().
LOOP AT GROUP ASSIGNING FIELD-SYMBOL().
ENDLOOP.
ENDLOOP.
分组处理,不像at end of 那样需要调整字段顺序。
7. Replace
lv_string1 = 'statements'.
lv_string2 = 'asffasfa'..
write: replace( val = lv_string1
sub = 'sta'
case = abap_true
with = to_upper( lv_string2 ) "如果发现sta,用lv_char2的大写替换
occ = 2 )."如果occ为正,则从左边开始计数;如果occ为负,则从右数。值1、2、....表示第一,第二,事件。取值为-1、-2、....表示最后一个,倒数第二个,。
WRITE: |Converte Date: { CONV d( sy-datum + 7 ) DATE = USER } |."CONV conversion data type
转换为当前用户的日期格式
9. Case
TRY.
CATCH cx_root INTO DATA(exc).
CASE TYPE OF exc.
WHEN TYPE cx_sy_arithmetic_error.
out->display( 'Arithmetic error' ).
WHEN TYPE cx_sy_conversion_error.
out->display( 'Conversion error' ).
WHEN OTHERS.
out->display( 'Other error' ).
ENDCASE.
ENDTRY.
抓异常,分异常类型报错
10. Conditional Operator
根据不同条件赋值,一般是写Case或If,下面这些可以在定义时赋值,并且在else中抓取异常
COND:
DATA(time) = COND string(
WHEN sy-timlo < '120000' THEN |{ sy-timlo TIME = ISO } AM|
WHEN sy-timlo > '120000' THEN |{ CONV t( sy-timlo - 12 * 3600 ) TIME = ISO } PM|
WHEN sy-timlo = '120000' THEN |High Noon|
ELSE THROW cx_cant_be( ) ).
Switch:
DATA(number) = SWITCH string( sy-index
WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
WHEN 3 THEN 'three'
ELSE THROW cx_overflow( ) ).