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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:46:32+00:00 2026-05-15T04:46:32+00:00

I’m writing a SQL Server stored procedure in which I want to lock a

  • 0

I’m writing a SQL Server stored procedure in which I want to lock a table for update before executing the body of the stored procedure. I don’t want to prevent other processes from reading the table, but I do want to prevent other processes updating the table.

Here is my first attempt:

CREATE PROCEDURE someProcedure
BEGIN
   SET TRANSACTION ISOLATION LEVEL READ COMITTED
   BEGIN TRANSANCTION
     SELECT COUNT(*) FROM TheTable WITH (UPDLOCK, TABLOCK)

     -- Pause procedure so that we can view the locks with sp_lock
     WAITFOR DELAY '00:15'

     -- Do stuff
   COMMIT
END

When I execute the stored procedure, and invoke sp_lock, I see that the table is indeed locked. However, it’s locked with an Exclusive lock instead of an update lock:

spid | dbid | ObjId     | IndId | Type | Resource | Mode | Status
------------------------------------------------------------------
63   | 10   | 233208031 | 0     | TAB  |          | X    | GRANT

How can I get an update (U) lock instead?

  • 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-15T04:46:33+00:00Added an answer on May 15, 2026 at 4:46 am

    You said:

    I don’t want to prevent other
    processes from reading the table, but
    I do want to prevent other processes
    updating the table.

    You simply need a shared read lock for the duration of the TXN. This means no other process can get a “write” lock, in conjunction with a TABLOCK. And you don’t need COUNT either.

    ...
       BEGIN TRANSANCTION
         SELECT TOP 1 KeyCol FROM TheTable WITH (TABLOCK, HOLDLOCK)
    ...
    

    Why do you think you want an UPDATE LOCK?

    HOLDLOCK or SERIALIZABLE

    Is equivalent to SERIALIZABLE. For more
    information, see SERIALIZABLE later in
    this topic. HOLDLOCK applies only to
    the table or view for which it is
    specified and only for the duration of
    the transaction defined by the
    statement that it is used in.
    …
    Makes shared locks more restrictive by
    holding them until a transaction is
    completed, instead of releasing the
    shared lock as soon as the required
    table or data page is no longer
    needed, whether the transaction has
    been completed or not.

    Edit, after comment:

    • “exclusive lock” means “only one process using the data”.
    • “SERIALIZABLE” basically means hold the locks (shared, exclusive, whatever) for a lot longer.

    You can’t specify “exclusive lock” and allow other processes to read. The concepts are mutually exclusive. You want to prevent writes to the entire table, which a persisted shared/read lock will do. This is where SERIALIZABLE comes in.

    From “Lock Modes”

    Shared Locks

    …No other
    transactions can modify the data while
    shared (S) locks exist on the
    resource. Shared (S) locks on a
    resource are released as soon as the
    read operation completes, unless the
    transaction isolation level is set to
    repeatable read or higher, or a
    locking hint is used to retain the
    shared (S) locks for the duration of
    the transaction.

    So: a shared lock disallows writes and can be made to persist by making it SERIALIZABLE

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

Sidebar

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.