--------------------------------------------------------------------
-- Summary: RecievesMessage script for Service Broker
--          HelloWorld sample.
-- 
--------------------------------------------------------------------
-- This file is part of the Microsoft SQL Server Code Samples.
-- Copyright (C) Microsoft Corporation. All Rights reserved.
-- This source code is intended only as a supplement to Microsoft
-- Development Tools and/or on-line documentation. See these other
-- materials for detailed information regarding Microsoft code samples.
--
-- THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
-- ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
-- PARTICULAR PURPOSE.
--------------------------------------------------------------------

USE AdventureWorks ;
GO

-- Process all conversation groups.
WHILE (1 = 1)
BEGIN

DECLARE @conversation_handle UNIQUEIDENTIFIER,
        @conversation_group_id  UNIQUEIDENTIFIER,
        @message_body XML,
        @message_type_name NVARCHAR(128);

BEGIN TRANSACTION ;

-- Get next conversation group.

WAITFOR(
   GET CONVERSATION GROUP @conversation_group_id FROM [dbo].[TargetQueue]),
   TIMEOUT 500 ;

-- If there are no more conversation groups, roll back the
-- transaction and break out of the outermost WHILE loop.

IF @conversation_group_id IS NULL
BEGIN
    ROLLBACK TRANSACTION ;
    BREAK ;
END ;

    -- Process all messages in the conversation group. Notice
    -- that all processing occurs in the same transaction.

    WHILE 1 = 1
    BEGIN

        -- Receive the next message for the conversation group.
        -- Notice that the receive statement includes a WHERE
        -- clause to ensure that the messages recieved belong to
        -- the same conversation group.

        RECEIVE
           TOP(1)
           @conversation_handle = conversation_handle,
           @message_type_name = message_type_name,
           @message_body =
           CASE
              WHEN validation = 'X' THEN CAST(message_body AS XML)
              ELSE CAST(N'<none/>' AS XML)
          END
        FROM [dbo].[TargetQueue]
        WHERE conversation_group_id = @conversation_group_id ;

        -- If there are no more messages, or an error occured,
        -- stop processing this conversation group.

        IF @@ROWCOUNT = 0 OR @@ERROR <> 0 BREAK;

        -- Show the information received.

        SELECT 'Conversation Group Id' = @conversation_group_id,
               'Conversation Handle' = @conversation_handle,
               'Message Type Name' = @message_type_name,
               'Message Body' = @message_body ;


        -- If the message_type_name indicates that the message is an error
        -- or an end dialog message, end the conversation.

        IF @message_type_name = 'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog'
           OR @message_type_name = 'http://schemas.microsoft.com/SQL/ServiceBroker/Error'
        BEGIN
           END CONVERSATION @conversation_handle ;
        END ;

    END; -- Process all messages in conversation group.

   -- Commit the receive statements and the end conversation statement. 

   COMMIT TRANSACTION ;

END ; -- Process all conversation groups.
