1.获取第N行数据,替代READ TABLE写法
SELECT * FROM bkpf INTO TABLE @DATA(lt_bkpf) UP TO 5 ROWS.
IF sy-subrc EQ 0.
DATA(lw_bkpf) = VALUE #( lt_bkpf[ 3 ] ).
cl_demo_output=>display( lw_bkpf ).
ENDIF.
2.*号用法,自动匹配内表字段
DATA:BEGIN OF lw_itab,
matnr TYPE matnr,
pstat TYPE pstat_d,
maktx TYPE maktx,
END OF lw_itab,
itab LIKE TABLE OF lw_itab.
SELECT
mara~*,
makt~maktx
INTO CORRESPONDING FIELDS OF TABLE @itab
FROM mara
LEFT JOIN makt ON makt~matnr = mara~matnr AND makt~spras = @sy-langu UP TO 5 ROWS.
cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lcl_alv) CHANGING t_table = itab ).
lcl_alv->display( ).
3.嵌套VALUE赋值
DATA:BEGIN OF struc,
a TYPE c,
BEGIN OF b,
b1 TYPE c,
b2 TYPE c,
END OF b,
c TYPE c,
END OF struc,
lt_out LIKE TABLE OF struc.
lt_out = VALUE #( ( a = 'a' b = VALUE #( b1 = 'b' b2 = 'b' ) c = 'c' ) ).
cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lcl_alv) CHANGING t_table = lt_out ).
lcl_alv->display( ).
4.参照结构VALUE赋值
TYPES: BEGIN OF ty_man,
name TYPE char10, " 姓名
sex TYPE char1, " 性别
age TYPE p DECIMALS 2, " 年龄
school TYPE char20, " 学校
END OF ty_man.
DATA: gt_man TYPE TABLE OF ty_man.
DATA gw_man TYPE ty_man.
DATA(gs_man) = VALUE ty_man( name = 'Tom' sex = 'B' ).
"cl_demo_output=>display( gs_man ).
"附加年龄信息
gs_man = VALUE #( name = 'Tom' sex = 'B' age = 18 ).
"cl_demo_output=>display( gs_man ).
"附加学校信息
gs_man = VALUE #( BASE gs_man school = 'A SCHOOL' ).
"cl_demo_output=>display( gs_man ).
"调整学校信息
gs_man = VALUE #( BASE gs_man school = 'B SCHOOL' ).
"cl_demo_output=>display( gs_man ).
"内表赋值
gt_man = VALUE #( ( name = 'Anna' sex = 'G' age = 17 ) ( name = 'Ann' sex = 'G' age = 16 ) ).
"cl_demo_output=>display( gt_man ).
"内表基础上附加额外数据
gt_man = VALUE #( BASE gt_man ( name = 'Xiaohong' sex = 'G' age = 20 school = 'C SCHOOL' )
( name = 'Xiaoming' sex = 'B' age = 21 school = 'D SCHOOL' ) ).
"cl_demo_output=>display( gt_man ).
"新工作区按旧工作区对应字段赋值
gw_man = CORRESPONDING #( gs_man ).
"cl_demo_output=>display( gw_man ).
5.內表预定义函数
line_exists( ) - 判断记录是否存在
line_index( ) - 获取符合记录的索引值
"通过索引值判断某一行记录是否存在,也可通过条件判断
IF line_exists( lt_vbak[ 4 ] ).
"获取第4行记录
DATA(lw_vbak) = lt_vbak[ 4 ].
"获取第4行记录中的销售订单号
DATA(l_vbeln) = lt_vbak[ 4 ]-vbeln."标签类型
" cl_demo_output=>display( lw_vbak ).
" WRITE l_vbeln.
ENDIF.
"获取符合条件的索引值,未找到LV_INDEX为0
DATA(lv_index) = line_index( lt_vbak[ vbeln = '0010058344' kunnr = '0000604630' ] ).
IF lv_index NE 0 AND line_exists( lt_vbak[ lv_index + 1 ] ) .
CLEAR lw_vbak.
"获取下一行记录
lw_vbak = lt_vbak[ lv_index + 1 ].
"cl_demo_output=>display( lw_vbak ).
ENDIF.
6."内表行数
DATA(lv_lines) = lines( lt_vbak ).
"WRITE: / , 'lv_lines = ' && lv_lines.
"内表中符合条件的数据有几条
DATA(lv_lines_s) = REDUCE i( INIT x = 0
FOR ls_vbak IN lt_vbak WHERE ( vbeln = '0010058344' )
NEXT x = x + 1 ).
7.判断是否存在where条件下的数据,count(*)或者single @abap_true
"主键作为条件用count,否则single @abap_ture
SELECT COUNT(*) FROM ekko WHERE ebeln = @ebeln.
"SELECT SINGLE @abap_true FROM ekko WHERE ebeln = @ebeln INTO @DATA(field).
8.select取数用法,表名~*表示取表所有字段
SELECT ekko~*,lfa1~*
FROM ekko
INNER JOIN lfa1 ON ekko~lifnr = lfa1~lifnr
INTO CORRESPONDING FIELDS OF TABLE @ekko
WHERE ebeln = @ebeln.
9.前导零处理
DATA(lv_matnr) = |{ matnr ALPHA = IN WIDTH = 18 }|.“加前导零
DATA(lv_matnr) = |{ matnr ALPHA = OUT }|.“去前导零
10.FOR循环用法
TYPES:BEGIN OF ty_out,
field1 TYPE c LENGTH 2,
field2 TYPE c LENGTH 2,
END OF ty_out.
DATA gt_out TYPE TABLE OF ty_out.
DATA lt_out TYPE TABLE OF ty_out.
gt_out = VALUE #( ( field1 = '01' field2 = '02' ) ( field1 = '03' field2 = '04' ) ).
"for循环相等于loop循环,后面括号中的(wa_out)代表工作区所有值都赋值
lt_out[] = VALUE #( FOR wa_out IN gt_out ( wa_out ) ).
"对内表数据增加where条件筛选
lt_out[] = VALUE #( FOR wa_out IN gt_out WHERE ( field1 = '01' OR field1 = '03' ) ( wa_out ) ).
"对内表部分字段进行赋值
lt_out[] = VALUE #( FOR wa_out IN gt_out
WHERE ( field1 = '01' OR field1 = '03' )
( field1 = wa_out-field2 ) ).
cl_demo_output=>display( lt_out ).
11.用分组循环代替AT END OF,可以不用处理字段顺序等问题
TYPES: BEGIN OF ty_employee,
name TYPE char30,
role TYPE char30,
age TYPE i,
sex TYPE char10,
END OF ty_employee,
ty_employee_t TYPE STANDARD TABLE OF ty_employee WITH KEY name.
"对内表进行赋值"
DATA(gt_employee) = VALUE ty_employee_t(
( name = 'Mandy' role = 'SD Consultant' age = 64 sex = 'MAN' )
( name = 'John' role = 'ABAP guru' age = 34 sex = 'MAN' )
( name = 'Alice' role = 'FI Consultant' age = 42 sex = 'WOMAN' )
( name = 'Barry' role = 'ABAP guru' age = 54 sex = 'WOMAN' )
( name = 'Mary' role = 'FI Consultant' age = 37 sex = 'WOMAN' )
( name = 'Arthur' role = 'ABAP guru' age = 34 sex = 'MAN' ) ).
DATA: gv_tot_age TYPE i,
gv_avg_age TYPE decfloat34.
"通过角色进行分组"
LOOP AT gt_employee INTO DATA(ls_employee) GROUP BY ( role = ls_employee-role
size = GROUP SIZE "分组组数"
index = GROUP INDEX ) "分组的组序号"
ASCENDING ASSIGNING FIELD-SYMBOL(<group>).
CLEAR: gv_tot_age.
WRITE: / | 组序号: { <group>-index } 角色: { <group>-role WIDTH = 15 }| & | 这个组有几条数据: { <group>-size } |.
"loop循环组,将组里的数据读出来"
LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_member>).
gv_tot_age = gv_tot_age + <ls_member>-age.
WRITE: /13 <ls_member>-name .
ENDLOOP.
gv_avg_age = gv_tot_age / <group>-size.
WRITE: / |组平均年龄: { gv_avg_age }|.
SKIP.
ENDLOOP.
12.REDUCE汇总
DATA:BEGIN OF lw_data,
a TYPE char1,
b TYPE i,
END OF lw_data,
lt_data LIKE TABLE OF lw_data.
lt_data = VALUE #( ( a = 'A' b = 10 )
( a = 'B' b = 30 )
( a = 'C' b = 20 )
( a = 'B' b = 50 ) ) .
DATA(lv_sum) = REDUCE #( INIT sum TYPE menge_d"定义求和的字段SUM类型QUAN,名字随便
FOR ls_wa IN lt_data "创建一个工作区ls_wa对应内表
WHERE ( a = 'A' OR a = 'B' ) "汇总如果有条件,可以添加汇总条件
NEXT sum = sum + ls_wa-b )."把值和工作区wa取的值相加
新语法之内表处理
于 2022-08-30 21:04:54 首次发布