--------------------------------------------------------------------
-- Summary: SendMessage 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


-- Begin a transaction.

BEGIN TRANSACTION ; 
GO

-- Create the message.

DECLARE @message XML ;
SET @message = N'<message>Hello, World!</message>' ;

-- Declare a variable to hold the conversation
-- handle.

DECLARE @conversationHandle UNIQUEIDENTIFIER ;

-- Begin the dialog.

BEGIN DIALOG CONVERSATION @conversationHandle
    FROM SERVICE InitiatorService
    TO SERVICE 'TargetService'
    ON CONTRACT HelloWorldContract 
	WITH ENCRYPTION = OFF;

-- Send the message on the dialog.

SEND ON CONVERSATION @conversationHandle
  MESSAGE TYPE HelloWorldMessage
  (@message) ;

-- End the conversation.

END CONVERSATION @conversationHandle ;

GO

-- Commit the transaction. Service Broker
-- sends the message to the destination
-- service only when the transaction commits.

COMMIT TRANSACTION ;
GO


-- Show the messages in the target queue. Notice that target queue
-- contains both a HelloWorldMessage and a message of type
-- http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog.

SELECT * FROM [dbo].[TargetQueue]
