CREATE ROUTE (Transact-SQL)
Adds a new route to the routing table for the current database. For outgoing messages, Service Broker determines routing by checking the routing table in the local database. For messages on conversations that originate in another instance, including messages to be forwarded, Service Broker checks the routes in msdb.
The routing table that stores the routes is a metadata table that can be read through the sys.routes catalog view. This catalog view can only be updated through the CREATE ROUTE, ALTER ROUTE, and DROP ROUTE statements.
By default, the routing table in each user database contains one route. This route is named AutoCreatedLocal. The route specifies 'LOCAL' for the next_hop_address and matches any service name and broker instance identifier.
When a route specifies 'TRANSPORT' for the next_hop_address, the network address is determined based on the name of the service. SQL Server can successfully process service names that begin with a network address in a format that is valid for a next_hop_address.
The routing table can contain any number of routes that specify the same service, network address, and broker instance identifier. In this case, Service Broker chooses a route using a procedure designed to find the most exact match between the information specified in the conversation and the information in the routing table. For more information about how Service Broker chooses a route, see Service Broker Routing.
Service Broker does not remove expired routes from the routing table. An expired route can be made active using the ALTER ROUTE statement.
A route cannot be a temporary object. Route names that start with # are allowed, but are permanent objects.
A. Creating a TCP/IP route by using a DNS name
The following example creates a route to the service //Adventure-Works.com/Expenses. The route specifies that messages to this service travel over TCP to port 1234 on the host identified by the DNS name www.Adventure-Works.com. The target server delivers the messages upon arrival to the broker instance identified by the unique identifier D8D4D268-00A3-4C62-8F91-634B89C1E315.
CREATE ROUTE ExpenseRoute
WITH
SERVICE_NAME = '//Adventure-Works.com/Expenses',
BROKER_INSTANCE = 'D8D4D268-00A3-4C62-8F91-634B89C1E315',
ADDRESS = 'TCP://www.Adventure-Works.com:1234' ;
B. Creating a TCP/IP route by using a NetBIOS name
The following example creates a route to the service //Adventure-Works.com/Expenses. The route specifies that messages to this service travel over TCP to port 1234 on the host identified by the NetBIOS name SERVER02. Upon arrival, the target SQL Server delivers the message to the database instance identified by the unique identifier D8D4D268-00A3-4C62-8F91-634B89C1E315.
CREATE ROUTE ExpenseRoute
WITH
SERVICE_NAME = '//Adventure-Works.com/Expenses',
BROKER_INSTANCE = 'D8D4D268-00A3-4C62-8F91-634B89C1E315',
ADDRESS = 'TCP://SERVER02:1234' ;
C. Creating a TCP/IP route by using an IP address
The following example creates a route to the service //Adventure-Works.com/Expenses. The route specifies that messages to this service travel over TCP to port 1234 on the host at the IP address 192.168.10.2. Upon arrival, the target SQL Server delivers the message to the broker instance identified by the unique identifier D8D4D268-00A3-4C62-8F91-634B89C1E315.
CREATE ROUTE ExpenseRoute
WITH
SERVICE_NAME = '//Adventure-Works.com/Expenses',
BROKER_INSTANCE = 'D8D4D268-00A3-4C62-8F91-634B89C1E315',
ADDRESS = 'TCP://192.168.10.2:1234' ;
D. Creating a route to a forwarding broker
The following example creates a route to the forwarding broker on the server dispatch.Adventure-Works.com. Because both the service name and the broker instance identifier are not specified, SQL Server uses this route for services that have no other route defined. For more information about routing, see Service Broker Routing.
CREATE ROUTE ExpenseRoute
WITH
ADDRESS = 'TCP://dispatch.Adventure-Works.com' ;
E. Creating a route to a local service
The following example creates a route to the service //Adventure-Works.com/LogRequests in the same instance as the route.
CREATE ROUTE LogRequests
WITH
SERVICE_NAME = '//Adventure-Works.com/LogRequests',
ADDRESS = 'LOCAL' ;
F. Creating a route with a specified lifetime
The following example creates a route to the service //Adventure-Works.com/Expenses. The lifetime for the route is 259200 seconds, which equates to 72 hours.
CREATE ROUTE ExpenseRoute
WITH
SERVICE_NAME = '//Adventure-Works.com/Expenses',
LIFETIME = 259200,
ADDRESS = 'TCP://services.Adventure-Works.com:1234' ;
G. Creating a route to a mirrored database
The following example creates a route to the service //Adventure-Works.com/Expenses. The service is hosted in a database that is mirrored. One of the mirrored databases is located at the address services.Adventure-Works.com:1234, and the other database is located at the address services-mirror.Adventure-Works.com:1234.
CREATE ROUTE ExpenseRoute
WITH
SERVICE_NAME = '//Adventure-Works.com/Expenses',
BROKER_INSTANCE = '69fcc80c-2239-4700-8437-1001ecddf933',
ADDRESS = 'TCP://services.Adventure-Works.com:1234',
MIRROR_ADDRESS = 'TCP://services-mirror.Adventure-Works.com:1234' ;
H. Creating a route that uses the service name for routing
The following example creates a route that uses the service name to determine the network address to send the message to. Notice that a route that specifies 'TRANSPORT' as the network address has lower priority for matching than other routes.
CREATE ROUTE TransportRoute
WITH ADDRESS = 'TRANSPORT' ;
