您是否使用INSERTED和DELETED表嘗試了Audit Trigger? http://weblogs.asp.net/jgalloway/archive/2008/01/27/adding-simple-trigger-based-auditing-to-your-sql-server-database.aspx
OR
,在插入存儲過程,刪除,更新,你可以使用FOR XML AUTO。獲取記錄的XML並將其添加到審計表。 http://www.a2zdotnet.com/View.aspx?Id=71
UPDATE T-SQL示例
BEGIN
-- these tables would be in your database
DECLARE @table TABLE(ID INT IDENTITY(1,1) PRIMARY KEY, STR VARCHAR(10), DT DATETIME)
DECLARE @audit_table TABLE(AuditXML XML, Type VARCHAR(10), Time DATETIME)
-- this is defined at the top of your stored procedure
DECLARE @temp_table TABLE(PK INT)
-- your stored procedure will add an OUTPUT to the temp table
INSERT INTO @table
OUTPUT inserted.ID INTO @temp_table
VALUES ('test1', GetDate()),
('test2', GetDate() + 2)
-- at the end of your stored procedure update your audit table
INSERT INTO @audit_table
VALUES(
(
SELECT *
FROM @table
WHERE ID IN (SELECT PK FROM @temp_table)
FOR XML AUTO
),
'INSERTION',
GETDATE()
)
-- your audit table will have the record data
SELECT * FROM @audit_table
END
在上面的例子中,你可以做temp_table表的克隆(具有所有列的表),並插入了OUTPUT子句中使用。 * INTO @temp_table,這將避免必須在獲取FOR XML AUTO之前重新選擇記錄。另外請注意,對於執行DELETE的存儲過程,您將在OUTPUT中使用DELETED。*而不是INSERTED。*。
在我們的環境中,觸發器是不允許所以對於XML聽起來像一個選項,遺憾的是本文沒有詳細說明如何使用它進行審計 –
@CD史密斯 - 我用你的例子更新了我的答案。 –