CREATE CONTRACT (Transact-SQL)
Creates a new contract. A contract defines the message types that are used in a Service Broker conversation and also determines which side of the conversation can send messages of that type. Each conversation follows a contract. The initiating service specifies the contract for the conversation when the conversation starts. The target service specifies the contracts that the target service accepts conversations for.
The order of message types in the contract is not significant. After the target has received the first message, Service Broker allows either side of the conversation to send any message allowed for that side of the conversation at any time. For example, if the initiator of the conversation can send the message type //Adventure-Works.com/Expenses/SubmitExpense, Service Broker allows the initiator to send any number of SubmitExpense messages during the conversation.
The message types and directions in a contract cannot be changed. To change the AUTHORIZATION for a contract, use the ALTER AUTHORIZATION statement.
A contract must allow the initiator to send a message. The CREATE CONTRACT statement fails when the contract does not contain at least one message type that is SENT BY ANY or SENT BY INITIATOR.
Regardless of the contract, a service can always receive the message types http://schemas.microsoft.com/SQL/ServiceBroker/DialogTimer, http://schemas.microsoft.com/SQL/ServiceBroker/Error, and http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog. Service Broker uses these message types for system messages to the application.
A contract cannot be a temporary object. Contract names starting with # are permitted, but are permanent objects.
By default, members of the db_ddladmin or db_owner fixed database roles and the sysadmin fixed server role can create contracts.
By default, the owner of the contract, members of the db_ddladmin or db_owner fixed database roles, and members of the sysadmin fixed server role have REFERENCES permission on a contract.
The user executing the CREATE CONTRACT statement must have REFERENCES permission on all message types specified.
A. Creating a contract
The following example creates an expense reimbursement contract based on three message types.
CREATE MESSAGE TYPE [//Adventure-Works.com/Expenses/SubmitExpense] VALIDATION = WELL_FORMED_XML ; CREATE MESSAGE TYPE [//Adventure-Works.com/Expenses/ExpenseApprovedOrDenied] VALIDATION = WELL_FORMED_XML ; CREATE MESSAGE TYPE [//Adventure-Works.com/Expenses/ExpenseReimbursed] VALIDATION= WELL_FORMED_XML ; CREATE CONTRACT [//Adventure-Works.com/Expenses/ExpenseSubmission] ( [//Adventure-Works.com/Expenses/SubmitExpense] SENT BY INITIATOR, [//Adventure-Works.com/Expenses/ExpenseApprovedOrDenied] SENT BY TARGET, [//Adventure-Works.com/Expenses/ExpenseReimbursed] SENT BY TARGET ) ;