升级复制脚本(复制 Transact-SQL 编程)

Transact-SQL 脚本文件可用于以编程方式配置复制拓扑。 有关详细信息,请参阅复制系统存储过程概念

重要

虽然不需要升级由 sysadmin 角色的成员执行的脚本,我们仍建议您按照本主题中的说明修改现有脚本。 按照 Replication Agent Security Model主题的“代理所需权限”部分的说明为每个复制代理指定一个具有最低权限的帐户。

这些安全改进允许您显式指定用于执行复制代理作业的 Microsoft Windows 帐户,从而可对权限进行更多控制,这些安全改进会影响现有脚本中的以下存储过程:

  • sp_addpublication_snapshot

    现在,在执行sp_addpublication_snapshot (Transact-SQL) 创建在分发服务器上运行快照代理的作业时,应提供 Windows 凭据作为@job_login和@job_password。

  • sp_addpushsubscription_agent

    现在,应执行 sp_addpushsubscription_agent (Transact-SQL) 显式添加作业,并提供 Windows 凭据 (@job_login@job_password) ,分发代理作业在分发服务器上运行。 在 SQL Server 2005 之前的 SQL Server 版本中,创建推送订阅时会自动完成此操作。

  • sp_addmergepushsubscription_agent

    现在应执行 sp_addmergepushsubscription_agent (Transact-SQL) 显式添加作业,并提供 Windows 凭据 (@job_login@job_password) ,合并代理作业在分发服务器上运行。 在 SQL Server 2005 之前的 SQL Server 版本中,创建推送订阅时会自动完成此操作。

  • sp_addpullsubscription_agent

    现在,在执行sp_addpullsubscription_agent (Transact-SQL) 创建在订阅服务器上运行分发代理的作业时,应提供 Windows 凭据作为@job_login和@job_password。

  • sp_addmergepullsubscription_agent

    现在,在执行 sp_addmergepullsubscription_agent (Transact-SQL) 创建在订阅服务器上运行合并代理的作业时,应提供 Windows 凭据作为@job_login和@job_password。

  • sp_addlogreader_agent

    现在应执行 sp_addlogreader_agent (Transact-SQL) 手动添加作业并提供在分发服务器上运行日志读取器代理时所依据的 Windows 凭据。 在 SQL Server 2005 之前的 SQL Server 版本中,这在创建事务发布时自动完成。

  • sp_addqreader_agent

    现在应执行 sp_addqreader_agent (Transact-SQL) 手动添加作业并提供队列读取器代理在分发服务器上运行时所依据的 Windows 凭据。 在 2005 SQL Server 之前的 SQL Server 版本中,当创建支持排队更新的事务发布时,会自动完成此操作。

在 SQL Server 2005 中引入的安全模型中,复制代理始终使用 @job_name 和 @job_password 中提供的凭据通过 Windows 身份验证连接到本地 SQL Server 实例。 有关运行复制代理作业时所使用的 Windows 帐户的要求,请参阅 Replication Agent Security Model

重要

如果可能,请在运行时提示用户输入安全凭据。 如果将凭据保存在脚本文件中,请确保该文件本身受到安全保护。

升级用于配置快照发布或事务发布的脚本

  1. 在现有脚本中, 在 sp_addpublication (Transact-SQL) 之前,在发布服务器上对发布数据库执行 sp_addlogreader_agent (Transact-SQL) 。 为 @job_name和@job_password 指定运行日志读取器代理时所依据 Windows 凭据。 如果代理在连接到发布服务器时将使用SQL Server身份验证,则还必须将 @publisher_security_mode 的值指定为 0,并为@publisher_login和@publisher_password指定SQL Server登录信息。 这样,便为发布数据库创建了一个日志读取器代理作业。

    注意

    此步骤仅针对事务发布,无需对快照发布执行该步骤。

  2. (可选) 在 sp_addpublication (Transact-SQL) 之前,在分发服务器上对分发数据库执行 sp_addqreader_agent (Transact-SQL) 。 为 @job_name@job_password指定运行队列读取器代理时所依据的 Windows 凭据。 这样,便为分发服务器创建了一个队列读取器代理作业。

    注意

    仅需要对支持排队更新订阅服务器的事务发布执行此步骤。

  3. (可选) 更新 sp_addpublication (Transact-SQL) 的执行,为实现新复制功能的参数设置任何非默认值。

  4. sp_addpublication (Transact-SQL) 后,在发布服务器上对发布数据库执行 sp_addpublication_snapshot (Transact-SQL) 。 为@job_name和@job_password指定运行快照代理所依据的@publicationWindows 凭据。 如果代理在连接到发布服务器时将使用SQL Server身份验证,则还必须将 @publisher_security_mode 的值指定为 0,并为@publisher_login和@publisher_password指定SQL Server登录信息。 此操作将为发布创建一个快照代理作业。

  5. (可选) 更新 sp_addarticle (Transact-SQL) 的执行,为实现新复制功能的参数设置任何非默认值。

