SQL Server 2019
I'm trying to start stored procedures from a trigger (so that the trigger stays lean) to process data in some production facility. Working with queues/messages etc is new to me
I've created a queue and service:
CREATE QUEUE [dbo].[StoredProcedureQueue]
CREATE SERVICE [StoredProcedureService] ON QUEUE [dbo].[StoredProcedureQueue];
ALTER QUEUE dbo.StoredProcedureQueue
WITH ACTIVATION (
PROCEDURE_NAME = dbo.ActivationProcedure,
MAX_QUEUE_READERS = 1,
EXECUTE AS OWNER)
The activation procedure is very simple (just for now, it will trigger different stored procedures with parameters later on)
CREATE or ALTER PROCEDURE dbo.ActivationProcedure
AS
BEGIN
print 'test'
INSERT INTO tbl_Log (LogType, LogText) values ('test', 'ActivationProcedure')
END
But it's not started
The table trigger would do something like this (but I'm starting this manually):
--------------------
DECLARE @message_body XML;
DECLARE @dlg UNIQUEIDENTIFIER;
-- Construct the XML message body
SET @message_body = '
<Message>
<barcode>PS23060094/9.9</barcode>
<palletweight>993</palletweight>
<timestamp>' + CONVERT(NVARCHAR(30), GETDATE(), 120) + '</timestamp>
<processing_procedure>spProcessStep1</processing_procedure>
</Message>
';
-- Begin a dialog conversation
BEGIN DIALOG CONVERSATION @dlg
FROM SERVICE [StoredProcedureService]
TO SERVICE 'StoredProcedureService'
ON CONTRACT [DEFAULT]
WITH ENCRYPTION = OFF;
-- Send the XML message to the Service Broker queue using the conversation handle
SEND ON CONVERSATION @dlg (@message_body);
-- End the dialog conversation
END CONVERSATION @dlg;
but I'm not getting anything in tbl_log, which indicates that the activation procedure is not triggered.
The activation procedure is linked to the queue
SELECT name, activation_procedure
FROM sys.service_queues
WHERE name = 'StoredProcedureQueue';
When I query
SELECT * FROM sys.transmission_queue
SELECT * FROM sys.conversation_endpoints
I'm not seeing anything, except some times some records, sometimes not. I'm not sure this means my messages are getting processed, but I'm still not seeing the result of the activation procedure in tbl_log
At one point these gave me records, the first ... transmission_status: the target service name could not be found ... and the second ... state_desc: DISCONNECTED_OUTBOUND ...
I am db_owner, so permissions should not be a problem
Anyone that knows what to do? Btw the above code is mostly provided by Chat GPT also