Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:22:21+00:00 2026-06-12T04:22:21+00:00

I am using SQL broker to do some asynchronous tasks(In my case sending mails).

  • 0

I am using SQL broker to do some asynchronous tasks(In my case sending mails). But the problem I am having is the stored procedure which is run when the the value is inserted to the queue runs twice every time the XML message is passed from a Trigger to the queue.

I have a message type:

CREATE MESSAGE TYPE MailMessage
AUTHORIZATION dbo 
VALIDATION = WELL_FORMED_XML

I have a contract:

CREATE CONTRACT MailContract
AUTHORIZATION dbo 
(MailMessage SENT BY INITIATOR)

I have a Queue:

    CREATE QUEUE dbo.MessageQueue
WITH STATUS=ON, 
 ACTIVATION ( 
 PROCEDURE_NAME = MailExecuter , 
 MAX_QUEUE_READERS = 1,
 EXECUTE AS OWNER );

I have two services:

    CREATE SERVICE MailSendActivator  
AUTHORIZATION dbo  
ON QUEUE dbo.MessageQueue (MailContract) ; // I have removed this the contract to make it a initiator but it did not worked out  

-- Create target Service   
CREATE SERVICE MailSendExec  
AUTHORIZATION dbo   
ON QUEUE dbo.MessageQueue (MailContract);  

Here is my trigger:

 CREATE TRIGGER MailSendTrigOnMailQueue ON dbo.MailQueue
 FOR INSERT 
 As  
 SET NOCOUNT ON;   

DECLARE @MessageBody XML  
DECLARE @TableId int  


SET @MessageBody = (SELECT CreatedDateTime,[Subject], MailType FROM inserted  
FOR XML AUTO)  


If (@MessageBody IS NOT NULL)  
BEGIN  

DECLARE @Handle UNIQUEIDENTIFIER;   
BEGIN DIALOG CONVERSATION @Handle   
FROM SERVICE MailSendActivator   
TO SERVICE 'MailSendExec'   
ON CONTRACT MailContract   
WITH ENCRYPTION = OFF;   

SEND ON CONVERSATION @Handle MESSAGE TYPE MailMessage(@MessageBody);   

END 

I have a stored procedure:
In the stored procedure I am Inserting values to a test table whether the stored procedure is running.

Stored Procedure:

    CREATE PROCEDURE dbo.MailExecuter  
AS   
BEGIN   

DECLARE @msgBody XML    
DECLARE @dlgId uniqueidentifier 
Insert into TestTable(Name, Test) values('MEX','test'); 
WHILE (1 = 1)   
    BEGIN 

            WAITFOR ( RECEIVE TOP(1) @msgBody = CAST(message_body AS XML),     @dlgId = conversation_handle  FROM dbo.MessageQueue ), TIMEOUT 500    
            IF (@@ROWCOUNT = 0 OR @msgBody IS NULL) 
                BEGIN   
                    BREAK   
                END   
            ELSE   
                BEGIN 


                 DECLARE @Subject nvarchar(200), @CreatedDateTime datetime, @MailType nvarchar(50)


                ---EXEC dbo.SendMails 1,1;

                END  
            END CONVERSATION @dlgId

            END  

    END 

But the stored procedure is running twice and populate my test table twice. I think the problem is with the send conversation part in the trigger.

I have been struck on this for a long time. Please, Can some one help me on this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:22:22+00:00Added an answer on June 12, 2026 at 4:22 am

    You need to add to the RECEIVEprojection list the message_type_name. You must look at what message type you received and only call the mailing routine when the message type is MailMessage. As things are, you will always get a second message, the one from your END DIALOG. You simply need to call END DIALOG again on that case, to close the sending side handle:

      WAITFOR ( 
          RECEIVE TOP(1) 
             @msgBody = CAST(message_body AS XML),     
             @dlgId = conversation_handle ,
              @msgType = message_type_name
           FROM dbo.MessageQueue ), TIMEOUT 500    
        IF (@@ROWCOUNT = 0 OR @msgBody IS NULL) 
           BREAK   
        ELSE IF @msgType = N'MailMessage'   
        BEGIN 
           DECLARE @Subject nvarchar(200), @CreatedDateTime datetime, @MailType nvarchar(50)
           ---EXEC dbo.SendMails 1,1;
        END  
    
        END CONVERSATION @dlgId;
    

    Any particular reason you are re-inventing the wheel? This is pretty much how sp_send_dbmail already works (except is using external activation).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using SQL Service Broker for calling a Stored Proc. This service broker
I have configured Service Broker communication between two SQL Server 2008 instances using Windows
Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which
I'm using Linq to SQL, which generates partial classes and partial methods. You then
I have some problems when using mysql. My environment is wamp, the problem occurred
This should be a simple logic problem but for some reason I've been struggling
I have a service broker which calls a stores procedure sp_dequeue queue --> ACTIVATION
I am testing one application that handles files (stored in SQL Server using FILESTREAM
I'm using some old code that runs a sql query as a reference. At
I am using SQL Service Broker. I have a queue that another process is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.