sqlserver清空service broker中的队列的语句分享
程序员文章站
2023-12-15 21:42:10
复制代码 代码如下: use testdb declare @conversation uniqueidentifier while exists (select 1 fr...
复制代码 代码如下:
use testdb
declare @conversation uniqueidentifier
while exists (select 1 from sys.transmission_queue )
begin
set @conversation = (select top 1 conversation_handle from sys.transmission_queue )
end conversation @conversation with cleanup
end
那么客户端接受到的消息如果没有处理,也会积攒在客户端队列中,其实就相当于许多未读邮件,我们可以使用以下脚本读取队列 ,读取后队列自动清空:
复制代码 代码如下:
use testdb
declare @recvreplymsg nvarchar(100) ;
declare @recvreplydlghandle uniqueidentifier ;
begin transaction ;
while ( 1 = 1 )
begin
waitfor
( receive top(1)
@recvreplydlghandle = conversation_handle,
@recvreplymsg = message_body
from dbo.test_targetqueue
), timeout 1000 ;
end conversation @recvreplydlghandle ;
select @recvreplymsg as receivedreplymsg ;
end
commit transaction ;