存儲在新表中的現有列值我有兩個表如何使用觸發
- 客戶
- CustomerUpdate
兩個表的結構是這樣的
客戶表的結構
CustomerName | CustomerId
CustomerUpdate表的結構
NewCustomerName | NewCustomerId | OldCustomerName
我插入Customer
表幾個值。無論何時我應該更新此表中的數據,我都希望將現有數據和新數據觸發到新表CustomerUpdate
中。
爲此,我創建了一個觸發器,但是這僅僅是拉動更新的數據,它不拉的現有數據..
CREATE TRIGGER trgAfterUpdate
ON [dbo].Customer
FOR UPDATE
AS
SET NOCOUNT ON
declare @NewCustomerName nchar(20);
declare @NewCustomerId nchar(20);
declare @OldCustomerName nchar(20);
declare @audit_action varchar(100);
select @NewCustomerName = i.CustomerName from inserted i;
select @NewCustomerId = i.CustomerId from inserted i;
select @OldCustomerName = c.CustomerName
from Customer c
where CustomerId = @NewCustomerId;
if update(CustomerName)
set @audit_action='Updated Record -- After Update Trigger.';
if update(CustomerId)
set @audit_action='Updated Record -- After Update Trigger.';
insert into CustomerUpdate(NewCustomerName, NewCustomerId, OldCustomername)
values(@NewCustomerName, @NewCustomerId, @OldCustomerName);
PRINT 'AFTER UPDATE Trigger fired.'
GO
請幫我
你的觸發器有一個** **主要每行中的缺陷,你認爲它會被稱爲** **一次 - 那是**不是**的情況。它會在每個語句**中觸發一次**,所以如果你的'UPDATE'語句影響了25行,你將觸發**觸發一次**,但是'Inserted'僞表將包含25行。您的代碼在這25行中選擇哪一個? '從插入的i中選擇@NewCustomerName = i.CustomerName;' - 它是非確定性的,你將得到**一個任意的行**,並且你將**忽略所有其他行**。你需要重寫你的觸發器來考慮這個問題! –
如果您想要「舊」值,您需要使用已刪除的虛擬表。當然,在審計表中,包含新值是多餘的。它將始終存在於下一個審計行或主表中。存儲兩個值需要兩倍的存儲空間。 –