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

举报
Thunder Wang 发表于 2023/01/31 22:14:04 2023/01/31
【摘要】 本文主要介绍一下SAP ABAP中内表的增删查改语句中的删除内表数据的相关语句,包括利用关键字删除数据,利用索引删除数据,利用WHERE语句删除数据,删除重复语句行。

💂作者简介: THUNDER王,一名热爱财税和SAP ABAP编程以及热爱分享的博主。目前于江西师范大学会计学专业大二本科在读,同时任汉硕云(广东)科技有限公司ABAP开发顾问。在学习工作中,我通常使用偏后端的开发语言ABAP,SQL进行任务的完成,对SAP企业管理系统,SAP ABAP开发和数据库具有较深入的研究。


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

- 效果演示(使用工作区)


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


利用索引删除数据

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. 

- 效果演示

 

 利用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.

- 效果演示


利用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.

- 效果演示

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。