如何将审计操作添加到审计表

时间:2017-06-03 03:02:02

标签: sql-server tsql

如何将审核操作添加到审核表。我有下表和触发器来跟踪更改。哪个商店,但我想看到具体的变化。例如;如果更新字段x值从1到5.我想在审计条目上看到它,告诉我该记录的字段x从1更改为5。目前我得到两行,如下所示

  <deleted PART_x0020_LOOKUP_x0020_H="1005551 ,Part:  .325 ,  , , ()" STOCK_x0020_NO="1005551" PART_x0020_LOOKUP_x0020_Vold="1005551 ,Part:  , , , ()" PART_x0020_LOOKUP_x0020_Hnew="1005551 ,Part:  .325 " SIZE=".325" Created_x0020_By="IT-1" Updated_x0020_Date="Jun  2 2017 10:08AM" Last_x0020_Updated_x0020_By="IT-1" SALES_x0020_DESCRIPTION=" .325 " PURCHASE_x0020_DESCRIPTION=" .325 " ITEM_x0020_DESCRIPTION=" .325 " PART_x0020_LOOKUP_x0020_V="1005551 ,Part:  , , , ()" Created_x0020_Date="Jun  2 2017 10:07AM" /> 

<deleted PART_x0020_LOOKUP_x0020_H="1005551 ,Part:  .325 ,  , , ()" STOCK_x0020_NO="1005551" PART_x0020_LOOKUP_x0020_Vold="1005551 ,Part:  , , , ()" PART_x0020_LOOKUP_x0020_Hnew="1005551 ,Part:  .325 " SIZE=".325" Created_x0020_By="IT-1" Updated_x0020_Date="Jun  2 2017 10:08AM" Last_x0020_Updated_x0020_By="IT-1" SALES_x0020_DESCRIPTION=" .325 " PURCHASE_x0020_DESCRIPTION=" .325 " ITEM_x0020_DESCRIPTION=" .325 " PART_x0020_LOOKUP_x0020_V="1005551 ,Part:  , , , ()" Created_x0020_Date="Jun  2 2017 10:07AM" />

//这是表格

CREATE TABLE [dbo].[AuditAll](
    [AuditId] [int] IDENTITY(1,1) NOT NULL,
    [DateTime] [datetime] NULL,
    [TableName] [nvarchar](255) NULL,
    [AuditEntry] [xml] NULL,
 CONSTRAINT [PK_AuditAll] PRIMARY KEY CLUSTERED 
(
    [AuditId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

这是表格上的触发器

create TRIGGER [dbo].[AuditSimple] 
 ON  [dbo].[PARTS]
 AFTER  INSERT,DELETE,UPDATE
 AS 
 BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

IF (SELECT COUNT(*) FROM deleted) > 0 
begin
    Declare @AuditMessage XML
    --set valut to all xml from deleted table
    set @AuditMessage =  (select * from deleted for xml auto) 

    insert into AuditAll(DateTime,TableName,AuditEntry) 
        values (GetDate(),'Simple',@AuditMessage )
end

END

0 个答案:

没有答案