From:
1, http://blog.sina.com.cn/s/blog_7dce1fac01014yp2.html
2, http://www.cnblogs.com/cnlmjer/archive/2012/04/18/4099808.html
3,http://www.sap123.com/a/47/
目的:
一、sap的更改记录的保存
2、表CDHDR
表CDHDR记录了用户于什么时间点用什么样的事务代码修改了什么样的对象
在表CDHDR字段:Change doc. Object代表了修改的对象
3、表CDPOS 是更改记录的行项目
记录了具体是哪个表中的哪个字段,从什么值修改为了什么值
如需查看文档对象,可使用事务代码SCDO,相关表为TCDOB/TCDOBT
CHNGIND(修改标识符)输入:D(U表示更新,I表示插入,E表示删除单个字段,D表示删除整行或整个单据)
二、更改记录的报表查询
1、可以在t-code中点击changes,来进行查询;
2、通过专门的t-code查询某个具体Change Object
如OV51 查看:Display of Changes for Customer
ME14 查看 changes to Purchasing Info Record
3、通过事务代码AUT10 可以查询所有的Change Object,实际上就是针对上述2个表做了一个报表
三、表CDHDR and CDPOS更新原则
1、创建时,系统的更新原则与更新内容。
可以通过创建主数据、业务数据、创建业务数据项目来测试
基本的结论是
创建时一般不保存记录,即使保存,也只是简单记录一下,能够在上述2个表"新建一条数据"
2、修改数据时,系统的更新原则与更新内容。
基本原则:以下类型的字段不会查看到更改记录
- 不能够修改的字段,例如创建日期、创建者;
- 在可以通过其他途径可以看到的字段(在其他表中可以看到更改的东西);如在销售订单中,对行项目的数量、金额更改在表VBEP(数量更改)、KONA(金额更改)中
- 其他一些特殊情况
具体的控制点是sap会对 表 中的每一个 字段 都会有一个标记LOGFLAG,该 字段 控制Indicator for writing change documents
如何使用
Change SAP documents -- tables CDHDR and CDPOS
Changes to a lot of SAP documents are stored in table CDHDR and table CDPOS. This include changes such as: change data in Material Master, changes to Purchase Req (PR), Purch Orders, Contracts, Sales Orders, and many more.
Lets look at an example where an existing purchase order (PO) is changed.
- Create a PO (number 4500000616)
- Now lets make a change to the PO. Lets change the Short text in item 10 from "FLOWERS" to "SAP123".
The change information can be see in Purchase Order. Menu: Environment > Item Changes
This changed information are stored in table CDHDR and table CDPOS. Lets see if we can find it by displaying the table using transaction se16 (display table):
The detail of what was changed is in table CDPOS. The link is via the document number, field CHANGENR. Lets look at table CDPOS
These tables contains not just PO changes but changes to most SAP documents (accross all functional areas). So, as you can imagine these tables get huge. Entries in tables are grouped by Object Class (field OBJECTCLAS -- Change doc object). Some of these options include:
OBJECTCLAS - transaction (fields in CDHDR)
- ADRESSE - xk01
- ADRESSE3 - su01
- BANF - me54n
- BELEG - vf21
- BETRIEB - vd02
- COND_A - me31k
- DEBI - xd01, xd02
- EINKBELEG - me22n <<< example above
- ENTRYSHEET - ml85
- FEATURE - ct04
- INCOMINGINVOICE - mrbr
- INFOSATZ - me13
- KLASSE - cl02
- KRED - xk02
- LIEFERUNG - vl03n
- MATERIAL - mm01
- MM_SERVICE - me22n
- MRP_AREA - mm01
- NRINTERVAL - omh6
- PFCG - pfcg
- REVISION - me32k
- SACH - fs00
- SPEC_TMP - ml10
- STLV - ml02
- VASMD - ac03
- VERKBELEG - va02
- .... etc
提取旧值方法
CALL FUNCTION 'CHANGEDOCUMENT_READ_POSITIONS' "Change document: Read change document items
EXPORTING
* archive_handle = 0 " sy-tabix Handle on Open Archive Files
changenumber = " cdpos-changenr Change document number
* tablekey = SPACE " cdpos-tabkey Object class table key
* tablename = SPACE " cdpos-tabname Object class table name
* tablekey254 = SPACE " cdpos_uid-tabkey Table Key for CDPOS in Character 254
* keyguid = SPACE " cdpos_uid-keyguid UUID in Character Format
* keyguid_str = SPACE " cdpos_str-keyguid UUID in Character Format
IMPORTING
header = " cdhdr Change document header (structure CDHDR)
et_cdred_str = " cdred_str_tab Additional Change Document - Table for STRINGs
* TABLES
* editpos = " cdshw Table with edited change document items
EDITPOS_WITH_HEADER = "CDRED 更改文档,显示结构
EXCEPTIONS
NO_POSITION_FOUND = 1 " No item foun
WRONG_ACCESS_TO_ARCHIVE = 2 " incorrect access to archive
2)使用select语句直接从表中读取。
直接使用SELECT语句读取数据的示例:
*提取信用额度字段修改的抬头信息
select cdhdr~changenr cdhdr~udate cdhdr~utime
into corresponding fields of table p_cdhdr
from cdhdr
where cdhdr~objectclas = 'KLIM' and
cdhdr~objectid = wa_customerinfo-kunnr.
if sy-subrc = 0.
*提取信用额度字段修改的字段值
select cdpos~changenr cdpos~value_old cdpos~value_new
into corresponding fields of table p_cdpos
from cdpos
for all entries in p_cdhdr
where cdpos~objectclas = 'KLIM' and
cdpos~objectid = wa_customerinfo-kunnr and
cdpos~changenr = p_cdhdr-changenr and
cdpos~tabname = 'KNKK' and
cdpos~fname = 'KLIMK'.
if sy-subrc = 0.
endif.
endif.
可以在CHANGEDOCUMENT_READ_HEADERS 中设置中断获得对象类型。
ex.
提取有变动的 Acc. changes
SELECT SINGLE tabkey value_new INTO (cdpos-tabkey,ikoinh)
FROM cdpos
WHERE objectclas = 'KRED'
AND objectid = itab-lifnr
AND changenr = itab-changenr
AND tabname = 'LFBK'
AND fname = 'KOINH'.
提取有变动的vendor最新日期
SELECT MAX( udate ) INTO itab-cndate FROM cdhdr
WHERE objectclas = 'KRED' AND objectid = itab-lifnr.
--------------------------------------------------------------------------------
抓取所有有变动的程序
report ztest001.
TYPE-POOLS slis.
DATA : cdhdr TYPE cdhdr.
SELECT-OPTIONS :
s_objcls FOR cdhdr-objectclas OBLIGATORY,
s_objtid FOR cdhdr-objectid,
s_chngnr FOR cdhdr-changenr,
s_usrnam FOR cdhdr-username DEFAULT sy-uname,
s_udate FOR cdhdr-udate DEFAULT sy-datum,
s_time FOR cdhdr-utime,
s_tcode FOR cdhdr-tcode,
s_plncnr FOR cdhdr-planchngnr,
s_chngno FOR cdhdr-act_chngno,
s_wsplnd FOR cdhdr-was_plannd,
s_chngid FOR cdhdr-change_ind.
SELECTION-SCREEN SKIP.
PARAMETERS p_max TYPE numc3 OBLIGATORY DEFAULT '200'.
*---------------------------------------------------------------------*
TYPES :
BEGIN OF ty_s_cdhdr.
INCLUDE STRUCTURE cdhdr.
TYPES : checkbox,
END OF ty_s_cdhdr,
BEGIN OF ty_s_cdpos.
INCLUDE STRUCTURE cdpos.
TYPES : checkbox,
END OF ty_s_cdpos.
*---------------------------------------------------------------------*
DATA :
* Layout for ALV
gs_layout TYPE slis_layout_alv,
* Change document header
t_cdhdr TYPE TABLE OF ty_s_cdhdr.
*---------------------------------------------------------------------*
START-OF-SELECTION.
* Read Change document header
SELECT * INTO TABLE t_cdhdr
UP TO p_max ROWS
FROM cdhdr
WHERE objectclas IN s_objcls
and objectid in s_objtid
and changenr in s_chngnr
AND username IN s_usrnam
AND udate IN s_udate
AND utime IN s_time
AND tcode IN s_tcode
AND planchngnr IN s_plncnr
AND act_chngno IN s_chngno
AND was_plannd IN s_wsplnd
AND change_ind IN s_chngid.
gs_layout-zebra = 'X'.
gs_layout-colwidth_optimize = 'X'.
gs_layout-box_fieldname = 'CHECKBOX'.
* Display ALV
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-cprog
i_callback_user_command = 'USER_COMMAND'
i_structure_name = 'CDHDR'
is_layout = gs_layout
TABLES
t_outtab = t_cdhdr.
*---------------------------------------------------------------------*
* FORM USER_COMMAND *
*---------------------------------------------------------------------*
FORM user_command USING u_ucomm TYPE syucomm
us_selfield TYPE slis_selfield. "#EC CALLED
* Macro definition
DEFINE m_sort.
add 1 to ls_sort-spos.
ls_sort-fieldname = &1.
ls_sort-up = 'X'.
append ls_sort to lt_sort.
END-OF-DEFINITION.
DATA :
ls_cdhdr TYPE ty_s_cdhdr,
ls_sort TYPE slis_sortinfo_alv,
lt_sort TYPE slis_t_sortinfo_alv,
* Change document items
lt_cdpos TYPE TABLE OF ty_s_cdpos.
CASE u_ucomm.
WHEN '&IC1'.
PERFORM check_marked USING us_selfield.
* Read Change document items
LOOP AT t_cdhdr INTO ls_cdhdr WHERE checkbox = 'X'.
SELECT * APPENDING TABLE lt_cdpos
FROM cdpos
WHERE objectclas = ls_cdhdr-objectclas
AND objectid = ls_cdhdr-objectid
AND changenr = ls_cdhdr-changenr.
ENDLOOP.
m_sort 'CHANGENR'.
* Display ALV
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = 'CDPOS'
is_layout = gs_layout
it_sort = lt_sort
TABLES
t_outtab = lt_cdpos.
ENDCASE.
ENDFORM. " USER_COMMAND
*--------------------------------------------------------------------
FORM check_marked USING us_selfield TYPE slis_selfield.
FIELD-SYMBOLS :
<cdhdr> TYPE ty_s_cdhdr.
READ TABLE t_cdhdr TRANSPORTING NO FIELDS WITH KEY checkbox = 'X'.
IF NOT sy-subrc IS INITIAL AND
NOT us_selfield-tabindex IS INITIAL.
READ TABLE t_cdhdr INDEX us_selfield-tabindex ASSIGNING <cdhdr>.
<cdhdr>-checkbox = 'X'.
ENDIF.
ENDFORM.