Solved

SQL Identity Problem

Posted on 2007-03-19
3
253 Views
Last Modified: 2008-03-06
Is there a way to setup a SQL database to automatically reset an identity column based on the primary key?

Example:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[IdentityTest]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[IdentityTest]
GO

CREATE TABLE [dbo].[IdentityTest] (
      [RecordID] [int] NOT NULL ,
      [IdentityID] [int] IDENTITY (1, 1) NOT NULL ,
      [Data] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[IdentityTest] WITH NOCHECK ADD
      CONSTRAINT [PK_IdentityTest] PRIMARY KEY  CLUSTERED
      (
            [RecordID],
            [IdentityID]
      )  ON [PRIMARY]
GO


INSERT INTO IdentityTest (RecordID, Data) VALUES (1, 'DATA 1-1')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (1, 'DATA 1-2')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (2, 'DATA 2-1')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (2, 'DATA 2-2')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (2, 'DATA 2-3')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (3, 'DATA 3-1')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (4, 'DATA 4-1')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (4, 'DATA 4-2')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (4, 'DATA 4-3')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (4, 'DATA 4-4')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (5, 'DATA 5-1')
GO
SELECT * FROM IdentityTest
GO

Actual Results:

RecordID    IdentityID  Data
----------- ----------- -------------------------
1              1              DATA 1-1
1              2              DATA 1-2
2              3              DATA 2-1
2              4              DATA 2-2
2              5              DATA 2-3
3              6              DATA 3-1
4              7              DATA 4-1
4              8              DATA 4-2
4              9              DATA 4-3
4              10            DATA 4-4
5              11            DATA 5-1



Desired Results:

RecordID    IdentityID  Data
----------- ----------- -------------------------
1              1              DATA 1-1
1              2              DATA 1-2
2              1              DATA 2-1
2              2              DATA 2-2
2              3              DATA 2-3
3              1              DATA 3-1
4              1              DATA 4-1
4              2              DATA 4-2
4              3              DATA 4-3
4              4              DATA 4-4
5              1              DATA 5-1
0
Comment
Question by:jeshbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 18751139
no  the identity feature doesn't do that ...

you probably  want to implement the sequence numbering via triggers...

but how do you determine order?
by sorting the data column?

 
0
 
LVL 3

Author Comment

by:jeshbr
ID: 18751161
This is just an example of what I would like to do.  
The 'Data' column means nothing.
0
 
LVL 3

Author Comment

by:jeshbr
ID: 18751670
I was able to achive my desired result using a trigger.

Code:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[_InsertTrigger_IdentityTest]') and OBJECTPROPERTY(id, N'IsTrigger') = 1)
drop trigger [dbo].[_InsertTrigger_IdentityTest]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[IdentityTest]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[IdentityTest]
GO

CREATE TABLE [dbo].[IdentityTest] (
      [RecordID] [int] NOT NULL ,
      [IdentityID] [int] NOT NULL ,
      [Data] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[IdentityTest] WITH NOCHECK ADD
      CONSTRAINT [PK_IdentityTest] PRIMARY KEY  CLUSTERED
      (
            [RecordID],
            [IdentityID]
      )  ON [PRIMARY]
GO

ALTER TABLE [dbo].[IdentityTest] ADD
      CONSTRAINT [DF_IdentityTest_IdentityID] DEFAULT ((0)) FOR [IdentityID]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE TRIGGER _InsertTrigger_IdentityTest ON IdentityTest FOR INSERT AS
BEGIN
IF  ( SELECT [IdentityID] FROM INSERTED) = 0
    BEGIN
        UPDATE IT SET IdentityID = (LastIdentityID.IdentityID + 1)
        FROM IdentityTest IT
            INNER JOIN INSERTED I
                  ON (I.RecordID = IT.RecordID)
                  AND (I.IdentityID = IT.IdentityID)
            INNER JOIN (
                        SELECT ITTemp.RecordID AS RecordID,
                              MAX(ITTemp.IdentityID) AS IdentityID
                        FROM IdentityTest ITTemp
                        GROUP BY ITTemp.RecordID
                  ) LastIdentityID ON (LastIdentityID.RecordID = IT.RecordID)
    END
END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

INSERT INTO IdentityTest (RecordID, Data) VALUES (1, 'DATA 1-1')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (1, 'DATA 1-2')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (2, 'DATA 2-1')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (2, 'DATA 2-2')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (2, 'DATA 2-3')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (3, 'DATA 3-1')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (4, 'DATA 4-1')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (4, 'DATA 4-2')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (4, 'DATA 4-3')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (4, 'DATA 4-4')
GO
INSERT INTO IdentityTest (RecordID, Data) VALUES (5, 'DATA 5-1')
GO
SELECT * FROM IdentityTest
GO
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question