如何创建请求订阅(复制 Transact-SQL 编程)

请求订阅可以使用复制存储过程以编程的方式进行创建。所用的存储过程取决于订阅所属的发布的类型。

创建快照或事务性发布的请求订阅

  1. 在发布服务器中,通过执行 sp_helppublication (Transact-SQL) 确保发布支持请求订阅。

    • 如果结果集中 allow_pull 的值为 1,则发布支持请求订阅。
    • 如果 allow_pull 的值为 0,则执行 sp_changepublication (Transact-SQL),将 @property 指定为 allow_pull,将 @value 指定为 true
  2. 在订阅服务器中,执行 sp_addpullsubscription (Transact-SQL)。指定 @publisher@publication。有关更新订阅的信息,请参阅How to: Create an Updatable Subscription to a Transactional Publication (Replication Transact-SQL Programming)

  3. 在订阅服务器中,执行 sp_addpullsubscription_agent (Transact-SQL)。指定下列各项:

    • @publisher@publisher_db@publication 参数。
    • 订阅服务器中的分发代理运行时所使用的 Microsoft Windows 凭据:@job_login@job_password
      ms147346.note(zh-cn,SQL.90).gif注意:
      使用 Windows 集成身份验证进行的连接始终使用由 @job_login@job_password 指定的 Windows 凭据。分发代理始终使用 Windows 集成身份验证与订阅服务器进行本地连接。默认情况下,该代理将使用 Windows 集成身份验证连接到分发服务器。
    • (可选)@distributor_security_mode0 值以及 Microsoft SQL Server 登录信息:@distributor_login@distributor_password,如果您需要在连接到分发服务器时使用 SQL 身份验证,则指定这些参数。
    • 该订阅的分发代理作业计划。有关详细信息,请参阅How to: Specify Synchronization Schedules (Replication Transact-SQL Programming)
  4. 在发布服务器中,执行 sp_addsubscription (Transact-SQL) 以注册请求订阅。指定 @publication@subscriber@destination_db。将 @subscription_type 的值指定为 pull

创建合并发布的请求订阅

  1. 在发布服务器中,通过执行 sp_helpmergepublication (Transact-SQL) 确保发布支持请求订阅。

    • 如果结果集中 allow_pull 的值为 1,则发布支持请求订阅。
    • 如果 allow_pull 的值为 0,则执行 sp_changemergepublication (Transact-SQL),将 @property 指定为 allow_pull,将 @value 指定为 true
  2. 在订阅服务器中,执行 sp_addmergepullsubscription (Transact-SQL)。指定 @publisher@publisher_db@publication 以及下列参数:

    • @subscriber_type – 对于客户端订阅指定 local,对于服务器订阅指定 global
    • @subscription_priority – 指定订阅的优先级(从 0.0099.99)。只有服务器订阅要求指定优先级。
      有关详细信息,请参阅高级合并复制冲突的检测和解决
  3. 在订阅服务器中,执行 sp_addmergepullsubscription_agent (Transact-SQL)。指定下列参数:

    • @publisher@publisher_db@publication
    • 订阅服务器中的合并代理运行时所使用的 Windows 凭据:@job_login@job_password
      ms147346.note(zh-cn,SQL.90).gif注意:
      使用 Windows 集成身份验证进行的连接始终使用由 @job_login@job_password 指定的 Windows 凭据。合并代理始终使用 Windows 集成身份验证与订阅服务器进行本地连接。默认情况下,该代理将使用 Windows 集成身份验证连接到分发服务器和发布服务器。
    • (可选)@distributor_security_mode0 值以及 SQL Server 登录信息:@distributor_login@distributor_password,如果您需要在连接到分发服务器时使用 SQL 身份验证,则指定这些参数。
    • (可选)@publisher_security_mode0 值以及 SQL Server 登录信息:@publisher_login@publisher_password,如果您需要在连接到发布服务器时使用 SQL 身份验证,则指定这些参数。
    • 该订阅的合并代理作业计划。有关详细信息,请参阅How to: Specify Synchronization Schedules (Replication Transact-SQL Programming)
  4. 在发布服务器中,执行 sp_addmergesubscription (Transact-SQL)。指定 @publication@subscriber@subscriber_db,并将 @subscription_type 的值指定为 pull。这样便可注册请求订阅。

示例

以下示例创建事务性发布的请求订阅。第一个批处理在订阅服务器中执行,第二个批处理在发布服务器中执行。登录名和密码在运行时使用 sqlcmd 脚本变量进行提供。

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

-- Execute this batch at the Subscriber.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @publisher = $(PubServer);
SET @publicationDB = N'AdventureWorks';

-- At the subscription database, create a pull subscription 
-- to a transactional publication.
USE [AdventureWorksReplica]
EXEC sp_addpullsubscription 
  @publisher = @publisher, 
  @publication = @publication, 
  @publisher_db = @publicationDB;

-- Add an agent job to synchronize the pull subscription.
EXEC sp_addpullsubscription_agent 
  @publisher = @publisher, 
  @publisher_db = @publicationDB, 
  @publication = @publication, 
  @distributor = @publisher, 
  @job_login = $(Login), 
  @job_password = $(Password);
GO

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

-- Execute this batch at the Publisher.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorksReplica';

-- At the Publisher, register the subscription, using the defaults.
EXEC sp_addsubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @destination_db = @subscriptionDB, 
  @subscription_type = N'pull',
  @status = N'subscribed';
GO

以下示例创建合并发布的请求订阅。第一个批处理在订阅服务器中执行,第二个批处理在发布服务器中执行。登录名和密码在运行时使用 sqlcmd 脚本变量进行提供。

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

-- Execute this batch at the Subscriber.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @hostname AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @publisher = $(PubServer);
SET @publicationDB = N'AdventureWorks';
SET @hostname = N'adventure-works\david8';

-- At the subscription database, create a pull subscription 
-- to a merge publication.
USE [AdventureWorksReplica]
EXEC sp_addmergepullsubscription 
  @publisher = @publisher, 
  @publication = @publication, 
  @publisher_db = @publicationDB;

-- Add an agent job to synchronize the pull subscription. 
EXEC sp_addmergepullsubscription_agent 
  @publisher = @publisher, 
  @publisher_db = @publicationDB, 
  @publication = @publication, 
  @distributor = @publisher, 
  @job_login = $(Login), 
  @job_password = $(Password),
  @hostname = @hostname;
GO

-- Execute this batch at the Publisher.
DECLARE @myMergePub  AS sysname;
DECLARE @mySub       AS sysname;
DECLARE @mySubDB     AS sysname;

SET @myMergePub = N'AdvWorksSalesOrdersMerge';
SET @mySub = N'MYSUBSERVER';
SET @mySubDB = N'AdventureWorksReplica';

-- At the Publisher, register the subscription, using the defaults.
USE [AdventureWorks]
EXEC sp_addmergesubscription @publication = @myMergePub, 
@subscriber = @mySub, @subscriber_db = @mySubDB, 
@subscription_type = N'pull';
GO

请参阅

任务

如何创建请求订阅(RMO 编程)
如何创建推送订阅(复制 Transact-SQL 编程)

其他资源

How to: Create a Publication (Replication Transact-SQL Programming)
How to: Delete a Pull Subscription (Replication Transact-SQL Programming)
订阅发布
将 sqlcmd 与脚本变量结合使用

帮助和信息

获取 SQL Server 2005 帮助