个人网站:【芒果个人日志】
原文地址:SAP ABAP——内表(八)【修改内表数据】 - 芒果个人日志 (wyz-math.cn)
作者简介: THUNDER王,一名热爱财税和SAP ABAP编程以及热爱分享的博主。目前于江西师范大学会计学专业大二本科在读,同时任汉硕云(广东)科技有限公司ABAP开发顾问。在学习工作中,我通常使用偏后端的开发语言ABAP,SQL进行任务的完成,对SAP企业管理系统,SAP ABAP开发和数据库具有较深入的研究。
文章概要:本文主要介绍一下SAP ABAP中内表的增删查改语句中的修改内表数据的相关语句,包括利用关键字修改数据,利用索引修改数据,利用WHERE语句修改数据
每日一言:通过辛勤工作获得财富才是人生的大快事
目录
利用关键字修改数据
- 案例代码演示
- 效果演示
利用索引修改数据
- 案例代码演示
- 效果演示
利用WHERE语句修改数据
- 案例代码演示
- 效果演示
MODIFY TABLE itab FTOM wa [TRANSPORTING f1 f2...].
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.
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 = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200717'.
GS_SFLIGHT-PRICE = 1600.
MODIFY 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.
GS_SFLIGHT-CARRID = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200715'.
GS_SFLIGHT-PRICE = 1700.
MODIFY TABLE GT_SFLIGHT FROM GS_SFLIGHT TRANSPORTING PRICE.
ULINE.
WRITE:'内表修改后数据(指定修改PRICE字段,故FLDATE字段没有修改)'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
使用关键字修改内表数据
MODIFY itab INDEX n [TRANSPORTING f1 f2...].
PS:
1) 在LOOP循环中,MODIFY后不需要加INDEX,系统自动将数据更新到当前所在索引行
2) 使用索引修改数据只适用于标准表和排序表,不适用于哈希表
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 = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '202007174'.
GS_SFLIGHT-PRICE = 1800.
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 = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200715'.
GS_SFLIGHT-PRICE = 1700.
MODIFY GT_SFLIGHT FROM GS_SFLIGHT INDEX 2.
ULINE.
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 = 'AC'.
GS_SFLIGHT-CONNID = 0820.
GS_SFLIGHT-FLDATE = '20200721'.
GS_SFLIGHT-PRICE = 1900.
MODIFY GT_SFLIGHT FROM GS_SFLIGHT INDEX 1 TRANSPORTING PRICE.
ULINE.
WRITE:'内表修改后数据(指定修改PRICE字段,故FLDATE字段没有修改)'.
LOOP AT GT_SFLIGHT INTO GS_SFLIGHT.
WRITE:/ GS_SFLIGHT-CARRID,GS_SFLIGHT-CONNID,GS_SFLIGHT-FLDATE,GS_SFLIGHT-PRICE.
ENDLOOP.
使用索引修改内表数据
MODIFY itab FROM wa TRANSPORTING f1 f2... WHERE condition.
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-PRICE = 2000.
MODIFY GT_SFLIGHT FROM GS_SFLIGHT TRANSPORTING PRICE 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.
使用where语句修改内表数据