views:

27

answers:

2

I've just inherited a SQL Server 2005 database that's using service broker (this is part of a bigger project/solution). Everything about the solution is working fine. I'm trying to grok the service broker SQL, and I see this statement.

BEGIN DIALOG CONVERSATION @h
FROM SERVICE foo_Init
TO SERVICE 'foo_Target'
ON CONTRACT fooContract

Why is foo_Init not in single quotes? I'd expect it to be, just like 'foo_Target'.

+2  A: 

From Books On Line

BEGIN DIALOG [ CONVERSATION ] @dialog_handle
   FROM SERVICE initiator_service_name
   TO SERVICE 'target_service_name'
       [ , { 'service_broker_guid' | 'CURRENT DATABASE' } ] 
   [ ON CONTRACT contract_name ]
   [ WITH
   [  { RELATED_CONVERSATION = related_conversation_handle 
      | RELATED_CONVERSATION_GROUP = related_conversation_group_id } ] 
   [ [ , ] LIFETIME = dialog_lifetime ] 
   [ [ , ] ENCRYPTION = { ON | OFF }  ] ]
[ ; ]

FROM SERVICE initiator_service_name Specifies the service that initiates the dialog. The name specified must be the name of a service in the current database. The queue specified for the initiator service receives messages returned by the target service and messages created by Service Broker for this conversation.

TO SERVICE 'target_service_name' Specifies the target service with which to initiate the dialog. The target_service_name is of type nvarchar(256). Service Broker uses a byte-by-byte comparison to match the target_service_name string. In other words, the comparison is case-sensitive and does not take into account the current collation.

SQLMenace
I interpret this answer as "because target_service_name is of type nvarchar and initiator_service_name isn't. Am I overlooking something in the answer? Maybe I just asked the question poorly, but I'm wondering, if that's the answer, why the target and init names aren't specified using the same type.
lance
It is probably to differentiate with syntax coloring between the two
SQLMenace
I'd love to know if that's correct.
lance
+2  A: 

FROM SERVICE must be a service in a current database, i.e. something that you can get your hands on right away. Therefore you're required to refer to it by its sysname. TO SERVICE on the other hand is just "something out there". It may not even exist while the BEGIN DIALOG statement is executing. When a message needs to be sent, Service Broker routes come into play and tell Service Broker where that "something out there" is actually located and how to reach it. Therefore referring to the target service by sysname (as it is with initiator service) would make sense only if the target service was in the same database, which may not always be the case.

Pawel Marciniak