How to clean all messages of a queue
Online books says it can be done as below:
RECEIVE * FROM ExpenseQueue
But it only delete a row every time,and I found all the three statement only delete a row:
RECEIVE * FROM ExpenseQueue
RECEIVE TOP(1) * FROM ExpenseQueue
RECEIVE TOP(n) * FROM ExpenseQueue

how to clean all messages of a queue ?
Jaaaaa
The RECEIVE statement (as it exists today) only fetches messages belonging to a single conversation group at a time. So the only way to get rid of all messages is to call RECEIVE in a loop:
WHILE (1)
BEGIN
RECEIVE * FROM ExpenseQueue
IF (@@ROWCOUNT = 0)
BREAK
END
Are you just looking at means to clean up the queue, or do you really want to fetch multiple conversation groups in a single batch. If it is the latter, could you explain your requirements further to help us improve the product in future versions
Thanks,
Rushi
--
Developer, Service Broker Team, SQL Server Engine
Microsoft Corporation
KenHay
declare conv cursor for select conversation_handle from Queue_Name
And I think it is convenient to use a statement like "alter queue XXX with cleanup" .
Thank you.
Sebastian Mares
-- End conversations that are in an error state
declare
@handle uniqueidentifierdeclare
conv cursor for select conversation_handle from sys.conversation_endpoints where state = 'ER'open
convfetch
NEXT FROM conv into @handlewhile
@@FETCH_STATUS = 0Begin
END Conversation @handle with cleanup fetch NEXT FROM conv into @handleEnd
close
convdeallocate
conv-- End conversations that are half closed
declare
@handle uniqueidentifierdeclare
conv cursor for select conversation_handle from sys.conversation_endpoints where state = 'DI'open
convfetch
NEXT FROM conv into @handlewhile
@@FETCH_STATUS = 0Begin
END Conversation @handle with cleanup fetch NEXT FROM conv into @handleEnd
close
convdeallocate
conv-- End all conversations
declare
@handle uniqueidentifierdeclare
conv cursor for select conversation_handle from sys.conversation_endpointsopen
convfetch
NEXT FROM conv into @handlewhile
@@FETCH_STATUS = 0Begin
END Conversation @handle with cleanup fetch NEXT FROM conv into @handleEnd
close
convdeallocate
convTrojanfan20