Запрос обновления SQL не запускает триггер обновления для всех записей - PullRequest
0 голосов
/ 28 сентября 2018

Я вызвал запрос на обновление из триггера таблицы1, когда этот запрос выполняется, он успешно обновляет другую таблицу2, но триггер обновления таблицы2 срабатывает только 1 раз, значит, не для каждой записи, которая была обновлена ​​из запроса на обновление

Iиспользую SQL Server 2008 Express edition-

Пожалуйста, укажите мне, где я не прав

Ответы [ 3 ]

0 голосов
/ 29 сентября 2018
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
0 голосов
/ 29 сентября 2018

Наконец получил anwser: /, триггер запускается один раз для выполнения одного запроса.Массовое или массовое обновление / вставка / удаление не вызовет срабатывания триггера для каждой строки :( Я очень ценю г-на Маркова и г-на Дэвида Дюбуа, которые проявили интерес к сообщению, чтобы помочь мне :) спасибо, дорогой,

0 голосов
/ 29 сентября 2018

Это триггер, который запустил и обновил другую таблицу с помощью запроса на обновление, но не запустил триггер обновления для всех записей, кроме первой записи второй таблицы, которая была обновлена.

    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
...