ABAP 给数据表写修改记录

16 篇文章 1 订阅
本文档介绍了一种在不使用BAPI的情况下,通过SQL语句直接修改数据表,并同时记录修改日志的方法。首先,定义了所需的数据变量,然后准备修改文档所需的参数,如用户名、时间、ID等。接着,使用UPDATE语句更新指定字段。最后,通过调用SAP的改变文档相关函数,记录修改操作,包括打开、单个案例处理和关闭修改文档的过程。这种方法确保了数据修改的同时,也保留了审计追踪信息。
摘要由CSDN通过智能技术生成
  • 在调用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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值