基本上我找的邏輯來生成觸發字符串變量插入或更新語句。假設當人們剛剛更新一樣update語句領域的「N」 ......然後我的觸發條件會,並從觸發,我想建立什麼更新語句是由用戶和存儲在字符串變量發出動態生成UPDATE語句INSERT/UPDATE內/ DELETE觸發
以同樣的方式,我想從觸發器構建插入語句,但我沒有得到邏輯。所以如果有人有任何想法或示例代碼,那麼請與我分享,以實現我的目標。
ALTER TRIGGER WSContent AFTER INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON;
DECLARE @Action VARCHAR(10)
DECLARE @PKValue INT
DECLARE @TableName VARCHAR(50)
SET @TableName='website_content'
IF EXISTS(SELECT * FROM INSERTED)
BEGIN
IF EXISTS(SELECT * FROM DELETED)
BEGIN
SET @Action ='U';
SELECT @PKValue=ContentNumber from DELETED
END
ELSE
BEGIN
SET @Action ='I';
SELECT @PKValue=ContentNumber from INSERTED
END
END
ELSE
BEGIN
SET @Action = 'D';
SELECT @PKValue=ContentNumber from DELETED
END;
INSERT INTO [ContentChangeLog]([PkValue],[TableName],[Action],ActionDate)
VALUES(@PKValue,@TableName,@Action,GetDate())
SET NOCOUNT OFF;
良好的審計線索腳本http://stackoverflow.com/questions/15941881/sql-server-auto-audit-updated-column – Thomas 2014-09-08 14:54:48