Home All Groups Group Topic Archive Search About

IDENTITY based on a second value

Author
26 Feb 2007 3:21 PM
Senna
Hi

Using SqlServer 2005.

At a INSERT statement I want CustomId to increase with 1 depending on what
the AppId are. CustomId must be unique per AppId.

Any ideas on how to solve this?

CREATE TABLE [Invoice] (
    [Id] [int] IDENTITY (1, 1) NOT FOR REPLICATION  NOT NULL ,
    [AppId] [int] NOT NULL ,
    [CustomId] [int] NOT NULL ,
    CONSTRAINT [PK_Invoice] PRIMARY KEY  CLUSTERED
    (
        [Id]
    )  ON [PRIMARY]
) ON [PRIMARY]
GO

--Fill with temp data
INSERT INTO [dbo].[Invoice]([AppId], [CustomId]) VALUES(1,1)
INSERT INTO [dbo].[Invoice]([AppId], [CustomId]) VALUES(1,2)
INSERT INTO [dbo].[Invoice]([AppId], [CustomId]) VALUES(2,1)
INSERT INTO [dbo].[Invoice]([AppId], [CustomId]) VALUES(1,3)
INSERT INTO [dbo].[Invoice]([AppId], [CustomId]) VALUES(1,4)
INSERT INTO [dbo].[Invoice]([AppId], [CustomId]) VALUES(1,5)
INSERT INTO [dbo].[Invoice]([AppId], [CustomId]) VALUES(2,2)
INSERT INTO [dbo].[Invoice]([AppId], [CustomId]) VALUES(1,6)

--View temp data
SELECT * FROM dbo.Invoice

AddThis Social Bookmark Button