ABAP常用小技巧-SQL语句
1.9 LOOP循环内尽量减少对数据库的访问操作(第一种情况会多次去TVKBT表中取相同的记录)
尽量避免
LOOP AT TD_VBAK INTO TH_VBAK.
CLEAR : TH_OUT.
SELECT SINGLE BEZEI
INTO TH_OUT-BEZEI
FROM TVKBT
WHERE SPRAS = SY-SPRAS
AND VKBUR = TH_VBAK-VKBUR.
TH_OUT-VBELN = TH_VBAK-VBELN.
APPEND TH_OUT TO TD_OUT.
ENDLOOP.
推荐用法
LOOP AT TD_VBAK INTO TH_VBAK.
CLEAR : TH_OUT.
CLEAR : TH_TVKBT.
READ TABLE TD_TVKBT INTO TH_TVKBT
WITH KEY VKBUR = TH_VBAK-VKBUR.
IF SY-SUBRC <> 0.
SELECT SINGLE BEZEI
INTO TH_TVKBT-BEZEI
FROM TVKBT
WHERE SPRAS = SY-SPRAS
AND VKBUR = TH_VBAK-VKBUR.
TH_TVKBT-VKBUR = TH_VBAK-VKBUR.
APPEND TH_TVKBT TO TD_TVKBT.
ELSE.
TH_OUT-BEZEI = TH_TVKBT-BEZEI.
ENDIF.
TH_OUT-VBELN = TH_VBAK-VBELN.
APPEND TH_OUT TO TD_OUT.
ENDLOOP.