clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2335687 fiddles created (27449 in the last week).

CREATE TABLE [MyTable] ( [GroupId] INT NOT NULL, [ItemId] INT NOT NULL, [Data] INT NOT NULL, PRIMARY KEY CLUSTERED ([GroupId], [ItemId]) );
 hidden batch(es)


CREATE TRIGGER [Trigger_MyTable_OnInsert] ON [MyTable] INSTEAD OF INSERT AS BEGIN INSERT INTO MyTable([GroupId], [ItemId], [Data]) SELECT [GroupId], ISNULL((SELECT MAX(MyTable.ItemId) FROM MyTable WHERE MyTable.GroupId = i.GroupId), 0) + 1, [Data] FROM inserted i END
 hidden batch(es)


INSERT dbo.MyTable (GroupId, [Data]) VALUES (1, 100); INSERT dbo.MyTable (GroupId, [Data]) VALUES (1, 110); INSERT dbo.MyTable (GroupId, [Data]) VALUES (1, 120); INSERT dbo.MyTable (GroupId, [Data]) VALUES (2, 210); INSERT dbo.MyTable (GroupId, [Data]) VALUES (2, 220);
10 rows affected
 hidden batch(es)


SELECT * FROM dbo.MyTable AS MT;
GroupId ItemId Data
1 1 100
1 2 110
1 3 120
2 1 210
2 2 220
 hidden batch(es)