SQL Server,使用表作为队列

编程入门 行业动态 更新时间:2024-10-23 13:37:11
本文介绍了SQL Server,使用表作为队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 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,使用表作为队列

本文发布于:2023-10-15 01:01:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492776.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!