Link to home
Start Free TrialLog in
Avatar of soozh
soozhFlag for Sweden

asked on

MS SQL - How to handle a table that queue

Hello,

I have a table that represents a queue, where many applications are reading and processing the top of the queue.  The top item is not deleted from the queue - just marked as "being processed"

How do i best handle this so that i dont end up with two applications symultaneously reading the top entry and trying to handle it?  Should i put all my code in a stored procedure and wrap a transaction around it?

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of soozh

ASKER

Hej,

My programmer has written the following :

UPDATE TOP (1) ReportTemplateSchedules SET sch_Running = 'T', sch_NextRuntime = NULL OUTPUT inserted.* WHERE sch_Running = 'F' AND sch_id in (SELECT TOP 1 sch_id FROM ReportTemplateSchedules WHERE sch_NextRunTime < 'some date' ORDER BY sch_NextRunTime asc)

He sets the sch_Running flag true, and the sch_Runtime to NULL to the top record and then checks to see any data is returned.

DO you see anything wrong with this... it seems to be similar to your example except it uses on statement?