Sql update query doesn't fire update trigger for all records

半城伤御伤魂 提交于 2019-12-23 06:14:56

问题


I called update query from table1 trigger, when that query runs, it updates other table2 successfully but update trigger of table2 is fired only 1 time means not for every record that was updated from query of update

I'm using SQL Server 2008 Express edition-

Please guide me where I am wrong


回答1:


Finally got the anwser :/, Trigger fire once for single query execution. mass or bulk update/insert/delete will not awake trigger for every row :(, i really appreciate Mr. Markov & Mr. David Dubois, who showed interest in post to help me:) thanks dear,




回答2:


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



回答3:


This is the trigger that fired & Update another Table with update query, but not fired update trigger for all records except first record of second table which was updated.

    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


来源:https://stackoverflow.com/questions/52558871/sql-update-query-doesnt-fire-update-trigger-for-all-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!