ABAP常用小技巧-SQL语句
1.3 只查询一条数据时,区分使用SELECT SINGLE 和UP TO 1 ROWS
1.3.1 查询条件为表的主键且只查一条记录,建议使用SELECT SINGLE
尽量避免
SELECT MATNR
INTO W_MATNR
FROM MARA
WHERE MATNR = P_MATNR.
ENDSELECT.
推荐用法
SELECT SINGLE MATNR
INTO W_MATNR
FROM MARA
WHERE MATNR = P_MATNR.
1.3.2 查询条件不是表的主键可能会查出多条记录,只取一条数据,建议使用UP TO 1 ROWS
尽量避免
SELECT MATNR
INTO W_MATNR
FROM MARA
WHERE MTART = ‘HAWA’.
EXIT.
ENDSELECT.
推荐用法
SELECT MATNR
INTO W_MATNR
FROM MARA
UP TO 1 ROWS
WHERE MTART = ‘HAWA’.
EXIT.
ENDSELECT.
1.4 查询多条数据并进行操作,尽量避免SELECT/ENDSELECT,建议使用INTO到内表
尽量避免
SELECT MATNR MTART
INTO TH_MARA
FROM MARA
WHERE MATNR IN S_MATNR.
CLEAR TH_OUT.
TH_OUT-MATNR = TH_MARA-MATNR.
APPEND TH_OUT TO TD_OUT.
ENDSELECT.
推荐用法
SELECT MATNR MTART
INTO TABLE TD_MARA
FROM MARA
WHERE MATNR IN S_MATNR.
LOOP AT TD_MARA INTO TH_MARA.
CLEAR TH_OUT.
TH_OUT-MATNR = TH_MARA-MATNR.
APPEND TH_OUT TO TD_OUT.
ENDLOOP.