In Message Broker can you configure to use one queue but based on criteria send to a different Databases or is it a one to one relationship? I am tryibg to figure out if I should use Message broker or SSIS package.
Service Broker to Send Messages Specific Database bases based on critiera
service-broker
Related Solutions
is there a way I can send messages directly to the Database Mail queue using Service Broker
When you invoke sp_send_dbmail
that is exactly what is happening.
I want to setup an EVENT NOTIFICATION for SQL Server, and I'd like to send e-mails to operators for each message on the queue
Then do just that. Create an activated stored procedure on the queue of your service that receives the notifications and have it RECEIVE the message and invokes sp_send_dbmail
.
I think you are trying to 'cut out the middleman' and have the EVENT NOTIFICATION send the notification directly to the DB Mail queue. That is not possible because the code that handles the messages arriving in the DB Mail queue expects the messages to have a certain format (ie. the one created by sp_send_dbmail
). The messages sent by Event Notifications would have a completely different format and the DB Mail processor would choke as it does not understand them. In order to prevent exactly such situation Service Broker services are bound to specific contracts and DB Mail service does not accept the http://schemas.microsoft.com/SQL/Notifications/PostEventNotification
contract used by Event Notifications.
You could send the xml data in predefined chunks by using something like the trigger in the following setup.
Create the testbed in tempdb:
USE tempdb;
IF EXISTS (SELECT 1 FROM sys.triggers t WHERE t.name = 'TriggerTest_Chunked')
DROP TRIGGER dbo.TriggerTest_Chunked;
IF EXISTS(SELECT 1 FROM sys.tables t WHERE t.name = 'TriggerTest')
DROP TABLE dbo.TriggerTest;
CREATE TABLE dbo.TriggerTest
(
ID INT NOT NULL
);
IF EXISTS(SELECT 1 FROM sys.tables t WHERE t.name = 'TriggerTestTarget')
DROP TABLE dbo.TriggerTestTarget;
CREATE TABLE dbo.TriggerTestTarget
(
OperationType INT NOT NULL
, DEETS XML NOT NULL
);
The trigger:
CREATE TRIGGER dbo.TriggerTest_Chunked
ON dbo.TriggerTest
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @RowStart INT;
DECLARE @RowEnd INT;
DECLARE @RowCount INT;
DECLARE @BatchSize INT;
SET @BatchSize = 1000;
SET @RowStart = 0;
SELECT @RowCount = COUNT(1)
FROM inserted;
IF @RowCount > @BatchSize
SET @RowEnd = @BatchSize
ELSE
SET @RowEnd = @RowCount;
WHILE @RowEnd <= @RowCount AND (@RowStart < @RowEnd)
BEGIN
INSERT INTO dbo.TriggerTestTarget (OperationType, DEETS)
SELECT 1, (
SELECT ID
FROM (
SELECT *
, rn = ROW_NUMBER() OVER (ORDER BY ID)
FROM inserted
) i_rn
WHERE i_rn.rn > @RowStart
AND i_rn.rn <= @RowEnd
FOR XML PATH('')
);
SET @RowStart = @RowStart + @BatchSize;
IF @RowEnd + @BatchSize > @RowCount
SET @BatchSize = @RowCount - @RowEnd;
SET @RowEnd = @RowEnd + @BatchSize;
END
SET @RowStart = 0;
SELECT @RowCount = COUNT(1)
FROM deleted;
IF @RowCount > @BatchSize
SET @RowEnd = @BatchSize
ELSE
SET @RowEnd = @RowCount;
WHILE @RowEnd <= @RowCount AND (@RowStart < @RowEnd)
BEGIN
INSERT INTO dbo.TriggerTestTarget (OperationType, DEETS)
SELECT 2, (
SELECT ID
FROM (
SELECT *
, rn = ROW_NUMBER() OVER (ORDER BY ID)
FROM deleted
) i_rn
WHERE i_rn.rn > @RowStart
AND i_rn.rn <= @RowEnd
FOR XML PATH('')
);
SET @RowStart = @RowStart + @BatchSize;
IF @RowEnd + @BatchSize > @RowCount
SET @BatchSize = @RowCount - @RowEnd;
SET @RowEnd = @RowEnd + @BatchSize;
END
END
GO
Insert some test data to see what the trigger does:
INSERT INTO dbo.TriggerTest (ID)
SELECT ROW_NUMBER() OVER (ORDER BY o1.object_id, o2.object_id)
FROM sys.objects o1
, sys.objects o2;
Show results:
SELECT *
FROM dbo.TriggerTest;
SELECT *
FROM dbo.TriggerTestTarget;
DELETE TOP(100)
FROM dbo.TriggerTest;
SELECT *
FROM dbo.TriggerTestTarget;
Best Answer
Service Broker conversations are between services, not between queues. You send from a service, to a service. A conversation is tied to exactly a pair of services and cannot change. The same service can participate in any number conversations, with any number of peer services. If you want to send messages to multiple peers, you need to start multiple conversations.