Avatar of MICHAEL REESE
MICHAEL REESE
Flag for United States of America

asked on 

Need help with Insert Trigger on inserted detail record using Max() - Thanks

Hello,


I'm trying to increase the rowno column in the detail file for each detail inserted.

Increase the RowNo column in the detail table (on insert) based on the ParentID and the MAX row for the related detail records using a trigger, however, this does not appear to be working.  All RowNo for the related details are updating when the trigger fires.  

Trigger

USE [TR_DB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [Citations].[trg_LogDetail_NewRowNo]
ON Citations.LogDetails
AFTER INSERT
AS
BEGIN  
    SET NOCOUNT ON;
    UPDATE Citations.LogDetails
    SET RowNO = (SELECT MAX(rowno) FROM Citations.LogDetails WHERE ParentLogNo = i.ParentLogNo) +1
      FROM Citations.LogDetails S INNER JOIN Inserted I
      ON S.ParentLogNo = I.ParentLogNo
      WHERE I.ParentLogNo = I.ParentLogNo
END
GO
* triggerMicrosoft SQL ServerGolang

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon