SQL Server触发器插入和更新操作不起作用?

时间:2012-09-27 11:41:43

标签: sql-server-2008 triggers

我正在使用SQL Server 2008.我的触发器如下所示。我正在插入两个表中没有得到任何更新的值。

 CREATE TRIGGER trgAfterInserta ON AMS_33.dbo.access_event_logs
 AFTER INSERT,UPDATE 
 AS           
        declare @idams bigint;  
        declare @id int; 
        declare @uid nvarchar(20);
        declare @tentry datetime;
        declare @tsn nvarchar(20);
        declare @evtid nvarchar(20);
        declare @audit_action nvarchar(300);

select @idams   = i.id from inserted i;
select @id      =  max(Rownumber) + 1   from   TAV3.dbo.access_event_logs ;  
select @uid    =i.USERID from inserted i;   
select @tentry =i.TIMESTAMPS from inserted i;   
select @tsn   =i.TERMINALSN from inserted i;
select @evtid =i.EVENTID from inserted i;
set    @audit_action='Inserted Record -- After Insert.';
set    @tentry = dateadd(hh,3,@tentry); 


insert into TAV3.dbo.access_event_logs 
(Rownumber,Userid, Timeentry, Eventid, Terminalsn, Notes)
 values(@id,@uid, @tentry, @evtid, @tsn, @audit_action);

update AMS_33.dbo.access_event_logs set Calculated=1 where id= @idams;  

GO

表1 AMS_33.dbo.access_event_logs

  id    bigint  Unchecked
  USERID    nvarchar(50)    Unchecked
  DEPARTMENT    nvarchar(50)    Unchecked
  TIMESTAMPS    datetime    Unchecked
  EVENTID   nvarchar(50)    Unchecked
  TERMINALSN    nvarchar(50)    Unchecked
  ACCESSMETHOD  nvarchar(255)   Unchecked
  REMARKS   nvarchar(255)   Unchecked
  TERMINALIP    nvarchar(50)    Unchecked
  PHOTO image   Checked
  PHOTOSIZE int Unchecked
  RECLOGDTM datetime    Unchecked
  RECLOGFROMIP  nvarchar(50)    Unchecked
  LOCALTIMESTAMP    datetime    Unchecked
  PhotoPath nvarchar(50)    Checked
  Calculated    int Checked

表2 TAV3.dbo.access_event_logs

Rownumber   int Unchecked
Userid  nvarchar(20)    Unchecked
Timeentry   datetime    Unchecked
Eventid nvarchar(20)    Checked
Terminalsn  nvarchar(20)    Unchecked
Calculated  int Checked
Notes   nvarchar(300)   Checked

此处没有执行插入或更新?

1 个答案:

答案 0 :(得分:1)

您假设在语句中只插入或更新了一行 从插入的i中选择@idams = i.id;

这可能是错误的。那些声明将永远无法进入插入