We need an incremental column for each user. When a record is added we figure a trigger will fire after insert and generate the number. However each users will start with the number 100 and be able to increment to 101,102,103,etc. (See data) (see table and trigger below)
ID | UserId | aName | aNumber 1 | 1001 | ‘a’ | 100 2 | 1001 | ‘a’ | 101 3 | 1002 | ‘b’ | 100 4 | 1002 | ‘b’ | 101 5 | 1001 | ‘a’ | 102 6 | 1001 | ‘a’ | 103 7 | 1002 | ‘b’ | 102
Here is the table: CREATE TABLE [dbo].[TableA]( [ID] [int] IDENTITY(1,1) NOT NULL, [UserId] [int] NOT NULL, [aName] [nvarchar](500) NOT NULL, [aNumber] [nvarchar](50) NULL, CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] ) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [UIX_TableA_aNumber] ON [dbo].[TableA]([aNumber],[UserId]) ON [PRIMARY] GO
CREATE TRIGGER [t_TableA] ON [dbo].[TableA] FOR INSERT AS UPDATE [TableA] SET [TableA].aNumber = inserted.ID FROM [TableA],inserted WHERE inserted.ID =[TableA].ID GO
|