SAP ABAP——内表(九)【删除内表数据】

简介: 本文主要介绍一下SAP ABAP中内表的增删查改语句中的删除内表数据的相关语句,包括利用关键字删除数据,利用索引删除数据,利用WHERE语句删除数据,删除重复语句行

本文主要介绍一下SAP ABAP中内表的增删查改语句中的删除内表数据的相关语句,包括利用关键字删除数据,利用索引删除数据,利用WHERE语句删除数据,删除重复语句行


利用关键字删除数据

DELETE TABLE itab.
DELETE TABLE itab FROM wa.
DELETE TABLE itab WITH TABLE KEY k1 = f1 k2 = f2 ... kn = fn.

案例代码演示(使用工作区)

DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID,
      GS_SFLIGHT TYPE SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200716'.
GS_SFLIGHT-PRICE = 1500.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1600.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'BC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200718'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
WRITE:'内表数据删除前:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
GS_SFLIGHT-CARRID = 'BC'.
DELETE TABLE GT_SFLIGHT FROM GS_SFLIGHT.   
ULINE.
WRITE:'内表数据删除后:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.

效果演示(使用工作区)

41b193f7b6dce4ee93e51278f89039b.png


案例代码演示(使用TABLE KEY)

DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID FLDATE,
      GS_SFLIGHT TYPE SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200716'.
GS_SFLIGHT-PRICE = 1500.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1600.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'BC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200718'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
WRITE:'内表数据删除前:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
DELETE TABLE GT_SFLIGHT WITH TABLE KEY CARRID = 'AC' FLDATE = '20200716'.
ULINE.
WRITE:'内表数据删除后:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.

效果演示(使用TABLE KEY)

a2d16cf1f8bcd1c2b9e3e14558de448.png


利用索引删除数据

DELETE itab INDEX n.
DELETE itab FROM n1 TO n2.
DELETE itab FROM n1. "删除n1后面的数据(包括n1) 
DELETE itab TO n2.   "删除n2前面的数据(包括n2)


案例代码演示

PS:依次接触下面案例中的DELETE语句的注释代码,观察它们不同的效果

DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID FLDATE,
      GS_SFLIGHT TYPE SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200716'.
GS_SFLIGHT-PRICE = 1500.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1600.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'BC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200718'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
WRITE:'内表数据删除前:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
DELETE  GT_SFLIGHT INDEX 3.
*DELETE GT_SFLIGHT FROM 2 TO 3.
*DELETE GT_SFLIGHT FROM 2.
*DELETE GT_SFLIGHT TO 2.
ULINE.
WRITE:'内表数据删除后:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.

效果演示

a6ff7b8933132a325ddcb52c575ada1.png




利用WHERE语句删除数据

DELETE itab WHERE condition.

案例代码演示

DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID FLDATE,
      GS_SFLIGHT TYPE SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200716'.
GS_SFLIGHT-PRICE = 1500.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1600.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'BC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200718'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
WRITE:'内表数据删除前:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
DELETE GT_SFLIGHT WHERE CARRID = 'AC'.
ULINE.
WRITE:'内表数据删除后:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.

效果演示

9362f6832f6eefe56153c0990235cd2.png


利用ADJACENT DUPLICATE语句删除重复数据

DELETE ADJACENT DUPLICATE FROM itab [COMPARING f1 f2...|ALL FIELDS].

PS:

1)若不指定字段,则系统自动以内表的关键字为基准删除重复数据;若内表没有设置关键字,则自动定义以CHAR类型的字段为关键字

2)使用ADJACENT DUPLICATE语句之前需要对内表进行排序才能达到预期效果


案例代码演示

DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT WITH NON-UNIQUE KEY CARRID,
      GS_SFLIGHT TYPE SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200716'.
GS_SFLIGHT-PRICE = 1500.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
GS_SFLIGHT-CARRID = 'BC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200718'.
GS_SFLIGHT-PRICE = 1700.
APPEND GS_SFLIGHT TO GT_SFLIGHT.
WRITE:'内表数据删除前:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
DELETE ADJACENT DUPLICATES FROM GT_SFLIGHT.
ULINE.
WRITE:'内表数据删除后:'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
  WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.

效果演示

6046f5c4a9fd9abef9dd095b65cb6ff.png

目录
相关文章
|
7月前
SAP CLIENT 数据配置文件的导出/导入
SAP CLIENT 数据配置文件的导出/导入
181 1
SAP ABAP 程序调用设置成后台作业模式
ABAP 程序调用设置成后台作业模式
532 0
|
数据处理
|
SQL
SAP ABAP-OPENSQL-数值函数简介
S4中针对OPENSQL新提供一些函数
145 0
|
SQL
SAP ABAP-OPENSQL-字符串函数介绍
S4中SQL新语法字符串函数
224 0
|
SQL
SAP ABAP-OPENSQL-日期/时间戳函数
S4提供针对OPENSQL一些新的SQL函数调用,本文展示相关函数的使用
698 0
SAP ABAP-删除工序示例代码
ABAP-删除工序示例代码
178 0
|
7月前
|
程序员
开发语言漫谈-ABAP
ABAP是SAP公司专门用于SAP软件环境的专门语言
|
SQL 设计模式 前端开发
【置顶】SAP ABAP开发实战——从入门到精通系列目录
本文章为SAP ABAP开发实战——从入门到精通系列的目录以及关于该教程的后续写作计划表
1674 0
【置顶】SAP ABAP开发实战——从入门到精通系列目录
|
BI
SAP ABAP在线预览文档对象的开发实现
应用场景:有些定制化开发(报表/功能增强等)完成之后,客户需要将其操作手册或者相关文档放在某个报表的初始画面,供实际操作者在线查阅,当然这个功能也同样类似于模板的下载,这里就以在线预览(直接打开)为例进行说明。
231 0
下一篇
DataWorks