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

  • SEARCH
  • Home
  • 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 184973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:23:16+00:00 2026-05-11T15:23:16+00:00

I have to insert some records in a table in a legacy database and,

  • 0

I have to insert some records in a table in a legacy database and, since it’s used by other ancient systems, changing the table is not a solution.

The problem is that the target table has a int primary key but no identity specification. So I have to find the next available ID and use that:

select @id=ISNULL(max(recid)+1,1) from subscriber 

However, I want to prevent other applications from inserting into the table when I’m doing this so that we don’t have any problems. I tried this:

begin transaction     declare @id as int     select @id=ISNULL(max(recid)+1,1) from subscriber WITH (HOLDLOCK, TABLOCK)     select @id     WAITFOR DELAY '00:00:01'     insert into subscriber (recid) values (@id) commit transaction select * from subscriber 

in two different windows in SQL Management Studio and the one transaction is always killed as a deadlock victim.

I also tried SET TRANSACTION ISOLATION LEVEL SERIALIZABLE first with the same result…

Any good suggestions to how I can ensure that I get the next id and use that without risking that someone else (or me!) is getting hosed?

Sorry for not mentioning this earlier, but this is a SQL 2000 server so I can’t use things like FOR UPDATE and OUTPUT

UPDATE: This is the solution that worked for me:

BEGIN TRANSACTION     DECLARE @id int      SELECT  @id=recid     FROM    identities WITH (UPDLOCK, ROWLOCK)     WHERE table_name = 'subscriber'      waitfor delay '00:00:06'      INSERT INTO subscriber (recid) values (@id)      UPDATE identities SET recid=recid+1      WHERE table_name = 'subscriber'  COMMIT transaction  select * from subscriber 

The WAITFOR is so that I can have multiple connections and start the query several times to provoke concurrency.

Thanks to Quassnoi for the answer and to all you other guys that contributed! Awesome!

  • 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. 2026-05-11T15:23:17+00:00Added an answer on May 11, 2026 at 3:23 pm

    Create another table:

    t_identity (id INT NOT NULL PRIMARY KEY CHECK (id = 1), value INT NOT NULL) 

    with a single row, lock this row, and increment value by one each time you need an IDENTITY.

    To lock, increment, and return the new value in a single statement, use:

    UPDATE  t_identity SET     value = value + 1 OUTPUT  INSERTED.value 

    If you don’t want to update, just lock, then issue:

    SELECT  value FROM    t_identity WITH (UPDLOCK, ROWLOCK) 

    This will lock the table until the end of the transaction.

    If you always first lock t_identity before messing with ancient_table, you will never get a deadlock.

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

Sidebar

Related Questions

I have problem when I try insert some data to Informix TEXT column via
I have client application that uses WCF service to insert some data to backend
I have to read data from some files and insert the data into different
(Database: Oracle 10G R2) It takes 1 minute to insert 100,000 records into a
i have a table called 'studentdetails'. i dont want to insert the record one
I have a table with 100 K records. I am writing an XML file
I have a script that appends some rows to a table. One of the
I have two insert statements, almost exactly the same, which run in two different
I have a huge file, where I have to insert certain characters at a
Using POSIX threads & C++, I have an Insert operation which can only be

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.