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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:37:36+00:00 2026-05-20T01:37:36+00:00

I have a series of T-SQL queries that I need to run atomically. (See

  • 0

I have a series of T-SQL queries that I need to run atomically. (See Below)… The purpose is to allow 1 user to retrieve a single, unique row at a time and prevent other users from retrieving the same row simultaneously.

So far I have seen two possible solutions. 1) Table Hints (HOLDLOCK, TABLOCKX) and 2) Transaction Isolation Level (SERIALIZABLE)…

My Questions:

  1. Which option is better?

  2. Is there another/better solution?

DECLARE @recordId int;

SELECT @recordId = MIN([id])
FROM Exceptions
WHERE [status] = 'READY';

UPDATE Exceptions
SET [status] = 'PROCESSING',
    [username] = @Username
WHERE [id] = @recordId;

SELECT *
FROM Exceptions
WHERE [id] = @recordId;
  • 1 1 Answer
  • 1 View
  • 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-20T01:37:37+00:00Added an answer on May 20, 2026 at 1:37 am

    In this case,

    • HOLDLOCK = SERIALIZABLE = duration, concurrency
    • TABLOCKX = an exclusive table lock

    The 2 concepts are different and neither does what you want.

    To do what you want, to avoid race conditions, you need to force a non-blocking (READPAST) exclusive (UPDLOCK) row level (ROWLOCK) lock,. You can also use the OUTPUT clause to make it a single statement that will be atomic. This scales well.

    UPDATE
        E
    SET
       [status] = 'PROCESSING', [username] = @Username
    OUTPUT
       INSERTED.*
    FROM
       (
        SELECT TOP 1 id, [status], [username]
        FROM Exceptions (ROWLOCK, READPAST, UPDLOCK)
        WHERE [status] = 'READY'
        ORDER BY id
       ) E
    

    In general, locks have 3 aspects

    • Granularity = what is locked = row, page, table (PAGLOCK, ROWLOCK, TABLOCK)
    • Isolation Level = lock duration, concurrency (HOLDLOCK, READCOMMITTED, REPEATABLEREAD, SERIALIZABLE)
    • Mode = sharing/exclusivity (UPDLOCK, XLOCK)

    And

    • “combined” eg NOLOCK, TABLOCKX
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of data that need to be written into SQL, what
I have a series of T-SQL queries that I use that are running very
I have some script that is delivering a series of rows from an SQL
I have a series of calculation times in a DB2 SQL DB that are
I have a charting application that dynamically generates SQL Server queries to compute values
I have a series of T-SQL DELETE FROM TABLE and INSERT statements to run.
I have a series of update statements that I need to use in my
Specifically, Sql Server 2005/T-Sql. I have a field that is mostly a series of
I have a time series loaded into SQL Server, which i need to query
I have a series of questions about Keys, Indexes and Constraints in SQL, SQL

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.