升级向快照发布或事务发布添加订阅的脚本

  1. 在执行用于创建订阅的存储过程之后,请确保执行用于创建分发代理作业的存储过程以同步订阅。 所用的存储过程将取决于订阅类型。

升级用于配置合并发布的脚本

  1. (可选) 在现有脚本中,更新 sp_addmergepublication (Transact-SQL) 的执行,以便为实现新复制功能的参数设置任何非默认值。

  2. sp_addmergepublication (Transact-SQL) 后,在发布服务器上对发布数据库执行 sp_addpublication_snapshot (Transact-SQL) 。 为@job_name和@job_password指定运行快照代理所依据的@publicationWindows 凭据。 如果代理在连接到发布服务器时将使用SQL Server身份验证,则还必须将 @publisher_security_mode 的值指定为 0,并为@publisher_login和@publisher_password指定SQL Server登录信息。 此操作将为发布创建一个快照代理作业。

  3. (可选) 更新 sp_addmergearticle (Transact-SQL) 的执行,为实现新复制功能的参数设置任何非默认值。

升级向合并发布添加订阅的脚本

  1. 执行完用于创建订阅的存储过程后,请确保执行用于创建合并代理作业的存储过程以同步订阅。 所用的存储过程将取决于订阅类型。

示例

下面是为 Product 表创建事务发布的 SQL Server 2000 脚本的示例。 此发布支持以排队更新作为故障转移的立即更新。 为了方便阅读,删除了默认参数。

USE [Northwind]
GO

DECLARE @publication AS sysname
DECLARE @publicationDB AS sysname
DECLARE @article AS sysname
SET @publication = N'NwdProductTran'
SET @publicationDB = N'Northwind'
SET @article = N'Products'

-- Enable the replication database.
EXEC sp_replicationdboption 
    @dbname = @publicationDB, 
    @optname = N'publish', 
    @value = N'true'

-- Add the transactional publication.
EXEC sp_addpublication 
    @publication = @publication, 
    @sync_method = N'native', 
    @status = N'active', 
    @repl_freq = N'continuous', 
    @description = N'Transactional publication of Northwind.', 
    @allow_push = N'true', 
    @allow_pull = N'true', 
    @allow_sync_tran = N'true', 
    @autogen_sync_procs = N'true', 
    @allow_queued_tran = N'true'

-- Add a snapshot job.
EXEC sp_addpublication_snapshot 
    @publication = @publication

-- Add the transactional articles.
EXEC sp_addarticle 
    @publication = @publication, 
    @article = @article, 
    @source_owner = N'dbo', 
    @source_object = @article, 
    @destination_table = @article, 
    @type = N'logbased', 
    @schema_option = 0x00000000000080F3, 
    @ins_cmd = N'CALL sp_MSins_Products', 
    @del_cmd = N'XCALL sp_MSdel_Products', 
    @upd_cmd = N'XCALL sp_MSupd_Products', 
    @auto_identity_range = N'false'
GO

示例

下面是升级上一个脚本的示例,该脚本创建事务发布,以成功运行 SQL Server 2005 及更高版本。 此发布支持以排队更新作为故障转移的立即更新。 在此示例中显式声明了新参数的默认值。

注意

Windows 凭据是在运行时使用 sqlcmd 脚本变量来提供的。

-- To avoid storing the login and password in the script file, the value 
-- is passed into SQLCMD as a scripting variable. 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 at the Distributor.
USE [distribution]

DECLARE @login AS sysname;
DECLARE @password AS sysname;
-- Specify the Windows account to run the Queue Reader Agent.
SET @login = $(Login); 
-- Pass the password at runtime.
SET @password = $(Password); 

