Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select records to show they are in use

Author  Topic 

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2011-11-18 : 13:17:33
i am writing a multithreaded app that grabs records by 100 and processes them.

how do i write a select query that thread 1 grabs the first 100 rows and update a status field to 1 and thread 2 grabs the next 100 row and updates the status feild to 1

my problem is thread one grabs its 100 and starts updating and thread 2 selects some of the same records becuase they havent had their status feild updated yet. so Im not getting a 100 records per thread

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-11-18 : 13:22:39
Use a transaction.
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2011-11-18 : 14:41:16
thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-11-18 : 15:23:24
It depends on how you are "grabbing" them and "updating" them. A transaction probably isn't the answer.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-19 : 01:51:34
another way is to have a updateowner field and each thread will update it to a unique id when it starts. Any threads that starts subsquently will only fetches those rows where updateowner field is null which will avoid it from picking rows already picked by thread1 again.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -