SAP ABAP 学习总结(14):Database table Update_Insert_Delete_Modify(数据库增删改查)

UPDATE
  • 第一种:查询一条数据后更新数据
    UPDATE 表名 FROM 结构体.
"声明一个表
DATA:gt_demos TYPE TABLE OF zcurry_table_2.
"声明一个结构体
DATA:gs_demo TYPE zcurry_table_2.

"查询一条数据后更新数据
SELECT SINGLE vbeln posnr matnr arktx FROM zcurry_table_2
  INTO CORRESPONDING FIELDS OF gs_demo
  WHERE vbeln = '0000000001' AND posnr = '000030'.
IF sy-subrc = 0.
  gs_demo-matnr = 'DHA_DEMO_2'.
  gs_demo-arktx = 'TEST'.
  UPDATE zcurry_table_2 FROM gs_demo.
  IF sy-subrc = 0.
    WRITE:'Update Success!'.
    WRITE:gs_demo-vbeln,gs_demo-posnr,gs_demo-matnr,gs_demo-arktx.
  ENDIF.
ENDIF.
  • 第二种:查询数据后更新:UPDATE 表名 SET 字段名=值 WHERE 条件.
"UPDATE 表名 SET 字段名=值 WHERE 条件.
UPDATE zcurry_table_2 SET WAERK = 'RMB'
  WHERE vbeln = '0000000001'.
IF sy-subrc = 0.
  WRITE:'Update Success!'.
ENDIF.
INSERT
  • 通过结构体插入数据:
    INSERT INTO 表 VALUES 结构体.
"声明要插入的数据
gs_demo-vbeln = '0000000003'.
gs_demo-posnr = '000050'.
gs_demo-matnr = 'DHA_DEMO_2'.
gs_demo-arktx = 'Demo Material'.

INSERT INTO zcurry_table_2 VALUES gs_demo.
IF sy-subrc = 0.
  WRITE:'Insert Success!'.
ENDIF.
  • 通过内表插入数据:
    INSERT 表名 FROM TABLE 内表 ACCEPTING DUPLICATE KEYS.
    (ACCEPTING DUPLICATE KEYS 表示跳过出现问题的字段,插入其他没问题的字段)
"通过内表插入数据
gs_demo-vbeln = '0000000003'.
gs_demo-posnr = '000010'.
gs_demo-matnr = 'DHA_DEMO_3'.
gs_demo-arktx = 'Demo Material1'.
"向内表插入数据
APPEND gs_demo to gt_demos.

gs_demo-vbeln = '0000000003'.
gs_demo-posnr = '000060'.
gs_demo-matnr = 'DHA_DEMO_3'.
gs_demo-arktx = 'Demo Material1'.
"向内表插入数据
APPEND gs_demo to gt_demos.

INSERT zcurry_table_2 FROM TABLE gt_demos
ACCEPTING DUPLICATE KEYS.
IF sy-subrc = 0.
  WRITE:'Insert Success!'.
ENDIF.
DELETE
  • 通过WHERE条件删除
DELETE FROM zcurry_table_2 WHERE vbeln = '0000000003'
  AND POSNR = '000020'.
IF sy-subrc = 0.
  WRITE:'删除成功!'.
ENDIF.
  • 通过结构体删除
    DELETE 表 FROM 结构体.
  • 通过内保删除
    DELETE 表 FROM TABLE 内表.

你可能感兴趣的:(ABAP)