SQL Server, using a table as a queue

前端 未结 5 1617
情深已故
情深已故 2021-01-02 20:34

I\'m using an SQL Server 2008 R2 as a queuing mechanism. I add items to the table, and an external service reads and processes these items. This works great, but is missing

5条回答
  •  梦毁少年i
    2021-01-02 21:22

    I need mechanism whereby I can attempt to select a single row from the table and, if there isn't one, block until there is (preferably for a specific period of time).

    You can loop and check for new rows every second:

    while not exists (select * from QueueTable)
        begin
        wait for delay '00:01'
        end
    

    Disclaimer: this is not code I would use for a production system, but it does what you ask.

提交回复
热议问题