我想要做的是找出哪些字段已更新並將更改記錄到不同的表中。基於觸發器的歷史記錄
DECLARE
@BillNo int,
@column_name varchar(500)
SELECT @BillNo = BillNo FROM INSERTED
DECLARE HistoryMonitorLoop CURSOR FOR
SELECT
column_name
FROM
information_schema.columns
WHERE
table_name = 'Shipment';
OPEN HistoryMonitorLoop
FETCH next FROM HistoryMonitorLoop INTO @column_name
WHILE @@Fetch_status = 0
BEGIN
DECLARE
@OldValue varchar(500),
@NewValue varchar(500)
SET @OldValue = (SELECT @column_name FROM Deleted);
SET @NewValue = (SELECT @column_name FROM Inserted);
IF(@OldValue != @NewValue)
BEGIN
DECLARE @Comment varchar(5000)
SELECT @Comment = @column_name + ' Changed from ' + @OldValue + ' to ' + @NewValue
EXEC ShipmentNote_Insert @[email protected],@CoordinatorID=1,@[email protected]
END
FETCH next FROM HistoryMonitorLoop INTO @column_name
END
CLOSE HistoryMonitorLoop
DEALLOCATE HistoryMonitorLoop
正在發生的事情是
SET @OldValue = (SELECT @column_name FROM Deleted);
SET @NewValue = (SELECT @column_name FROM Inserted);
被設置@OldValue
和@NewValue
=的列名,而不是列的值 - SQL處理它作爲SET @OldValue = (SELECT 'column_name' FROM Deleted);
那麼,腳本是否正常工作?你有錯誤嗎?你是否在測試之前問它是否好像? – BradC 2009-04-13 22:59:21
和?,最新的問題是什麼? – 2009-04-13 23:24:00
正在發生的事是 SET @OldValue =(SELECT @column_name FROM Deleted); SET @NewValue =(SELECT @column_name FROM Inserted); 正在將@OldValue和@NewValue =設置爲列名,而不是列的值 – 2009-04-13 23:35:39