0

我創建了一個目標隊列使用服務代理。我的目標隊列在SQL Server收到的消息是這樣的:服務經紀人與激活程序錯誤

USE [DatabaseB] 
GO 

ALTER QUEUE [dbo].[TargetQueue] WITH STATUS = OFF , RETENTION = OFF , ACTIVATION ( STATUS = ON , PROCEDURE_NAME = [dbo].[myp] , MAX_QUEUE_READERS = 10 , EXECUTE AS N'dbo' ), POISON_MESSAGE_HANDLING (STATUS = ON) 

這裏是我的存儲過程,我打電話的時候,該消息在target_queue

ALTER PROCEDURE [DBO]接收。[MYP]

AS 


WHILE (1=1) 
    BEGIN 

    Declare @ConversationHandle as uniqueidentifier 
Declare @MessageBody as nvarchar(max) 
Declare @MessageType as sysname 

Begin Transaction 
Print 'Started Receiving '; 

RECEIVE top (1) 
     @MessageType = message_type_name, 
     @ConversationHandle = conversation_handle, 
    @MessageBody = message_body 
FROM TargetQueue; 

if @MessageType = 'SenderMessageType' 
     Begin 
      SEND 
        ON CONVERSATION @ConversationHandle 
        Message Type ReceiverMessageType 
        ('Message is received') 
      END Conversation @ConversationHandle 
      insert into table_1 (dataa) values ('salam') 
     END 

Commit 


    end 

但是,當由隊列接收到的消息我的商店過程增添了無限數據到table_1但我有一個條件,你可以看到上面,我的意思是收到的每個消息sp應該添加一行表,而不是無限的行。爲什麼?

回答

0

我只是改變了存儲過程這樣:

ALTER PROCEDURE [dbo].[myp] 

AS 


WHILE (1=1) 
    BEGIN 

    Declare @ConversationHandle as uniqueidentifier 
Declare @MessageBody as nvarchar(max) 
Declare @MessageType as sysname 

Begin Transaction 
Print 'Started Receiving '; 
WAITFOR 
    (
RECEIVE top (1) 
     @MessageType = message_type_name, 
     @ConversationHandle = conversation_handle, 
    @MessageBody = message_body 
FROM TargetQueue), TIMEOUT 5000; 
IF (@@ROWCOUNT = 0) 
    BEGIN 
     ROLLBACK TRANSACTION; 
     BREAK; 
    END 

if @MessageType = 'SenderMessageType' 
     Begin 
      SEND 
        ON CONVERSATION @ConversationHandle 
        Message Type ReceiverMessageType 
        ('Message is received') 
      END Conversation @ConversationHandle 
      insert into table_12 (dataa) values ('salam') 
     END 

Commit 


    end 
相關問題