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

Ask A Question

Stats

  • Questions 99k
  • Answers 99k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer At least in the title or in the text, it… May 11, 2026 at 7:41 pm
  • Editorial Team
    Editorial Team added an answer You need separate application pools no matter what extension you… May 11, 2026 at 7:41 pm
  • Editorial Team
    Editorial Team added an answer Yes, you can. Here is an example: Mac/iPhone: Show Available… May 11, 2026 at 7:41 pm

Related Questions

I'm working on a stored procedure in SQL Server 2000 with a temp table
Trying to duplicate some rows in a table but just change the ssreportid column
(Database: Oracle 10G R2) It takes 1 minute to insert 100,000 records into a
Background I am working on a legacy small-business automation system (inventory, sales, procurement, etc.)
I am having to use triggers in MSSQL for the first time, well triggers

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.