Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:21:36+00:00 2026-06-04T19:21:36+00:00

I use SQL Server 2008 R2. I’m looking for a feature that I describe

  • 0

I use SQL Server 2008 R2.

I’m looking for a feature that I describe as dependent identity.

I’ll explain by an example.

consider a table like this one:

script

CREATE TABLE [dbo].[Rooms](
    [RoomID] [int] NOT NULL,
    [ItemID] [int] NOT NULL,
    [ItemDescription] [nvarchar] (250))
GO

data:

RoomID  ItemID  ItemDescription
------  ------  --------------- 
7       1       Door
7       2       Window (West)
7       3       Window (North)
8       1       Door
8       2       Table #1
8       3       Table #2
7       4       Table #1 
8       4       Chair #1 
7       5       Table #2 
7       6       Table #3 
8       5       Chair #2

(can anyone tell the secret how to format an example table here?)

I would have love to be able to declare a dependent identity column like this:

ItemID [int] Identity(RoomID,1,1) NOT NULL

A new row in [Rooms] should triggers a test for the max value of ItemID where RoomID = @roomID and add 1.

Instead of update with a change in RoomID use delete and insert the required data.

Nowadays I do that programmatically like this:

DECLARE @roomID INT
SET @roomID = 7
INSERT INTO [Allocation].[dbo].[Rooms]
    ([RoomID], [ItemID], [ItemDescription]) VALUES (@roomID,
    (SELECT max([ItemID])+1 FROM [Allocation].[dbo].[Rooms] WHERE [RoomID]=@roomID)
    ,'Chair #1')
GO

So, Is there such a feature?

In the probable case there is none, could I program the server to set next dependent identity for me automatically, given a specific table, parent column and dependent identity column?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:21:39+00:00Added an answer on June 4, 2026 at 7:21 pm

    You can use a trigger, and an index to improve performance and ensure there are no duplicates.

    Change your table to have a primary key, and allow null for ItemID

    CREATE TABLE [dbo].[Rooms](
        [RoomID] [int] NOT NULL,
        [ItemID] [int] NULL,
        [ItemDescription] [nvarchar](250) NULL,
        [Id] [int] IDENTITY(1,1) NOT NULL,
        CONSTRAINT [PK_Rooms] PRIMARY KEY CLUSTERED 
        (
            [Id] ASC
        )
    )
    

    and then add a trigger

    CREATE TRIGGER RoomTrigger
       ON  Rooms
       AFTER INSERT
    AS 
    BEGIN
        SET NOCOUNT ON;
    
        update Rooms
        set 
            ItemID = (select coalesce(MAX(itemid), 0) + 1 
                          from Rooms r where r.RoomID = inserted.RoomID )
        from
            inserted where Rooms.Id = inserted.Id
    
    END
    

    Then you can do this

    insert into Rooms (RoomID, ItemDescription) values (1, 'Test')
    insert into Rooms (RoomID, ItemDescription) values (1, 'Test')
    

    which results in

    RoomID  ItemID ItemDescription Id
    2       0      Test            1
    2       1      Test            2
    

    As suggested by marc_s I’ve used SQL Query Stress with 10 threads to see what happens with this trigger under load. I didn’t get any duplicates at all (using the default isolation level), but I did get loads of deadlocks as I would have expected.

    Using the original query from the question I get a lot of duplicates.

    Using the trigger approach I get deadlocks and results like this:

    RoomID ItemID ItemDescription Id
    1      6      Test            6
    1      7      Test            9
    1      8      Test            902
    1      9      Test            903
    

    Here ItemID is contiguous, but about 900 out of 1000 rows failed to be inserted leaving large gaps in Id.

    If we add the following index:

    CREATE UNIQUE NONCLUSTERED INDEX [IX_Rooms] ON [dbo].[Rooms] 
    (
        [RoomID] ASC,
        [ItemID] ASC
    )
    

    in order to guarantee no duplicates, and improve the performance of calculating Max(ItemId) for a particular RoomID, then now:

    • the original query from the question causes duplicates and only manages to insert 500 rows.
    • the trigger version using the default isolation level succeeds without any deadlocks or errors and runs very fast.

    Using the trigger with isolation level = serializable brings back deadlocks so only 40% of the inserts succeed (but no exceptions due to duplicates).

    As a final test tried with trigger + 50 threads + isolation level = default. No errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use SQL Server 2008. I have a database (Database1) that has one table
I use SQL Server 2008 and have a table with 5 char typed columns.
I have a view in SQL Server 2008 that I want to use for
I'm trying to use a dynamically generated fully-qualified table name in sql server 2008.
I use SQL Server 2005. I have a simple logging table that my web
I use SQL Server 2008, C#, I have a table which contains about 20000
We use SQL server 2008 as our RDBMS and we have a database that
I'm converting an app to use SQL Server 2008 that is currently using SQLite.
I use SQL Server 2008 R2 and I'm looking for a good reporting tool.
I got a multi-use SQL table in my SQL Server 2008 database. It can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.