SQL Server,使用表作为队列 [英] SQL Server, using a table as a queue
问题描述
我使用 SQL Server 2008 R2 作为排队机制.我将项目添加到表中,外部服务读取并处理这些项目.这很好用,但缺少一件事 - 我需要一种机制,我可以尝试从表中选择一行,如果没有,则阻塞直到有(最好在特定时间段内).
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 one thing - 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).
谁能就我如何实现这一目标提出建议?
Can anyone advise on how I might achieve this?
推荐答案
我需要一种我可以尝试的机制从表中选择一行并且,如果没有,则阻塞直到有(最好是针对特定的一段时间).
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.
这篇关于SQL Server,使用表作为队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!