ABAP常用小技巧-SQL语句
1.7 联合查询表时,可以使用 INNER/LEFT/RIGHT JOIN ON后面的条件最好是两个的主键,否则可能会多查出N多数据
多于3表不建议使用INNER JOIN
尽量避免
SELECT VBELN
INTO TABLE TD_VBAK
FROM VBAK
WHERE VBELN IN S_VBELN.
LOOP AT TD_VBAK INTO TH_VBAK.
SELECT VBELN POSNR
INTO TABLE TD_VBAP
FROM VBAP
WHERE VBELN = TH_VBAK-VBELN.
LOOP AT TD_VBAP INTO TH_VBAP.
TH_OUT-VBELN = TH_VBAP-VBELN.
TH_OUT-POSNR = TH_VBAP-POSNR.
APPEND TH_OUT TO TD_OUT.
ENDLOOP.
ENDLOOP.
推荐用法
SELECT VBAP~VBELN VBAP~POSNR
INTO TABLE TD_OUT
FROM VBAK AS VBAK
INNER JOIN
VBAP AS VBAP
ON VBAK~VBELN = VBAP~VBELN
WHERE VBELN IN S_VBELN.