本文主要介绍一下SAP ABAP中内表的增删查改语句中的修改内表数据的相关语句,包括利用关键字修改数据,利用索引修改数据,利用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.
效果演示
利用WHERE语句修改数据
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.
效果演示