我从table1触发器中调用了更新查询,当该查询运行时,它成功更新了其他table2,但是仅触发了table2的更新触发器1次,这意味着不是针对从update查询中更新的每条记录
我正在使用SQL Server 2008 Express版-
请指导我哪里错了
答案 0 :(得分:1)
最后得到anwser:/,触发一次触发以执行单个查询。批量更新或批量更新/插入/删除不会唤醒每一行的触发条件:(,我非常感谢Markov先生和David Dubois先生,他对发帖有兴趣帮助我:)亲爱的,
答案 1 :(得分:0)
这是使用更新查询触发并更新另一个表的触发器,但未触发除已更新的第二个表的第一条记录以外的所有记录的更新触发器。
ALTER TRIGGER [dbo].[tbl_Inv_PurchaseNoteDetailUpdate]
ON [dbo].[tbl_Inv_PurchaseNoteDetail]
AFTER Update
AS
BEGIN
SET NOCOUNT ON;
Declare @RefNo as varchar(15) Declare @rate as float
Declare @NewNetValue as float Declare @OldNetValue as float
select
@RefNo=ReferenceNo,
@rate=Rate
from inserted
select
@NewNetValue=isnull(i.NetAmount,0), @OldNetValue=isnull(d.NetAmount,0)
from inserted i inner join deleted d on i.id = d.id
--if amount changes then rate should be updated to another table
if @NewNetValue<>@OldNetValue
begin
---this will update new rate in table[tbl_Inv_Ledger]
--but didnot fire update trigger of table[tbl_Inv_Ledger] for all record which was updated
update dbo.tbl_Inv_Ledger
set Rate=Rate
where ReferenceNo=@RefNo
---update trigger of table[tbl_Inv_Ledger] is fired only for first record
end
end
答案 2 :(得分:0)
ALTER TRIGGER [dbo].[tbl_Inv_PurchaseNoteDetailUpdate]
ON [dbo].[tbl_Inv_PurchaseNoteDetail]
AFTER Update
AS
BEGIN
SET NOCOUNT ON;
Declare @RefNo as varchar(15) Declare @rate as float
Declare @NewNetValue as float Declare @OldNetValue as float
select
@RefNo=ReferenceNo,
@rate=Rate
from inserted
select
@NewNetValue=isnull(i.NetAmount,0), @OldNetValue=isnull(d.NetAmount,0)
from inserted i inner join deleted d on i.id = d.id
--if amount changes then rate should be updated to another table
if @NewNetValue<>@OldNetValue
begin
-- using cursor work fine, update trigger of table[tbl_Inv_Ledger] is fired for every record
Declare @Inv_LedgerID as numeric
DECLARE PurchaseNoteDetail_Cursor CURSOR FOR
Select ID from dbo.tbl_Inv_Ledger where ReferenceNo=@ReferenceNo
and FK_tbl_Inv_PurchaseNoteDetail_ID is null
and FK_tbl_Inv_PurchaseReturnNoteDetail_ID is null -- also check purchase return entry should not be distrubed
OPEN PurchaseNoteDetail_Cursor
FETCH NEXT FROM PurchaseNoteDetail_Cursor INTO @Inv_LedgerID
WHILE @@FETCH_STATUS = 0
BEGIN
update dbo.tbl_Inv_Ledger
set Rate=@Rate
where ID=@Inv_LedgerID
FETCH NEXT FROM PurchaseNoteDetail_Cursor INTO @Inv_LedgerID
END
CLOSE PurchaseNoteDetail_Cursor
DEALLOCATE PurchaseNoteDetail_Cursor
end
-----------------xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-----------------
END