本文主要介绍SAP ABAP中OPEN SQL的INTO语句
结构体插入(插入一条语句时)
SELECT...INTO [CORRESPONDING FIELDS OF] <wa> FROM <db> WHERE <condition>.
内表插入(插入多条语句时)
SELECT...INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE <itab> FROM <db> WHERE <condition>
1. 当不需要连续查询多条语句并且依次插入内表时使用"INTO",因为每次使用INTO语句将数据插入内表都会先清空内表,示例代码及效果如下
REPORT ZWYZTEST_FOR_MANGO. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. DATA:GS_SFLIGHT TYPE SFLIGHT. SELECT CARRID INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT FROM SFLIGHT WHERE CARRID = 'AC'. SELECT CARRID INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT FROM SFLIGHT WHERE CARRID = 'AF'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:GS_SFLIGHT-CARRID. ENDLOOP.
2.当需要连续查询多条语句并且依次插入内表时使用"APPENDING",因为每次使用APPENDING语句将数据插入内表不会清空内表,保留先前的插入数据,示例代码及效果如下
REPORT ZWYZTEST_FOR_MANGO. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. DATA:GS_SFLIGHT TYPE SFLIGHT. SELECT CARRID APPENDING CORRESPONDING FIELDS OF TABLE GT_SFLIGHT FROM SFLIGHT WHERE CARRID = 'AC'. SELECT CARRID APPENDING CORRESPONDING FIELDS OF TABLE GT_SFLIGHT FROM SFLIGHT WHERE CARRID = 'AF'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:GS_SFLIGHT-CARRID. ENDLOOP.
SFLIGHT数据库表(下方示例代码也采用该数据库表)
PACKAGE用法
使用PACKAGE可以限制每次追加到内表里的数据件数,假设当n为5时,则每次只读取5条数据添加到内表中,注意!使用PACKAGE,结尾一定要使用ENDSELECT语句结束!
SELECT...INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE <itab> PACKAGE SIZE <n> FROM <db> WHERE <condition>. ENDSELECT.
Single Field
SELECT...INTO(f1,f2...)...
示例代码及效果:
REPORT ZWYZTEST_FOR_MANGO. DATA:GV_CARRID TYPE SFLIGHT-CARRID. DATA:GV_CONNID TYPE SFLIGHT-CONNID. SELECT CARRID CONNID INTO (GV_CARRID,GV_CONNID) FROM SFLIGHT WHERE CONNID = 2415. ENDSELECT. WRITE:GV_CARRID,GV_CONNID.