在我的应用程序中,我需要异步调用存储过程。 为此,我使用Sql Service Broker。 这些步骤涉及创建异步调用。异步调用存储过程使用服务代理
1)我创建了Message,Contract,Queue,Service。 并发送messages.I可以在'ReceiveQueue1'中看到我的消息。
2)我创建了一个存储过程和队列 当我执行存储过程(proc_AddRecord)时,它只执行一次。 它读取队列中的所有记录并将这些记录添加到表中。 012pt高兴这一点它的工作正常。 但是,当我向'ReceiveQueue1'添加一些新消息时,我的存储过程不会自动将这些 记录添加到表中。我必须重新执行存储过程(proc_AddRecord) 以添加新消息。为什么Stored proc没有被执行。 我应该这样做,以便异步调用存储过程。 使用Service Broker的重点是异步调用存储过程。 我完全不熟悉SQL Server Service Broker。 感谢任何帮助。 这里是我的存储过程
#--exec proc_AddRecord
ALTER PROCEDURE proc_AddRecord
AS
Declare
@Conversation UniqueIdentifier,
@msgTypeName nvarchar(200),
@msg varbinary(max)
While (1=1)
Begin
Begin Transaction;
WAITFOR
(
Receive Top (1)
@Conversation = conversation_handle,
@msgTypeName = message_type_name,
@msg = message_body
from dbo.ReceiveQueue1
), TIMEOUT 5000
IF @@Rowcount = 0
Begin
Rollback Transaction
Break
End
PRINT @msg
If @msg = 'Sales'
BEGIN
insert into TableCity(deptNo,Manager,Group,EmpCount) VALUES(101,'Reeves',51, 29)
COMMIT Transaction
Continue
End
If @msg = 'HR'
BEGIN
insert into TableCity(deptNo,Manager,Group,EmpCount) VALUES(102,'Cussac',55, 14)
COMMIT Transaction
Continue
End
Begin
Print 'Process end of dialog messages here.'
End Conversation @Conversation
Commit Transaction
Continue
End
Rollback Transaction
END
ALTER QUEUE AddRecorQueue
WITH ACTIVATION (
PROCEDURE_NAME=proc_AddRecord,
MAX_QUEUE_READERS = 1,
STATUS = ON,
EXECUTE AS 'dbo');
非常好,我添加了激活到''ReceiveQueue1',它的工作完美。我在最后删除了回滚。谢谢你的帮助。 – Henry