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

  • Home
  • SEARCH
  • 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 378461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:47:43+00:00 2026-05-12T14:47:43+00:00

I want to update a single record in a table to reflect that a

  • 0

I want to update a single record in a table to reflect that a given client session has acquired the record (and now owns it for further updates) within a multi-session environment. I’ve got this so far:

create procedure AcquireRow(
  @itemNo   int,          -- Item ID to acquire
  @sessNo   int,          -- Session ID
  @res      char(1) out)  -- Result
as
begin
  -- Attempt to acquire the row
  update Items
    set
      State = 'A',      -- 'A'=Acquired
      SessionID = @sessNo
    where ItemID = @itemNo
      and State = 'N';  -- 'N'=Not acquired  

  -- Verify that the session actually acquired the row
  set @res = 'T';       -- 'T'=Success
  if @@rowcount = 0
    set @res = 'F';     -- 'F'=Failure
end;

The out variable @state is set to 'T' if the procedure successfully acquired the row, otherwise it’s set to 'F' to indicate failure.

My question: Is this guaranteed to work atomically, so that only one session successfully acquires (updates) the row if several sessions call AcquireRow() at the same time? Or is there a better way of doing this? Do I need an explicit rowlock?

Amended:
Based on Remus’s answer, I would rearrange the code thus:

set @res = 'F';
update ...;
if @@rowcount > 0
    set @res = 'T';

Using an output clause or assigning the resulting row’s ItemID to a variable within the update would also be prudent.

  • 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-12T14:47:44+00:00Added an answer on May 12, 2026 at 2:47 pm

    @@ROWCOUNT is notoriously easy to get wrong. For example, in your case. From MSDN:

    Statements that make a simple
    assignment always set the @@ROWCOUNT
    value to 1. No rows are sent to the
    client. Examples of these statements
    are: SET @local_variable …

    To be correct you should check the @@ROWCOUNT immediately after the UPDATE. It is safer to use a ROWLOCK but is not necessary. Even if the optimizer decides to use page locks, the ‘acquire’ semantics are correct.

    I would probably prefer another approach, namely to use the OUTPUT clause of UPDATE:

    declare @updated table (ItemId int);
    
    update Items
    set ...
    output inserted.ItemId
    into @updated (ItemId)
    where ...
    

    This scheme is more error proof and also more flexible, as it allows for acquiring of unknown ItemId: the id acquired is placed in the @updated table variable and can be returned to caller.

    As a general note, using real, committed, updates for ‘acquire’ is riddled with problems as you cannot know what rows are really acquired and which ones are just abandoned (client disconnected or crashed w/o releasing the ‘acquisition’).

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

Sidebar

Related Questions

I want to insert into table single record in one shot with values coming
I want to update a single datarow in a datatable using multiple threads. Is
I want to update two columns in a table. The value of the second
I want to update a column in a table making a join on other
I would like to update multiple records in a MySQL table using a single
I want to know how to Update / delete the table records in ASPNETDB.MDF
Here is my query, to locate a single record in table: SELECT TOP 1
How can I update a single field without knowing the record's ID? I have
Sometimes I want to join a record that I can easily identify as sort
I want update a column by adding days to current time. In pseudosyntax it

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.