troubleshooting Question

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

Avatar of MICHAEL REESE
MICHAEL REESEFlag for United States of America asked on
Microsoft SQL ServerGolang* trigger
18 Comments1 Solution148 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 18 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 18 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros