本文主要介绍一下SAP ABAP中内表的增删查改语句中的追加内表数据的INSERT相关语句,包括使用关键字追加和使用索引追加以及不同类型内表使用INSERT追加数据的不同效果
利用关键字追加单条数据
INSERT wa INTO TABLE itab.
案例代码演示
DATA:GT_SFLIGHT TYPE STANDARD TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. SELECT * FROM SFLIGHT INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT. WRITE:'追加前内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP. ULINE. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200616'. GS_SFLIGHT-PRICE = 1700. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT. CLEAR GS_SFLIGHT. WRITE:'追加后内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示
利用关键字追加多条数据
INSERT LINES OF itab1 [FROM n1 TO n2] INTO TABLE itab2.
案例代码演示
DATA:GT_SFLIGHT TYPE STANDARD TABLE OF SFLIGHT, GT_SFLIGHT2 TYPE STANDARD TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. SELECT * FROM SFLIGHT INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT. WRITE:'追加前内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP. ULINE. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200616'. GS_SFLIGHT-PRICE = 1700. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 1900. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2000. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. INSERT LINES OF GT_SFLIGHT2 FROM 2 TO 3 INTO TABLE GT_SFLIGHT. WRITE:'追加后内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示
利用索引追加单条数据
INSERT wa INTO itab INDEX n.
案例代码演示
DATA:GT_SFLIGHT TYPE STANDARD TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. SELECT * FROM SFLIGHT INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT. WRITE:'追加前内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP. ULINE. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200616'. GS_SFLIGHT-PRICE = 1700. INSERT GS_SFLIGHT INTO GT_SFLIGHT INDEX 3. CLEAR GS_SFLIGHT. WRITE:'追加后内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示
利用索引追加多条数据
INSERT LINES OF itab1 [FROM n1 TO n2] INTO itab2 INDEX n.
案例代码演示
DATA:GT_SFLIGHT TYPE STANDARD TABLE OF SFLIGHT, GT_SFLIGHT2 TYPE STANDARD TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. SELECT * FROM SFLIGHT INTO CORRESPONDING FIELDS OF TABLE GT_SFLIGHT. WRITE:'追加前内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP. ULINE. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200616'. GS_SFLIGHT-PRICE = 1700. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 1900. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2000. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. INSERT LINES OF GT_SFLIGHT2 FROM 1 TO 2 INTO GT_SFLIGHT INDEX 3. WRITE:'追加后内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示
不同类型内表INSERT的不同效果
\ | 标准表 | 排序表 | 哈希表 |
INSERT方式比较 | 同时支持关键字和索引追加数据 | 同时支持关键字和索引追加数据 | 只支持关键字追加数据 |
使用关键字INSERT效果比较 | 1.默认追加数据到内表最后一行 2.与APPEND语句具有相同效果 |
1.按照内表排序的顺序追加数据 2.若关键字不唯一,重复的数据 会追加到相同数据的上一行中 |
按照表关键字的哈希索引顺序追加数据 |
使用索引INSERT效果比较 | 默认数据追加到内表相应的索引号位置上 | 1.若数据追加到相应索引号后 不影响排序表的排列顺序,则程序不会报错 2.若数据追加到相应索引号后 影响了排序表的排列顺序,则程序报错 |
不能使用索引进行INSERT |
案例代码演示(排序表和哈希表使用关键字INSERT)
依次解除下面案例代码中这两种内表的注释,比较排序表和哈希表各自使用关键字INSERT数据的效果,标准表的案例在这里则不做过多赘述
DATA:GT_SFLIGHT TYPE SORTED TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID, GS_SFLIGHT TYPE SFLIGHT. *DATA:GT_SFLIGHT TYPE HASHED TABLE OF SFLIGHT WITH UNIQUE KEY CARRID, * GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200616'. GS_SFLIGHT-PRICE = 1700. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT. CLEAR GS_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 1900. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT. CLEAR GS_SFLIGHT. GS_SFLIGHT-CARRID = 'AB'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2000. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT. CLEAR GS_SFLIGHT. WRITE:'使用关键字追加数据后内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示(排序表和哈希表使用关键字INSERT)
排序表效果
哈希表效果
案例代码演示(排序表使用索引INSERT)
依次解除下面案例代码中添加数据到不同索引号位置的注释,比较排序表使用关键字INSERT数据的效果,标准表的案例在这里则不做过多赘述
DATA:GT_SFLIGHT TYPE SORTED TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID, GT_SFLIGHT2 TYPE SORTED TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID, GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200616'. GS_SFLIGHT-PRICE = 1700. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 1900. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. GS_SFLIGHT-CARRID = 'AB'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2000. INSERT GS_SFLIGHT INTO TABLE GT_SFLIGHT2. CLEAR GS_SFLIGHT. INSERT LINES OF GT_SFLIGHT2 INTO TABLE GT_SFLIGHT . WRITE:'使用索引追加数据前内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP. ULINE. GS_SFLIGHT-CARRID = 'AB'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2000. INSERT GS_SFLIGHT INTO GT_SFLIGHT INDEX 2. *INSERT GS_SFLIGHT INTO GT_SFLIGHT INDEX 3. WRITE:'使用索引追加数据后内表数据'. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示(排序表使用索引INSERT)
添加到INDEX3的位置,影响整表排序,程序报错