【问题标题】:insert data into queue in sql server service broker将数据插入到 sql server 服务代理中的队列中
【发布时间】:2012-09-04 16:45:00
【问题描述】:

我正在尝试将数据插入队列。存储过程 fire_event 将用于此目的。每当调用此存储过程时,它应该将该数据插入队列中。下面是将从存储过程传递的查询和变量。有人可以告诉我如何使用这个存储过程在 SQL Server 的队列中插入数据。我想通过将其直接插入队列来替换插入表 event_type 的操作。谢谢

BEGIN

INSERT event_type
VALUES (@p_message_id,@p_event_type,@p_classifier,
        @p_event_time,@p_correlation_id,@p_person_id,@p_channel_id,
        @p_source_address_id,@p_agent_user,
        @p_agent_channel_id,@p_device_os,@p_device_os_version,
        @p_device_manufacturer,@p_device_model,@p_product_id,
        @p_event_source,@p_event_version,
        @p_node_id,@p_user_agent_string,@p_event_data)

END 

【问题讨论】:

    标签: sql sql-server-2008 service-broker


    【解决方案1】:

    如果你真的是说 Service Broker,那么你应该使用SEND 命令。

    例如,Service Broker 对象:

    Create Queue MyTableQueue;
    Create Service MyTableService On Queue MyTableQueue([DEFAULT])
    Create Queue  ProcessQueue;
    Create Service ProcessService On Queue ProcessQueue([DEFAULT])
    

    发送消息:

    Declare @h UniqueIdentifier;
    Declare @doc xml;
    
    Set @doc =
    (
        Select 'Hello' Msg
        For XML Raw, Elements, Type, Root('Data')
    );
    Begin Dialog Conversation @h
    From Service MyTableService
        To Service 'ProcessService'
    With Encryption = OFF;
    
    Send On Conversation @h(@doc)
    

    或者在你的情况下(+列别名):

    Declare @h UniqueIdentifier;
    Declare @doc xml;
    
    Set @doc =
    (
        Select @p_message_id,@p_event_type,@p_classifier,@p_event_time,@p_correlation_id,@p_person_id,@p_channel_id,@p_source_address_id,@p_agent_user,
               @p_agent_channel_id,@p_device_os,@p_device_os_version,@p_device_manufacturer,@p_device_model,@p_product_id,@p_event_source,@p_event_version,
               @p_node_id,@p_user_agent_string,@p_event_data
        For XML Raw, Elements, Type, Root('Data')
    );
    
    Begin Dialog Conversation @h
    From Service MyTableService
        To Service 'ProcessService'
    With Encryption = OFF;
    
    Send On Conversation @h(@doc)
    

    【讨论】:

    • Janis- 你能告诉我如何将多列插入队列吗?将数据插入队列的存储过程如下所示
    • 您不能“插入多行”。您可以发送将显示在队列中的消息。消息可以是 xml,包含您想要的任何内容.. 或二进制数据。
    【解决方案2】:
    CREATE PROCEDURE [dwhuser].[put_event] 
       @p_message_id            nvarchar(256),
       @p_event_type            nvarchar(256),
       @p_classifier             nvarchar(256),
       @p_event_time             datetime,
       @p_correlation_id         nvarchar(256),
       @p_person_id              nvarchar(256),
       @p_channel_id             nvarchar(256),
       @p_source_address_id      nvarchar(256),
       @p_agent_user             nvarchar(256),
       @p_agent_channel_id       nvarchar(256),
       @p_device_os              nvarchar(256),
       @p_device_os_version      nvarchar(256),
       @p_device_manufacturer    nvarchar(256),
       @p_device_model           nvarchar(256),
       @p_product_id             nvarchar(256),
       @p_event_source           nvarchar(256),
       @p_event_version          nvarchar(256),
       @p_node_id                nvarchar(256),
       @p_user_agent_string      nvarchar(2000),
       @p_event_data             VARBINARY(MAX)
    
    AS
      BEGIN
    BEGIN TRY
       INSERT [aq_event_type]
       VALUES (@p_message_id,@p_event_type,@p_classifier,@p_event_time,@p_correlation_id,@p_person_id,@p_channel_id,@p_source_address_id,@p_agent_user,
               @p_agent_channel_id,@p_device_os,@p_device_os_version,@p_device_manufacturer,@p_device_model,@p_product_id,@p_event_source,@p_event_version,
               @p_node_id,@p_user_agent_string,@p_event_data)
    
    
    END TRY
    BEGIN CATCH    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多