lips和likp均是在数据字典里定义好结构的sap表,likp是抬头,lips是对应的行项目。我们要把符合条件(lips-vbeln=likp-vbeln)的数据选出,并输出lips表的vgbel vbeln posnr matnr arktx lfimg字段以及likp表的ernam erdat lfdat lifnr字段。其中vbeln是表likp的主键。
一.定义存储表lips,likp部分字段的内表。ekko表的数据下一个例子要用到。
DATA: BEGIN OF wa_lips_result,
bukrs TYPE ekko-bukrs,
ekorg TYPE ekko-ekorg,
vgbel TYPE lips-vgbel,
vbeln TYPE lips-vbeln,
posnr TYPE lips-posnr,
matnr TYPE lips-matnr,
arktx TYPE lips-arktx,
lfimg TYPE lips-lfimg,
ernam TYPE likp-ernam,
erdat TYPE likp-erdat,
lfdat TYPE likp-lfdat,
lifnr TYPE likp-lifnr,
END OF wa_lips_result,
it_lips_result LIKE TABLE OF wa_lips_result WITH HEADER LINE.
DATA: BEGIN OF wa_likp_result,
vbeln TYPE likp-vbeln,
ernam TYPE likp-ernam,
erdat TYPE likp-erdat,
lfdat TYPE likp-lfdat,
lifnr TYPE likp-lifnr,
END OF wa_likp_result,
it_likp_result LIKE TABLE OF wa_likp_result WITH HEADER LINE.
二.从屏幕条件中选出数据填入两内表,按关键字vbeln对两内表排序,然后按条件选出数据。
SELECT vgbel vbeln posnr matnr arktx lfimg
FROM lips
INTO CORRESPONDING FIELDS OF TABLE it_lips_result
WHERE vbeln IN s_lvbeln.
SELECT vbeln ernam erdat lfdat lifnr
FROM likp
INTO CORRESPONDING FIELDS OF TABLE it_likp_result
WHERE lfdat IN s_lfdat
AND erdat IN s_erdat.
SORT it_lips_result BY vbeln.
SORT it_likp_result BY vbeln.
LOOP AT it_lips_result.
LOOP AT it_likp_result.
IF it_likp_result-vbeln = it_lips_result-vbeln.
MOVE-CORRESPONDING it_likp_result TO it_lips_result.
MODIFY it_lips_result.
EXIT.
ELSE.
DELETE it_likp_result.
ENDIF.
ENDLOOP.
ENDLOOP.
这个查询看似嵌套查询,复杂度为O(m*n),但实际上只有O(m+n)。我让两个内表一直按排序的顺序从小到大向前走,也就是说每次循环至少让其中一个表走一个数据。