2012-09-27 131 views
1

我使用SQL服務器如下所示2008年我的觸發。而我插入的價值,我沒有得到任何更新在兩個表中。SQL Server觸發器插入和更新操作不起作用?

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 

這裏沒有插入或更新被performin G ?

+1

1)假定你是唯一一個行已被插入或更新,這可能是假的。 – automatic

+0

如果我正確讀取您的代碼,您將通過此創建一個循環?!你有一個觸發器上的更新,你的觸發器再次更新這個表。你有任何錯誤? – YvesR

回答

1

我們假定你是唯一一個行已被插入或更新,像 選擇@idams = i.id語句插入從我;

這可能是假的。然後,這些語句將永遠無法獲得的插入

+0

我該如何克服這一點。 – user1703145

+0

而不是使用插入的值條款使用一個選擇,即 插入(fielda,fieldb)選擇inserted.foo作爲fielda,inserted.bar作爲插入的fieldb – automatic