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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:36:04+00:00 2026-05-27T07:36:04+00:00

This is a general question about how to lock range of values (and nothing

  • 0

This is a general question about how to lock range of values (and nothing else!) when they are not exists in table yet. The trigger for the question was that I want to do “insert if not exists”, I don’t want to use MERGE because I need to support SQL Server 2005.

In the first connection I:

  1. begin transaction
  2. select data from a table using (SERIALIZABLE, ROWLOCK) + where clause to respecify range
  3. wait…

In the second connection, I insert data to the table with values that do not match the where clause in the first connection

I would expect that the second connection won’t be affected by the first one, but it finishes only after I commit (or rollback) the first connection’s transaction.

What am I missing?

Here is my test code:

First create this table:

CREATE TABLE test
(
    VALUE nvarchar(100)
)

Second, open new query window sql server managements studio and execute the following:

BEGIN TRANSACTION;
SELECT *
FROM  test WITH (SERIALIZABLE,ROWLOCK)
WHERE value = N'a';

Third, open another new query window and execute the following:

INSERT INTO test VALUES (N'b');

Notice that the second query doesn’t ends until the transaction in the first window ends

  • 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-27T07:36:05+00:00Added an answer on May 27, 2026 at 7:36 am

    You are missing an index on VALUE.

    Without that SQL Server has nothing to take a key range lock on and will lock the whole table in order to lock the range.

    Even when the index is added however you will still encounter blocking with the scenario in your question. The RangeS-S lock doesn’t lock the specific range given in your query. Instead it locks the range between the keys either side of the selected range.

    When there are no such keys either side the range lock extends to infinity. You would need to add a value between a and b (for example aa) to prevent this happening in your test and the insert of b being blocked.

    See Bonus Appendix: Range Locks in this article for more about this.

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

Sidebar

Related Questions

Ok this is a general question about how to solve this issue, not to
This is not a theoretical or general question about when to use pass by
Yes, I know, yet another question about mutable objects. See this for general background
Yesterday I asked this general question about decimals and their internal precisions. Here is
This is a general question about MVC as a pattern, but in this case
This is a general question about character encoding when using MD5 libraries in various
This question is about 64-bit Java apps in general, although I came upon it
This is a general question. And may not be specific to datagrids. How can
This is a general question about writing web apps. I have an application that
This is a general design question about how to make a web application that

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.