SAP ABAP——内表(五)【追加内表数据—INSERT】

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

本文主要介绍一下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.

效果演示

6b434c3057b2de686368ceeffd1f6ec.png


利用关键字追加多条数据

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.

效果演示

8617c818bd992a12e79e33fb71d3a5f.png


利用索引追加单条数据

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.

效果演示

47aa0874e68aeed5f8f1d8b209e9580.png


利用索引追加多条数据

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.

效果演示

6d1751b7e921a8cdc65c087ff365e1c.png


不同类型内表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)

排序表效果

59b3849177d7b3f1a32f869f68219b1.png

哈希表效果

718a5e729064c74e6c4181a3d51586f.png



案例代码演示(排序表使用索引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)


a2db0a49354c0d5bc19566ac04d3d87.png

添加到INDEX3的位置,影响整表排序,程序报错

149c541b2f38a0d464e11b6d94e23b8.png

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