-- Execute sp_addqreader_agent to create the Queue Reader Agent job. 
EXEC sp_addqreader_agent 
    @job_login = @login, 
    @job_password = @password;
GO

-- Execute at the Publisher.
USE [Northwind]
GO

DECLARE @publication AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @article AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdProductTran';
SET @publicationDB = N'Northwind';
SET @article = N'Products';
-- Specify the Windows account to run the Log Reader and Snapshot Agents.
SET @login = $(Login); 
-- Pass the password at runtime.
SET @password = $(Password); 

-- Enable the replication database.
EXEC sp_replicationdboption 
    @dbname = @publicationDB, 
    @optname = N'publish', 
    @value = N'true';

-- Execute sp_addlogreader_agent to create the agent job. 
EXEC sp_addlogreader_agent 
    @job_login = @login, 
    @job_password = @password, 
    -- Explicitly specify the use of Windows Integrated Authentication (default) 
    -- when connecting to the Publisher.
    @publisher_security_mode = 1;

-- Add the transactional publication.
EXEC sp_addpublication 
    @publication = @publication, 
    @sync_method = N'native', 
    @repl_freq = N'continuous', 
    @status = N'active',
    @description = N'Transactional publication of Northwind.', 
    @allow_push = N'true', 
    @allow_pull = N'true', 
    @allow_sync_tran = N'true', 
    @autogen_sync_procs = N'true', 
    @allow_queued_tran = N'true',
    @replicate_ddl = 1,
    @enabled_for_p2p = N'false';

-- Create a new snapshot job for the publication, using a default schedule.
EXEC sp_addpublication_snapshot 
    @publication = @publication, 
    @job_login = @login, 
    @job_password = @password,
    -- Explicitly specify the use of Windows Integrated Authentication (default) 
    -- when connecting to the Publisher.
    @publisher_security_mode = 1;

-- Add a transactional article.
EXEC sp_addarticle 
  @publication = @publication, 
  @article = @article, 
  @source_owner = N'dbo', 
  @source_object = @article, 
  @destination_table = @article, 
  @type = N'logbased', 
  @schema_option = 0x00000000000080F3, 
  @ins_cmd = N'CALL sp_MSins_Products', 
  @del_cmd = N'XCALL sp_MSdel_Products', 
  @upd_cmd = N'XCALL sp_MSupd_Products', 
  @auto_identity_range = N'false',
  @identityrangemanagementoption = N'manual',
  @fire_triggers_on_snapshot = N'false';
GO

示例

下面是为 Customers 表创建合并发布的 SQL Server 2000 脚本的示例。 为了方便阅读,删除了默认参数。

-- Enable the replication database.
USE [Northwind]
GO

DECLARE @publicationDB AS sysname
DECLARE @publication AS sysname
DECLARE @article AS sysname
SET @publicationDB = N'Northwind' 
SET @publication = N'NwdCustomersMerge' 
SET @article = N'Customers' 

EXEC sp_replicationdboption 
    @dbname = @publicationDB, 
    @optname = N'merge publish', 
    @value = N'true'

-- Add the merge publication.
EXEC sp_addmergepublication 
    @publication = @publication, 
    @description = N'Merge publication of Northwind.', 
    @retention = 14, 
    @sync_mode = N'native', 
    @centralized_conflicts = N'true', 
    @dynamic_filters = N'false', 
    @keep_partition_changes = N'false'
 
EXEC sp_addpublication_snapshot 
    @publication = @publication

-- Add the merge articles.
EXEC sp_addmergearticle 
    @publication = @publication, 
    @article = @article, 
    @source_owner = N'dbo', 
    @source_object = @article, 
    @type = N'table', 
    @description = null, 
    @column_tracking = N'true', 
    @schema_option = 0x000000000000CFF1
 GO

示例

下面是上一个脚本的示例,该脚本创建合并发布,该脚本已升级为成功运行 SQL Server 2005 及更高版本。 在此示例中显式声明了新参数的默认值。

注意

Windows 凭据是在运行时使用 sqlcmd 脚本变量来提供的。

-- To avoid storing the login and password in the script file, the value 
-- is passed into SQLCMD as a scripting variable. 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".

