2011-03-08 48 views
0

我正在嘗試創建一個觸發器,它將用收據信息更新歷史記錄表。加入觸發器?

我的源表有一個SourceID和一個ReceiptID。

所以當我更新收據表時,我想將該記錄複製到歷史表,但我首先需要獲取正在更新的源的SourceID。

這是我現在有:

ALTER TRIGGER [dbo].[trg_ReceiptHistory] 
ON [dbo].[tblReceipt] 
for UPDATE 
AS 
begin try 
INSERT INTO tblHistorySource(RecShipName, RecShipAddress, sourceID) 
select t1.shippingName, t1.shippingAddress, t2.sourceID 
from [DELETED] t1, tblSource t2 
where t2.receiptID = t1.receiptID 
end try 
begin catch 
SELECT 
ERROR_NUMBER() AS ErrorNumber, 
ERROR_SEVERITY() AS ErrorSeverity, 
ERROR_STATE() AS ErrorState, 
ERROR_PROCEDURE() AS ErrorProcedure, 
ERROR_LINE() AS ErrorLine, 
ERROR_MESSAGE() AS ErrorMessage; 
RAISERROR('Error in Source Hisotry Trigger' ,16,1) 
ROLLBACK TRAN 
END CATCH 

當我嘗試做一個簡單的更新到tblReceipt,我得到以下錯誤:

Msg 50000, Level 16, State 1, Procedure trg_ReceiptHistory, Line 24 
Error in Source Hisotry Trigger 
Msg 3609, Level 16, State 1, Line 1 
The transaction ended in the trigger. The batch has been aborted. 

我是不是做錯了?請幫助:(

+0

什麼是有關發送你進入'catch'塊錯誤的詳細信息在第一個地方? – 2011-03-08 20:56:13

回答

2

首先要做的是去掉CATCH,看到錯誤,我們可以從那裏。

ALTER TRIGGER [dbo].[trg_ReceiptHistory] 
ON [dbo].[tblReceipt] 
for UPDATE 
AS 
--begin try 
INSERT INTO tblHistorySource(RecShipName, RecShipAddress, sourceID) 
select t1.shippingName, t1.shippingAddress, t2.sourceID 
from [DELETED] t1, tblSource t2 
where t2.receiptID = t1.receiptID 
/*end try 
begin catch 
SELECT 
ERROR_NUMBER() AS ErrorNumber, 
ERROR_SEVERITY() AS ErrorSeverity, 
ERROR_STATE() AS ErrorState, 
ERROR_PROCEDURE() AS ErrorProcedure, 
ERROR_LINE() AS ErrorLine, 
ERROR_MESSAGE() AS ErrorMessage; 
RAISERROR('Error in Source Hisotry Trigger' ,16,1) 
ROLLBACK TRAN 
END CATCH*/ 
+0

謝謝!我看到錯誤,一切正在工作:) – 2011-03-08 21:05:49

+0

什麼是錯誤? – alldayremix 2013-05-18 19:26:20