以下是我的SQL Server 2008觸發存儲錯誤信息
ALTER TRIGGER [dbo].[Trigger_UpdateLeadMasters] ON [dbo].[LeadMasters]
FOR UPDATE
AS
DECLARE @CompanyName nvarchar(50);
DECLARE @PersonName nvarchar(50);
DECLARE @Designation nvarchar(50);
DECLARE @Number nvarchar(50);
DECLARE @Number2 nvarchar(50);
DECLARE @Emailaddress nvarchar(50);
DECLARE @Address nvarchar(MAX);
DECLARE @Address2 nvarchar(MAX);
DECLARE @CityName nvarchar(50);
DECLARE @State nvarchar(50);
DECLARE @PinNumber nvarchar(50);
DECLARE @Product nvarchar(50);
DECLARE @RemarkNote nvarchar(MAX);
DECLARE @Audit_Action varchar(100);
select @CompanyName=i.CompanyName from inserted i;
select @PersonName=i.PersonName from inserted i;
select @Designation=i.Designation from inserted i;
select @Number=i.Number from inserted i;
select @Number2=i.Number2 from inserted i;
select @EmailAddress=i.EmailAddress from inserted i;
select @Address=i.Address from inserted i;
select @Address2=i.Address2 from inserted i;
select @CityName=i.CityName from inserted i;
select @State=i.State from inserted i;
select @PinNumber=i.PinNumber from inserted i;
select @Product=i.Product from inserted i;
select @RemarkNote=i.RemarkNote from inserted i;
IF UPDATE(CompanyName)
set @Audit_Action=' Name Modified or Updated'
IF UPDATE(PersonName)
set @Audit_Action='Person Name Modified or Updated'
IF UPDATE(Designation)
set @Audit_Action='Designation Modified or Updated'
IF UPDATE(Number)
set @Audit_Action='1st Phone Number Modified or Updated'
IF UPDATE(Number2)
set @Audit_Action='2nd Phone Number Modified or Updated'
IF UPDATE(EmailAddress)
set @Audit_Action='Email Address Modified or Updated'
IF UPDATE(Address)
set @Audit_Action='Address Modified or Updated'
IF UPDATE(Address2)
set @Audit_Action='Alternate Address Modified or Updated'
IF UPDATE(CityName)
set @Audit_Action='City Name Modified or Updated'
IF UPDATE(State)
set @Audit_Action='State Modified or Updated'
IF UPDATE(PinNumber)
set @Audit_Action='PinNumber Modified or Updated'
IF UPDATE(Product)
set @Audit_Action='Product Field Modified or Updated'
IF UPDATE(RemarkNote)
set @Audit_Action='Remark Note Modified or Updated'
insert into LoggerLeadMasters(CompanyName,PersonName,Designation,Number,Number2,EmailAddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote,Audit_Action,Audit_Timestamp)
Values(@CompanyName,@PersonName,@Designation,@Number,@Number2,@EmailAddress,@Address,@Address2,@CityName,@State,@PinNumber,@Product,@RemarkNote,@Audit_Action,GETDATE())
上觸發但問題是,每當我修改LeadMasters表任何領域,觸發轉到行
IF UPDATE(RemarkNote)
set @Audit_Action='Remark Note Modified or Updated'
並存儲錯誤信息LoggerLeadMasters.Can任何人可以幫助我解決這個問題
謝謝
你的觸發器有** MAJOR **缺陷,你似乎認爲它會被稱爲**每行**一次 - 這是**不是**的情況。觸發器將在每個語句**中觸發一次**,所以如果你的UPDATE語句影響了25行,你將觸發**觸發**一次,但是,然後「插入」和「刪除」將分別包含25行。您的代碼在這25行中選擇哪一個?從插入的i中選擇@ CompanyName = i.CompanyName;' - 它是非確定性的 - 將挑選一個,所有其他人將被忽略。你需要重寫你的觸發器來考慮這個問題! – 2014-11-21 12:15:13
感謝@marc_s你可以建議更改我的代碼嗎? – user2614235 2014-11-21 12:23:51
除了marc_s提到的set操作的問題之外,如果多於一個字段被更新,例如預期的Audit_Action是什麼,城市名稱和地址和...? – bummi 2014-11-21 12:53:41