2012-01-17 50 views
1

我有兩個數據庫在不同的服務器上,我已經實現了複製以同步兩個數據庫之間的數據。數據更新複製時觸發器不觸發

我在訂閱數據庫上有一些觸發器,當通過複製進行任何更改時,這些觸發器不會觸發。

請爲此建議一個解決方案。

Create TRIGGER [dbo].[Ins_SLab] 
    ON [dbo].[Slab] 
    AFTER Insert 
AS 
BEGIN 
Declare @ProductId int 
set @ProductId =(select PurchaseProductID from Inserted) 
set @ProductId =(select ProductID from dbo.PurchaseProduct where ID = @ProductId) 
insert into tblTestTriger values('Hold Product Update',@ProductId) 
End 
Go 
+0

請發佈觸發器的一些代碼。 – 2012-01-17 10:02:23

回答

4

嗯,你有指定NOT FOR REPLICATION選項,這將是最明顯的原因。

但第二個最明顯的原因是觸發可能是射擊。但是,因爲您已經將觸發器寫入以假定它只適用於一行,它將僅適用於每批覆制行中的一行。

您需要的是將inserted作爲表格處理,然後重新編寫觸發器以保持所有基於集合的操作 - 如果您將變量分配給變量,那麼您做錯了。

insert into tblTestTriger --TODO: Explicit column list 
select 'Hold Product Update',pp.ProductId 
from inserted i 
    inner join 
    dbo.PurchaseProduct pp 
    on i.PurchaseProduct = pp.ID