本文主要介绍SAP ABAP中OPEN SQL的数据操控语言(DML)
ABAP DML语句分类
DML语句(data manipulation language)又称数据操控语言,在传统数据库中数据操控语言只有三种,分别是INSERT,UPDATE,DELETE语句,而在SAP ABAP OPEN SQL中DML语句还额外多了一个MODIFY语句,该语句融合了INSERT和UPDATE语句的特点。ABAP四种DML语句的基本介绍如下表所示
语句 | 功能 |
INSERT | 插入语句 |
UPDATE | 更新语句 |
DELETE | 删除语句 |
MODIFY | 插入&更新语句 |
INSERT语句
插入单条数据语句
INSERT INTO <target> VALUES <wa>. INSERT <target> FROM <wa>.
案例代码演示
DATA:GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. INSERT INTO SFLIGHT VALUES GS_SFLIGHT.
效果演示
插入多条数据语句
INSERT <target> FROM TABLE <itab> [ACCEPTING DUPLICATE KEYS].
注:插入相同主键的数据时会发生dump error,为了避免发生这种错误要使用ACCEPTING DUPLICATE KEYS语句
案例代码演示
DATA:GS_SFLIGHT TYPE SFLIGHT. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20020217'. APPEND GS_SFLIGHT TO GT_SFLIGHT. INSERT SFLIGHT FROM TABLE GT_SFLIGHT.
效果演示
UPDATE语句
修改单条数据语句
UPDATE <target> FROM <wa>.
案例代码演示
DATA:GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '00000000'. GS_SFLIGHT-PRICE = 1600. UPDATE SFLIGHT FROM GS_SFLIGHT.
效果演示
修改多条数据语句
UPDATE <target> FROM TABLE <itab>. UPDATE <target> SET <set1> <set2> WHERE <cond>.
案例代码演示(从内表更新数据库表)
DATA:GS_SFLIGHT TYPE SFLIGHT. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '00000000'. GS_SFLIGHT-PRICE = 2000. APPEND GS_SFLIGHT TO GT_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20020217'. GS_SFLIGHT-PRICE = 2100. APPEND GS_SFLIGHT TO GT_SFLIGHT. UPDATE SFLIGHT FROM TABLE GT_SFLIGHT.
效果演示
案例代码演示(修改指定字段)
UPDATE SFLIGHT SET PRICE = 1600 CURRENCY = 'CNY' WHERE CARRID = 'AC' AND CONNID = 0820 AND FLDATE = '00000000'.
效果演示
DELETE语句
删除单条数据
DELETE <target> FROM <wa>.
案例代码演示
DATA:GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-PRICE = 1600. DELETE SFLIGHT FROM GS_SFLIGHT.
效果演示
删除多条数据
DELETE FROM <target> WHERE <cond>.
案例代码演示
DELETE FROM SFLIGHT WHERE PRICE = 1600 OR PRICE = 2100.
效果演示
MODIFY语句
插入/更新单条数据
MODIFY <target> FROM <wa>.
案例代码演示(插入数据)
DATA:GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200615'. GS_SFLIGHT-PRICE = 1600. MODIFY SFLIGHT FROM GS_SFLIGHT.
效果演示
案例代码演示(更新数据)
DATA:GS_SFLIGHT TYPE SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200615'. GS_SFLIGHT-PRICE = 2100. MODIFY SFLIGHT FROM GS_SFLIGHT.
效果演示
插入/更新多条数据
MODIFY <target> FROM TABLE <itab>.
案例代码演示(更新数据)
DATA:GS_SFLIGHT TYPE SFLIGHT. DATA:GT_SFLIGHT TYPE TABLE OF SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200615'. GS_SFLIGHT-PRICE = 1600. APPEND GS_SFLIGHT TO GT_SFLIGHT. GS_SFLIGHT-CARRID = 'AC'. GS_SFLIGHT-CONNID = 0820. GS_SFLIGHT-FLDATE = '20200617'. GS_SFLIGHT-PRICE = 2100. APPEND GS_SFLIGHT TO GT_SFLIGHT. MODIFY SFLIGHT FROM TABLE GT_SFLIGHT.
效果演示