【问题标题】:Delete messages in service broker queue删除服务代理队列中的消息
【发布时间】:2012-06-05 11:05:38
【问题描述】:

我想在 SQL Server Management Studio 中清除我的队列,但我不想删除整个队列,只删除队列中的内容(消息)。

【问题讨论】:

  • 您要清除数据库中所有队列中的所有消息吗?你应该使用ALTER DATABASE ... SET NEW_BROKER WITH ROLLBACK IMMEDIATE;

标签: sql-server queue service-broker


【解决方案1】:

为了清楚起见,只需结合前面的两个答案(Ben 和 Jānis)。这对我有用:

declare @c uniqueidentifier
while(1=1)
begin
    select top 1 @c = conversation_handle from dbo.queuename
    if (@@ROWCOUNT = 0)
    break
    end conversation @c with cleanup
end

【讨论】:

  • 这个答案的另一个好处是使用SELECT 而不是RECEIVE 可以清理队列,即使它被禁用。
  • 当我用 (@c is null) 条件更改 (@@ROWCOUNT=0) 检查时为我工作。谢谢
【解决方案2】:

这样的事情应该可以工作:

while(1=1)
begin
    waitfor (
        receive top(1)
        conversation_group_id
        from dbo.yourQueue
    ), timeout 1000;

    if (@@rowcount = 0)
        break;
end

【讨论】:

【解决方案3】:

我会使用end conversation(这也会从所有队列中删除所有相关消息)使用语句:

End Converstation @c With CleanUp

如果您只是收到消息,那么您将保持对话打开。 使用 CleanUp 结束对话仅适用于特定情况。

【讨论】:

    【解决方案4】:

    如果您使用的是 SQL Server(从 2008 年开始),您可以使用 RECEIVE

    WHILE (0=0)
    BEGIN
        RECEIVE * FROM dbo.YourQueue;
        IF (@@ROWCOUNT = 0) BREAK;
    END
    

    【讨论】:

      【解决方案5】:

      我遇到了与原始海报相同的问题,但我需要清除队列 数百万条消息(失败的消息队列,尤其是在多年未检查的非生产环境中)。

      上述解决方案本来可行,但每分钟处理的消息不到 10 条。通过批量处理,我每分钟收到 30000 条消息。

      代码中唯一值得注意的项目是where validation = 'N'。这将对话句柄限制为真实消息。 end conversation 删除了响应/错误的重复对话句柄。如果没有这个子句,脚本仍然可以工作,但会在输出中产生很多错误。

      declare @conversationBatch table (convH uniqueidentifier)
      declare @conversationHandle uniqueidentifier
      
      declare convCursor cursor for
          select convH from @conversationBatch
      
      insert into @conversationBatch
          select top 1000 conversation_handle  
            from dbo.queuename WITH (NOLOCK) 
           where validation = 'N'
      
      
      while @@rowcount > 0
      begin
          open convCursor
          fetch next from convCursor into @conversationHandle
          while @@FETCH_STATUS = 0 
          begin
              end conversation @conversationHandle with cleanup
              fetch next from convCursor into @conversationHandle
          end
          close convCursor
      
          delete from @conversationBatch
          insert into @conversationBatch
              select top 1000 conversation_handle  
                from dbo.queuename WITH (NOLOCK) 
               where validation = 'N'
          
       end
      
       deallocate convCursor
      

      【讨论】:

        【解决方案6】:
        while(1=1)
        begin
            waitfor (
                receive top(1)
                conversation_group_id
                from kartokumaqueue2), timeout 1000;
        
                if(@@ROWCOUNT = 0) break;
        end
        

        【讨论】:

        猜你喜欢
        • 2023-03-17
        • 2020-01-10
        • 2018-10-24
        • 1970-01-01
        • 2015-10-18
        • 1970-01-01
        • 2022-01-13
        • 2015-04-23
        • 1970-01-01
        相关资源
        最近更新 更多