-- Enabling the replication database
-- Enable the replication database.
USE [Northwind]
GO

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @article AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'Northwind';
SET @publication = N'NwdCustomersMerge';
SET @article = N'Customers';
-- Specify the Windows account to run the Snapshot Agent.
SET @login = $(Login); 
-- Supply the password at runtime.
SET @password = $(Password); 

EXEC sp_replicationdboption 
    @dbname = @publicationDB, 
    @optname = N'merge publish', 
    @value = N'true';

-- Add the merge publication.
EXEC sp_addmergepublication 
    @publication = @publication, 
    @description = N'Merge publication of Northwind.', 
    @retention = 14, 
    @sync_mode = N'native', 
    @dynamic_filters = N'false', 
    @keep_partition_changes = N'false',
    -- Only set to '90RTM' if all Subscribers are SQL Server 2005.
    @publication_compatibility_level = N'90RTM',
    @replicate_ddl = 1,
    @allow_subscriber_initiated_snapshot = N'true',
    @allow_web_synchronization = N'false',
    @allow_partition_realignment = N'true',
    @retention_period_unit = N'day',
    @automatic_reinitialization_policy = 0,
    @conflict_logging = N'both';
 
EXEC sp_addpublication_snapshot 
    @publication = @publication, 
    @job_login = @login,
    @job_password = @password;

-- Add the merge article.
EXEC sp_addmergearticle 
    @publication = @publication, 
    @article = @article, 
    @source_owner = N'dbo', 
    @source_object = @article, 
    @type = N'table', 
    @description = null, 
    @column_tracking = N'true', 
    @schema_option = 0x0000000000034FD1,
    @partition_options = 0,
    @subscriber_upload_options = 0,
    @identityrangemanagementoption = N'manual',
    @delete_tracking = N'true',
    @compensate_for_errors = N'false',
    @stream_blob_columns = N'true';
GO

示例

下面是创建事务发布的推送订阅的 SQL Server 2000 脚本的示例。 为了方便阅读,删除了默认参数。

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'NwdProductTran' 
SET @subscriber = N'MYSUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica' 

-- Add a Subscriber, using the defaults.
USE [master]
EXEC sp_addsubscriber 
    @subscriber = @subscriber

-- Add a push subscription to a transactional publication.
USE [Northwind]
EXEC sp_addsubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @destination_db = @subscriptionDB, 
    @subscription_type = N'push'
GO

示例

下面是上一个脚本的示例,该脚本创建对事务发布的推送订阅,该脚本已升级为成功运行 SQL Server 2005 及更高版本。 在此示例中显式声明了新参数的默认值。

注意

Windows 凭据是在运行时使用 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".

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdProductTran'; 
SET @subscriber = $(Subscriber); 
SET @subscriptionDB = N'NorthwindReplica'; 
-- Specify the Windows account to run the Distribution Agent.
SET @login = $(Login); 
-- Supply the password at runtime.
SET @password = $(Password); 

-- Add a push subscription to a transactional publication.
USE [Northwind]
EXEC sp_addsubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @destination_db = @subscriptionDB, 
    @subscription_type = N'push';

-- Add an agent job to synchronize the push subscription.
EXEC sp_addpushsubscription_agent 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @job_login = @login, 
    @job_password = @password;
GO

示例

下面是创建合并发布的推送订阅的 SQL Server 2000 脚本的示例。 为了方便阅读,删除了默认参数。

DECLARE @publication AS sysname
DECLARE @subscriber AS sysname
DECLARE @subscriptionDB AS sysname
SET @publication = N'NwdCustomersMerge' 
SET @subscriber = N'SUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica' 

-- Add a Subscriber, using the defaults.
USE [master]
EXEC sp_addsubscriber 
  @subscriber = @subscriber

-- Add a push subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'push',
  @subscriber_type = N'local',
  @sync_type = N'automatic'
GO

示例

下面是上一个脚本的示例,该脚本创建合并发布的推送订阅,并升级为成功运行 SQL Server 2005 及更高版本。 在此示例中显式声明了新参数的默认值。

注意

Windows 凭据是在运行时使用 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".

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdCustomersMerge'; 
SET @subscriber = $(Subscriber); 
SET @subscriptionDB = N'NorthwindReplica'; 
-- Specify the Windows account to run the Merge Agent.
SET @login = $(Login); 
-- Supply the password at runtime.
SET @password = $(Password); 

