ABAP常用小技巧-SQL语句
1.8 FOR ALL ENTRIES IN使用,保证内表非空,查询时需要查出表的主键,否则查询出来的数据可能会重复
如果指定的是内表中的重复键,则是不允许的,此处不确定,应该是没有影响,重复记录会自动删除.
尽量避免
SELECT UMLMC
INTO TABLE TD_MARC
FROM MARC
FOR ALL ENTRIES IN TD_MARA
WHERE MATNR = TD_MARA-MATNR.
推荐用法
SELECT MATNR WERKS UMLMC
INTO TABLE TD_MARC
FROM MARC
FOR ALL ENTRIES IN TD_MARA
WHERE MATNR = TD_MARA-MATNR.
1.8.1 主数据取得后,再取关联数据时,可以使用SELECT SINGLE,UP TO 1 ROWS等
尽量避免
SELECT MATNR
SPRAS
MAKTX
INTO TABLE TD_MAKT
FROM MAKT
FOR ALL ENTRIES IN LTD_VBAP
WHERE MATNR = LTD_VBAP-MATNR.
SORT TD_MAKT BY MATNR
SPRAS.
LOOP AT TD_VBAP INTO TH_VBAP.
READ TABLE TD_MAKT INTO TH_MAKT
WITH KEY MATNR = TH_VBAP-MATNR
SPRAS = SY-LANGU
BINARY SEARCH.
ENDLOOP.
推荐用法
LOOP AT TD_VBAP INTO TH_VBAP.
SELECT SINGLE MAKTX
INTO LW_MAKTX
FROM MAKT
WHERE MATNR = TH_VBAP-MATNR
AND SPRAS = SY-LANGU.
ENDLOOP