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 6550613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:14:12+00:00 2026-05-25T12:14:12+00:00

SQL Server 2008 R2 (Data Center edition – I think) I have a very

  • 0

SQL Server 2008 R2 (Data Center edition – I think)

I have a very specific requirement for the database.

I need to insert a row marked with timestamp [ChangeTimeStamp]. Timestamp value is passed as a parameter. Timestamp has to be unique.

Two processes can insert values at the same time, and I happen to run into duplicate key insertion once in a while. To avoid this, I am trying:

declare @maxChangeStamp bigint

set transaction isolation level read committed
begin transaction 
select @maxChangeStamp = MAX(MaxChangeTimeStamp) from TSMChangeTimeStamp

if (@maxChangeStamp > @changeTimeStamp)
    set @maxChangeStamp = @maxChangeStamp + 1   
else 
    set @maxChangeStamp = @changeTimeStamp

update TSMChangeTimeStamp 
set MaxChangeTimeStamp = @maxChangeStamp 
commit

set @changeTimeStamp = @maxChangeStamp

insert statment
  • REPEATABLE READ – causes deadlock
  • READ COMMITTED – causes duplicate key inserts

@changeTimeStamp is my parameter.
TSMChangeTimeStamp holds only one value.

If anyone has a good idea how to solve this I will appreciate any help.

  • 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-05-25T12:14:13+00:00Added an answer on May 25, 2026 at 12:14 pm

    Inside the transaction, the SELECT statement will acquire a shared lock if the mode is not READ COMMITTED or snapshot isolation. If two processes both start the SELECT at the same time, they will both acquire a shared lock.

    Later, the UPDATE statement attempts to acquire an exclusive lock (or update lock). Unfortunately, neither one can acquire an exclusive lock, because the other process has a shared lock.

    Try using the WITH (UPDLOCK) table hint on the SELECT statement. From MSDN:

    UPDLOCK

    Specifies that update locks are to be taken and held until the
    transaction completes. UPDLOCK takes update locks for read operations
    only at the row-level or page-level. If UPDLOCK is combined with
    TABLOCK, or a table-level lock is taken for some other reason, an
    exclusive (X) lock will be taken instead.

    When UPDLOCK is specified, the READCOMMITTED and READCOMMITTEDLOCK
    isolation level hints are ignored. For example, if the isolation level
    of the session is set to SERIALIZABLE and a query specifies (UPDLOCK,
    READCOMMITTED), the READCOMMITTED hint is ignored and the transaction
    is run using the SERIALIZABLE isolation level.

    For example:

    begin transaction
    select @maxChangeStamp = MAX(MaxChangeTimeStamp) from TSMChangeTimeStamp with (updlock) 
    

    Note that update locks may be promoted to a table lock if there is no index for your table (Microsoft KB article 179362).

    Explicitly requesting an XLOCK may also work.

    Also note your UPDATE statement does not have a WHERE clause. This causes the UPDATE to lock and update every record in the table (if applicable in your case).

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

Sidebar

Related Questions

I need to localize data in tables in a SQL Server 2008 database. Each
I have a SQL Server 2008 GEOGRAPHY data type in my database containing a
I have a process that is inserting data into a database (SQL Server 2008)
I have an MS SQL Server 2008 Database, from which I am fetching data
Does SQL Server 2008 have a a data-type like MySQL's enum ?
I have a Microsoft SQL Server 2008 query that returns data from three tables
I need to transfer data daily from SQL Server (2008) to SQL Server (2005).
I am using SQL Server 2008 and I need to select all data from
For primary keys on a large SQL Server 2008 data table, I have the
How to backup SQL Server 2008 database - only the objects, without data?

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.