SAP ABAP 对工作区列遍历或按条件访问
需要对字段数量多的工作区或动态工作区进行数据处理时,列遍历可使代码更加的简洁高效。
示例代码:
点击查看代码
TYPES:BEGIN OF typ_kna1,
kunnr TYPE kna1-kunnr, "客户编号
name1 TYPE kna1-name1, "送达方名称
stras TYPE kna1-stras, "街道
telf1 TYPE kna1-telf1, "送达方电话
telfx TYPE kna1-telfx, "售达方传真
street(220) TYPE c, "加长街道
END OF typ_kna1.
DATA it_kna1 TYPE TABLE OF typ_kna1.
SELECT kunnr, "客户编号
name1, "送达方名称
stras, "街道
telf1, "送达方电话
telfx "售达方传真
FROM kna1
WHERE stras IS NOT INITIAL
ORDER BY kunnr
INTO TABLE @it_kna1
UP TO 10 ROWS.
SELECT
a~kunnr, "客户名
b~adrnr, "编号
c~street, "街道1
c~str_suppl1, "街道2
c~str_suppl2, "街道3
c~str_suppl3, "街道4
c~location "街道5
FROM adrc AS c
INNER JOIN kna1 AS b ON b~adrnr = c~addrnumber
INNER JOIN @it_kna1 AS a ON a~kunnr = b~kunnr
ORDER BY a~kunnr
INTO TABLE @DATA(lt_adrc).
在DO循环中,用ASSIGN COMPONENT OF STRUCTURE可以循环获取工作区中各字段的值,DO循环中无法使用SY-TABIX,但是可以使用SY-INDEX,SY-INDEX初始值为1,每次循环+1,通过EXIT结束循环.
点击查看代码
LOOP AT lt_adrc INTO DATA(ls_adrc).
READ TABLE it_kna1 ASSIGNING FIELD-SYMBOL(<it_kna1>) WITH KEY kunnr = ls_adrc-kunnr BINARY SEARCH.
IF sy-subrc EQ 0.
"sy-index可以在DO循环使用,初始值为1,每次循环+1
DO.
ASSIGN COMPONENT SY-INDEX + 2 OF STRUCTURE ls_adrc TO <fs>.
IF sy-subrc EQ 0.
<it_kna1>-street = <it_kna1>-street && <fs>.
ELSE.
EXIT.
ENDIF.
ENDDO.
WRITE : <it_kna1>-street.
ENDIF.
ENDLOOP.
不使用SY-INDEX,自行调整SUM和EXIT条件
点击查看代码
DATA sum TYPE i.
LOOP AT lt_adrc INTO DATA(ls_adrc).
READ TABLE it_kna1 ASSIGNING FIELD-SYMBOL(<it_kna1>) WITH KEY kunnr = ls_adrc-kunnr BINARY SEARCH.
IF sy-subrc EQ 0.
"不使用sy-index,自行调整SUM和EXIT条件
"sum代表的是第几列
sum = 3.
DO.
ASSIGN COMPONENT sum OF STRUCTURE ls_adrc TO <fs>.
IF sy-subrc EQ 0.
<it_kna1>-street = <it_kna1>-street && <fs>.
sum += 1.
ENDIF.
IF sum = 8.
EXIT.
ENDIF.
ENDDO.
CLEAR sum.
WRITE : <it_kna1>-street.
ENDIF.
ENDLOOP.
内表数据:
输出数据: