触发存储错误信息

时间:2014-11-21 12:12:46

标签: sql-server-2008 triggers

以下是我在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。任何人都可以帮我解决这个问题

由于

1 个答案:

答案 0 :(得分:3)

基本上,您需要重新设计触发器才能处理Inserted(和Deleted)伪表中的多行 - 您的触发器只被调用一次,即使UPDATE语句影响10,20,50行。

基本上,你的代码需要看起来像这样:

ALTER TRIGGER [dbo].[Trigger_UpdateLeadMasters] 
ON [dbo].[LeadMasters]
FOR UPDATE
AS
    INSERT INTO dbo.LoggerLeadMasters(CompanyName, PersonName, Designation, Number, Number2, 
                                      EmailAddress, Address, Address2, CityName, State, 
                                      PinNumber, Product, RemarkNote, 
                                      Audit_Action, Audit_Timestamp)
       SELECT
          CompanyName, PersonName, Designation, Number, Number2, 
          EmailAddress, Address, Address2, CityName, State, 
          PinNumber, Product, RemarkNote, 
          '?????', GETDATE()
       FROM
          Inserted

现在最大的挑战是提出Audit_Action,因为您现在可能会插入多行,每行都可能有不同的列已更新....

通常情况下,您只能将其转换为单个字符串 - 通常用于存储感兴趣的旧列和新列 - 因此您始终可以获得稍后可用的更新信息。 / p>

另外:SQL Server 2008 和更新版本已经内置了很多此功能 - 它被称为更改数据捕获,这可能会为您节省费用从必须写几十个触发器 - 检查出来!