/*
  Script for database 2 for SSB sample, communicating
  between two local databases.

  This script receives a message from the queue,
  sends a message in return and ends the converation

  Any questions etc.: nielsb@develop.com

*/

--Step 2

use [loc_ssb2];
go

--check that we have a message in the queue
--if you have followed the script up until here
--there should be a message in the queue

SELECT * from q2;
go


--Step 3
----so a message is in the queue
----read off the message, send a message back;
----uncomment from here to go and run

--DECLARE @m xml; --the received message
--DECLARE @h uniqueidentifier; -- conversation handle
--
--receive top(1) @m = message_body,
--              @h = conversation_handle
--from q2
--
----select out the message
--select @m;
--
--DECLARE @msg xml; --return msg
--SET @msg = '<worldo/>';
--SEND ON CONVERSATION @h
--MESSAGE TYPE [recmsg]
--(@msg);
--
----check that the message is not in the tx queue
--SELECT * from sys.transmission_queue
--if(@@rowcount > 0)
--  print 'something is not right, check the tx queue'
--
----check if we have received an error in s1's queue
--SELECT * from q2
--if(@@rowcount > 0)
--  print 'An error has happened. Check the queue'
--
--go

--Step 4
--well it seems everything is OK
--you can now end the conversation

----as the handle above has gone out of scope, we need
----to find the conversation handle
----uncomment from here to go and run
--DECLARE @h uniqueidentifier
--select top(1) @h = conversation_handle
--from sys.conversation_endpoints;
--
----end the conversation
--END CONVERSATION @h;
--go



----finally do a cleanup
----uncomment from here to go and run
--DECLARE @h uniqueidentifier
--select top(1) @h = conversation_handle
--from sys.conversation_endpoints;
--
----end the conversation
--END CONVERSATION @h
--with cleanup;
--go

--go now back to Step 5 in script 2_communication_db1.sql

