SAP ABAP——内表(六)【追加内表数据—APPEND】

简介: 本文主要介绍一下SAP ABAP中内表的增删查改语句中的追加内表数据的APPEND相关语句,包括使用索引追加数据(APPEND语句不能使用关键字追加数据)以及不同类型内表使用APPEND追加数据的不同效果

本文主要介绍一下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]语句)

e3881c378f2ebd7242a3d69dbd389f5.png


案例代码演示(使用[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]语句)

f57f067a9ced843e35838afa4f397c6.png


利用索引追加多条数据

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.

效果演示

847bacf0f00f52c5334e17eef8c9a42.png


不同类型内表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语句按顺序追加数据)

f6332b097cc507942743338ec714093.png


案例代码演示(排序表使用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语句不按顺序追加数据)

684d3fa1d511720489bbff4b2cf85fe.png


案例代码演示(哈希表使用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语句追加数据)

b7f22137d5a9463a159a0ad7c78ccd0.png


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.

效果代码演示

db5a295625872dbc8452f975e46a8c3.png

目录
相关文章
SAP ABAP 程序调用设置成后台作业模式
ABAP 程序调用设置成后台作业模式
516 0
|
SQL
SAP ABAP-OPENSQL-数值函数简介
S4中针对OPENSQL新提供一些函数
139 0
|
SQL
SAP ABAP-OPENSQL-字符串函数介绍
S4中SQL新语法字符串函数
209 0
|
SQL
SAP ABAP-OPENSQL-日期/时间戳函数
S4提供针对OPENSQL一些新的SQL函数调用,本文展示相关函数的使用
649 0
SAP ABAP-删除工序示例代码
ABAP-删除工序示例代码
171 0
SAP MM/FI_运费处理方式
常见的采购运费处理方式
SAP MM 途损处理方式
通常客户采购业务需求提到货物运输有损耗,需要针对此业务给出合理方案输出,下面笔者针对此类业务分析下各种实现方案的可行性!
SAP MM初阶之事务代码MIGO界面批次拆分最多输入15行?
SAP MM初阶之事务代码MIGO界面批次拆分最多输入15行?
SAP MM初阶之事务代码MIGO界面批次拆分最多输入15行?
SAP MM不常用移动类型之325
SAP MM不常用移动类型之325
SAP MM不常用移动类型之325