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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:48:33+00:00 2026-05-16T23:48:33+00:00

Let’s say I’ve got a SQL Server database table with X (> 1,000,000) records

  • 0

Let’s say I’ve got a SQL Server database table with X (> 1,000,000) records in it that need to be processed (get data, perform external action, update status in db) one-by-one by some worker processes (either console apps, windows service, Azure worker roles, etc). I need to guarantee each row is only processed once. Ideally exclusivity would be guaranteed no matter how many machines/processes were spun up to process the messages. I’m mostly worried about two SELECTs grabbing the same rows simultaneously.

I know there are better datastores for queuing out there, but I don’t have that luxury for this project. I have ideas for accomplishing this, but I’m looking for more.

  • 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-16T23:48:33+00:00Added an answer on May 16, 2026 at 11:48 pm

    I’ve had this situation.

    Add an InProcess column to the table, default = 0. In the consumer process:

    UPDATE tbl SET Inprocess = @myMachineID WHERE rowID = 
        (SELECT MIN(rowID) WHERE InProcess = 0)
    

    Now that machine owns the row, and you can query its data without fear. Usually your next line will be something like this:

    SELECT * FROM tbl WHERE rowID = 
        (SELECT MAX(rowID) FROM tbl WHERE ProcessID = @myMachineID)
    

    You’ll also have to add a Done flag of some kind to the row, so you can tell if the row was claimed but processing was incomplete.

    Edit

    The UPDATE gets an exclusive lock (see MSDN). I’m not sure if the SELECT in the subquery is allowed to be split from the UPDATE; if so, you’d have to put them in a transaction.

    @Will A posts a link which suggests that beginning your batch with this will guarantee it:

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED
    

    …but I haven’t tried it.

    @Martin Smith’s link also makes some good points, looking at the OUTPUT clause (added in SQL 2005).

    One last edit

    Very interesting exchange in the comments, I definitely learned a few things here. And that’s what SO is for, right?

    Just for color: when I used this approach back in 2004, I had a bunch of web crawlers dumping URLs-to-search into a table, then pulling their next URL-to-crawl from that same table. Since the crawlers were attempting to attract malware, they were liable to crash at any moment.

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

Sidebar

Related Questions

Let's say that I have a SQLite database that I create in a separate
Let me try to explain what I need. I have a server that is
Let's say I've got a collection of 10 million documents that look something like
let's say that I have a row in my table: | ID | Name
Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say I can call a method like this: core::get() . What is the
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say, if I need to assign an id to element with first name,
Let's say I have multiple requirements for a password. The first is that the

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.