本文主要介绍一下SAP ABAP中内表的增删查改语句中的追加内表数据的APPEND相关语句,包括使用索引追加数据(APPEND语句不能使用关键字追加数据)以及不同类型内表使用APPEND追加数据的不同效果
利用索引追加单条数据
APPEND wa TO itab [SORTED BY col].
PS:如果使用[SORTED BY col]语句则会自动以字段col为基准降序排序后追加数据,此语句只适用于标准表,另外还需用[INITIAL SIZE n]指定内表大小!!
案例代码演示(不使用[SORTED BY col]语句)
DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 2000. APPEND GS_SFLIGHT TO GT_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2500. APPEND GS_SFLIGHT TO GT_SFLIGHT. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示(不使用[SORTED BY col]语句)
案例代码演示(使用[SORTED BY col]语句)
DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT INITIAL SIZE 3, GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 2000. APPEND GS_SFLIGHT TO GT_SFLIGHT SORTED BY CARRID. GS_SFLIGHT-CARRID = 'AB'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2500. APPEND GS_SFLIGHT TO GT_SFLIGHT SORTED BY CARRID. GS_SFLIGHT-CARRID = 'AD'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200701'. GS_SFLIGHT-PRICE = 3000. APPEND GS_SFLIGHT TO GT_SFLIGHT SORTED BY CARRID. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示(使用[SORTED BY col]语句)
利用索引追加多条数据
APPEND LINES OF itab1 [FROM n1 TO n2] TO itab2.
案例代码演示
DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT, GT_SFLIGHT2 TYPE TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 2000. APPEND GS_SFLIGHT TO GT_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2500. APPEND GS_SFLIGHT TO GT_SFLIGHT . APPEND LINES OF GT_SFLIGHT TO GT_SFLIGHT2. LOOP AT GT_SFLIGHT2 INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示
不同类型内表APPEND的不同效果
\ | 标准表 | 排序表 | 哈希表 |
区别 | 1.默认追加数据到最后一行 2.可以使用[SORTED BY col]语句 |
需要将追加的数据按关键字排列好,否则程序会报错 | 不能使用APPEND语句 |
案例代码演示(排序表使用APPEND语句按顺序追加数据)
DATA:GT_SFLIGHT TYPE SORTED TABLE OF SFLIGHT WITH UNIQUE KEY CARRID, GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AB'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 2000. APPEND GS_SFLIGHT TO GT_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2500. APPEND GS_SFLIGHT TO GT_SFLIGHT. GS_SFLIGHT-CARRID = 'AD'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200701'. GS_SFLIGHT-PRICE = 3000. APPEND GS_SFLIGHT TO GT_SFLIGHT. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示(排序表使用APPEND语句按顺序追加数据)
案例代码演示(排序表使用APPEND语句不按顺序追加数据)
DATA:GT_SFLIGHT TYPE SORTED TABLE OF SFLIGHT WITH UNIQUE KEY CARRID, GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200618'. GS_SFLIGHT-PRICE = 2000. APPEND GS_SFLIGHT TO GT_SFLIGHT. GS_SFLIGHT-CARRID = 'AB'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200619'. GS_SFLIGHT-PRICE = 2500. APPEND GS_SFLIGHT TO GT_SFLIGHT. GS_SFLIGHT-CARRID = 'AD'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200701'. GS_SFLIGHT-PRICE = 3000. APPEND GS_SFLIGHT TO GT_SFLIGHT. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示(排序表使用APPEND语句不按顺序追加数据)
案例代码演示(哈希表使用APPEND语句追加数据)
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 = '20200618'. GS_SFLIGHT-PRICE = 2000. APPEND GS_SFLIGHT TO GT_SFLIGHT. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果演示(哈希表表使用APPEND语句追加数据)
APPEND INITIAL LINE(追加空行)
APPEND INITIAL LINE TO itab.
案例代码演示
DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. APPEND INITIAL LINE TO GT_SFLIGHT. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE. ENDLOOP.
效果代码演示