Cannot use UPDATE with OUTPUT clause when a trigger is on the table

后端 未结 3 1571
生来不讨喜
生来不讨喜 2020-11-29 02:04

I\'m performing an UPDATE with OUTPUT query:

UPDATE BatchReports
SET IsProcessed = 1
OUTPUT inserted.BatchFileXml, inserted.Respons         


        
3条回答
  •  佛祖请我去吃肉
    2020-11-29 02:28

    Why put all needed columns into table variable? We just need primary key and we can read all data after the UPDATE. There is no race when you use transaction:

    DECLARE @t TABLE (ID INT PRIMARY KEY);
    
    BEGIN TRAN;
    
    UPDATE BatchReports SET 
        IsProcessed = 1
    OUTPUT inserted.ID INTO @t(ID)
    WHERE BatchReports.BatchReportGUID = @someGuid;
    
    SELECT b.* 
    FROM @t t JOIN BatchReports b ON t.ID = b.ID;
    
    COMMIT;
    

提交回复
热议问题