个人网站:【芒果个人日志】
原文地址:
SAP ABAP——内表(六)【追加内表数据—APPEND】 - 芒果个人日志 (wyz-math.cn)
作者简介: THUNDER王,一名热爱财税和SAP ABAP编程以及热爱分享的博主。目前于江西师范大学会计学专业大二本科在读,同时任汉硕云(广东)科技有限公司ABAP开发顾问。在学习工作中,我通常使用偏后端的开发语言ABAP,SQL进行任务的完成,对SAP企业管理系统,SAP ABAP开发和数据库具有较深入的研究。
文章概要:本文主要介绍一下SAP ABAP中内表的增删查改语句中的追加内表数据的APPEND相关语句,包括使用索引追加数据(APPEND语句不能使用关键字追加数据)以及不同类型内表使用APPEND追加数据的不同效果
每日一言:宝剑锋从磨砺出,梅花香自苦寒来。
目录
利用索引追加单条数据
- 案例代码演示(不使用[SORTED BY col]语句)
- 效果演示(不使用[SORTED BY col]语句)
- 案例代码演示(使用[SORTED BY col]语句)
- 效果演示(使用[SORTED BY col]语句)
利用索引追加多条数据
- 案例代码演示
- 效果演示
不同类型内表APPEND的不同效果
- 案例代码演示(排序表使用APPEND语句按顺序追加数据)
- 效果演示(排序表使用APPEND语句按顺序追加数据)
- 案例代码演示(排序表使用APPEND语句不按顺序追加数据)
- 效果演示(排序表使用APPEND语句不按顺序追加数据)
- 案例代码演示(哈希表使用APPEND语句追加数据)
- 效果演示(哈希表表使用APPEND语句追加数据)
APPEND INITIAL LINE(追加空行)
-案例代码演示
-效果代码演示
APPEND wa TO itab [SORTED BY col].
PS:如果使用[SORTED BY col]语句则会自动以字段col为基准降序排序后追加数据,此语句只适用于标准表,另外还需用[INITIAL SIZE n]指定内表大小!!
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.
利用索引追加单条数据
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.
利用索引追加多条数据
\ | 标准表 | 排序表 | 哈希表 |
---|---|---|---|
区别 | 1.默认追加数据到最后一行 2.可以使用[SORTED BY col]语句 |
需要将追加的数据按关键字排列好,否则程序会报错 | 不能使用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.
排序表按顺序追加数据成功
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.
排序表不按顺序追加数据不成功
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 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.
为内表追加了一行空行