个人网站:【芒果个人日志】
原文地址:SAP ABAP——内表(五)【追加内表数据—INSERT】 - 芒果个人日志 (wyz-math.cn)
作者简介: THUNDER王,一名热爱财税和SAP ABAP编程以及热爱分享的博主。目前于江西师范大学会计学专业大二本科在读,同时任汉硕云(广东)科技有限公司ABAP开发顾问。在学习工作中,我通常使用偏后端的开发语言ABAP,SQL进行任务的完成,对SAP企业管理系统,SAP ABAP开发和数据库具有较深入的研究。
文章概要:本文主要介绍一下SAP ABAP中内表的增删查改语句中的追加内表数据的INSERT相关语句,包括使用关键字追加和使用索引追加以及不同类型内表使用INSERT追加数据的不同效果
每日一言:别人能做到的事,自己也能做到
目录
利用关键字追加单条数据
- 案例代码演示
- 效果演示
利用关键字追加多条数据
- 案例代码演示
- 效果演示
利用索引追加单条数据
- 案例代码演示
- 效果演示
利用索引追加多条数据
- 案例代码演示
- 效果演示
不同类型内表INSERT的不同效果
- 案例代码演示(排序表和哈希表使用关键字INSERT)
- 效果演示(排序表和哈希表使用关键字INSERT)
- 案例代码演示(排序表使用索引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效果比较 | 1.默认追加数据到内表最后一行 2.与APPEND语句具有相同效果 |
1.按照内表排序的顺序追加数据 2.若关键字不唯一,重复的数据 会追加到相同数据的上一行中 |
按照表关键字的哈希索引顺序追加数据 |
使用索引INSERT效果比较 | 默认数据追加到内表相应的索引号位置上 | 1.若数据追加到相应索引号后 不影响排序表的排列顺序,则程序不会报错 2.若数据追加到相应索引号后 影响了排序表的排列顺序,则程序报错 |
不能使用索引进行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数据的效果,标准表的案例在这里则不做过多赘述
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.
添加到INDEX2的位置(不影响整表排序,程序不报错)
添加到INDEX3的位置(影响整表排序,程序报错)