-- Add a push subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @subscription_type = N'push';

-- Add an agent job to synchronize the push subscription.
EXEC sp_addmergepushsubscription_agent 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @job_login = @login, 
    @job_password = @password;
GO

示例

下面是创建事务发布的请求订阅的 SQL Server 2000 脚本的示例。 为了方便阅读,删除了默认参数。

DECLARE @publication AS sysname
DECLARE @subscriber AS sysname
DECLARE @subscriptionDB AS sysname
SET @publication = N'NwdCustomersMerge' 
SET @subscriber = N'SUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica' 

-- Add a Subscriber, using the defaults.
USE [master]
EXEC sp_addsubscriber 
  @subscriber = @subscriber

-- Add a push subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'push',
  @subscriber_type = N'local',
  @sync_type = N'automatic'
GO

示例

下面是上一个脚本的示例,该脚本创建事务发布的请求订阅,该脚本已升级为成功运行 SQL Server 2005 及更高版本。 在此示例中显式声明了新参数的默认值。

注意

Windows 凭据是在运行时使用 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 at the Subscriber.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdProductTran'; 
SET @publisher = $(Publisher); 
SET @publicationDB = N'Northwind'; 
-- Specify the Windows account to run the Distribution Agent.
SET @login = $(Login); 
-- Supply the password at runtime.
SET @password = $(Password); 

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

-- 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

-- Execute at the Publisher.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'NwdProductTran'; 
SET @subscriber = $(Subscriber); 
SET @subscriptionDB = N'NorthwindReplica'; 

-- Add a pull subscription to a transactional publication.
USE [Northwind]
EXEC sp_addsubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @destination_db = @subscriptionDB, 
    @subscription_type = N'pull';
GO

示例

下面是创建合并发布的请求订阅的 SQL Server 2000 脚本的示例。 为了方便阅读,删除了默认参数。

-- Execute at the Subscriber
DECLARE @publication AS sysname
DECLARE @publisher AS sysname
DECLARE @publicationDB AS sysname
DECLARE @subscriber AS sysname
DECLARE @subscriptionDB AS sysname
SET @publication = N'NwdCustomersMerge' 
SET @publisher = N'PUBSERVER' 
SET @publicationDB = N'Northwind' 
SET @subscriber = N'SUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica'

-- At the subscription database, create a pull subscription 
-- to a merge publication.
USE [NorthwindReplica]
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, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @distributor = @publisher
GO

-- Execute at the Publisher.
DECLARE @publication AS sysname
DECLARE @subscriber AS sysname
DECLARE @subscriptionDB AS sysname
SET @publication = N'NwdCustomersMerge' 
SET @subscriber = N'MYSUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica' 

-- Add a Subscriber, using the defaults.
USE [master]
EXEC sp_addsubscriber 
  @subscriber = @subscriber

-- Add a pull subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'pull',
  @subscriber_type = N'local',
  @sync_type = N'automatic'
GO

示例

下面是上一个脚本的示例,该脚本创建合并发布的请求订阅,并升级为成功运行 SQL Server 2005 及更高版本。 在此示例中显式声明了新参数的默认值。

注意

Windows 凭据是在运行时使用 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 at the Subscriber
DECLARE @publication  AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdCustomersMerge'; 
SET @publisher = $(Publisher); 
SET @publicationDB = N'Northwind'; 
-- Specify the Windows account to run the Merge Agent.
SET @login = $(Login); 
-- Pass the password at runtime.
SET @password = $(Password); 

-- At the subscription database, create a pull subscription 
-- to a merge publication.
USE [NorthwindReplica]
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;
GO

-- Execute at the Publisher.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'NwdCustomersMerge';
SET @subscriber = $(Subscriber);
SET @subscriptionDB = N'NorthwindReplica';

-- Add a pull subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'pull',
  @subscriber_type = N'local',
  @sync_type = N'automatic';
GO

另请参阅

创建发布
创建推送订阅
创建请求订阅
查看和修改复制安全设置
MSSQL_ENG021797
MSSQL_ENG021798
Replication System Stored Procedures Concepts
升级复制的数据库