ABAP 给数据表写修改记录

  • 在调用BAPI时,一般会自动写数据表的修改记录,但有些简单的逻辑,不需要使用BAPI实现,直接使用SQL语句修改数据表时,为了方便查找字段的修改记录可以以下方法实现。

DATA: OBJECTID TYPE CDHDR-OBJECTID,
TCODE TYPE CDHDR-TCODE,
PLANNED_CHANGE_NUMBER TYPE CDHDR-PLANCHNGNR,
UTIME TYPE CDHDR-UTIME,
UDATE TYPE CDHDR-UDATE,
USERNAME TYPE CDHDR-USERNAME,
CDOC_PLANNED_OR_REAL TYPE CDHDR-CHANGE_IND,
CDOC_UPD_OBJECT TYPE CDHDR-CHANGE_IND VALUE ‘U’,
CDOC_NO_CHANGE_POINTERS TYPE CDHDR-CHANGE_IND.

**准备修改文档的参数,用户名,时间,ID,等
PERFORM prepare_change_document USING i_lifnr.
**因为只对lfa1的名称,是否冻结字段进行更改,所以直接用UPDATE语句

PERFORM update_lfa1 USING ns_lfa1.
** 写表的修改记录*
PERFORM write_document USING ‘KRED’ ‘LFA1’ ns_lfa1 os_lfa1.

FORM update_lfa1 USING ns_lfa1 STRUCTURE lfa1.
UPDATE lfa1 FROM ns_lfa1.
ENDFORM.

FORM prepare_change_document USING i_id.

udate = sy-datum.
utime = sy-uzeit.
objectid = i_id.
tcode = ‘SRM’.
username = sy-uname.

ENDFORM.

FORM write_document USING i_objectclass i_tablename
ns_workarea os_workarea.

CALL FUNCTION ‘CHANGEDOCUMENT_OPEN’
EXPORTING
objectclass = i_objectclass
objectid = objectid
planned_change_number = planned_change_number
PLANNED_OR_REAL_CHANGES = PLANNED_OR_REAL_CHANGES
EXCEPTIONS
sequence_invalid = 1
OTHERS = 2.

CASE sy-subrc.
WHEN 0. "OK.
WHEN 1. MESSAGE a600(cd) WITH ‘SEQUENCE INVALID’.
WHEN 2. MESSAGE a600(cd) WITH ‘OPEN ERROR’.
ENDCASE.

CALL FUNCTION ‘CHANGEDOCUMENT_SINGLE_CASE’
EXPORTING
tablename = i_tablename
workarea_old = os_workarea
workarea_new = ns_workarea
change_indicator = ‘U’
docu_delete = ‘’
docu_insert = ‘’
EXCEPTIONS
nametab_error = 1
open_missing = 2
position_insert_failed = 3
OTHERS = 4.

CASE sy-subrc.
WHEN 0. "OK.
WHEN 1. MESSAGE a600(cd) WITH ‘NAMETAB-ERROR’.
WHEN 2. MESSAGE a600(cd) WITH ‘OPEN MISSING’.
WHEN 3. MESSAGE a600(cd) WITH ‘INSERT ERROR’.
WHEN 4. MESSAGE a600(cd) WITH ‘SINGLE ERROR’.
ENDCASE.

CALL FUNCTION ‘CHANGEDOCUMENT_CLOSE’
EXPORTING
objectclass = i_objectclass
objectid = objectid
date_of_change = udate
time_of_change = utime
tcode = tcode
username = username
OBJECT_CHANGE_INDICATOR = OBJECT_CHANGE_INDICATOR
NO_CHANGE_POINTERS = NO_CHANGE_POINTERS
EXCEPTIONS
header_insert_failed = 1
object_invalid = 2
open_missing = 3
no_position_inserted = 4
OTHERS = 5.

CASE sy-subrc.
WHEN 0. "OK.
WHEN 1. MESSAGE a600(cd) WITH ‘INSERT HEADER FAILED’.
WHEN 2. MESSAGE a600(cd) WITH ‘OBJECT INVALID’.
WHEN 3. MESSAGE a600(cd) WITH ‘OPEN MISSING’.
WHEN 4. MESSAGE A600 WITH ‘NO_POSITION_INSERTED’.
" do not abort, if positions are not inserted!!!
WHEN 5. MESSAGE a600(cd) WITH ‘CLOSE ERROR’.
ENDCASE.

ENDFORM.

上一篇:MySQL--行列转换


下一篇:Visual Studio 2022 Preview 3和2019 16.11发布