升級複寫指令碼 (複寫 Transact-SQL 程式設計)

適用於:SQL ServerAzure SQL 受控執行個體

Transact-SQL 指令碼檔案可以用於以程式設計的方式設定複寫拓撲。 如需詳細資訊,請參閱複寫系統預存程序概念

重要

雖然並不一定要升級由 sysadmin 角色的成員所執行的指令碼,但建議您依照本主題所述修改現有的指令碼。 請根據主題< Replication Agent Security Model>的「代理程式所需的權限」一節所述,為每個複寫代理程式指定具有最小權限的帳戶。

這些安全性改進會影響現有指令碼中的下列預存程序,讓您可以明確地指定 Microsoft Windows 帳戶,並在這些帳戶下執行複寫代理程式工作,對權限可以有更多的控制:

  • sp_addpublication_snapshot:

    現在當您執行 sp_addpublication_snapshot (Transact-SQL) 時,應該以 @job_login@job_password 提供 Windows 認證,以建立散發者端執行快照集代理程式的作業。

  • sp_addpushsubscription_agent

    您現在應該執行 sp_addpushsubscription_agent (Transact-SQL) 以明確地新增作業並提供 Windows 認證 (@job_login@job_password),藉此在散發者端執行散發代理程式作業。 在 SQL Server 2005 (9.x) 之前的 SQL Server 版本中,這項作業會在建立發送訂閱時自動完成。

  • sp_addmergepushsubscription_agent

    您現在應該執行 sp_addmergepushsubscription_agent (Transact-SQL) 以明確地新增作業並提供 Windows 認證 (@job_login@job_password),藉此在散發者端執行合併代理程式作業。 在 SQL Server 2005 (9.x) 之前的 SQL Server 版本中,這項作業會在建立發送訂閱時自動完成。

  • sp_addpullsubscription_agent

    現在當您執行 sp_addpullsubscription_agent (Transact-SQL) 時,應該以 @job_login@job_password 提供 Windows 認證,以建立在訂閱者端執行散發代理程式的作業。

  • sp_addmergepullsubscription_agent

    現在當您執行 sp_addmergepullsubscription_agent (Transact-SQL) 時,應該以 @job_login@job_password 提供 Windows 認證,以建立在訂閱者端執行合併代理程式的作業。

  • sp_addlogreader_agent

    您現在應該執行 sp_addlogreader_agent (Transact-SQL) 以手動新增作業並提供 Windows 認證,藉此在散發者端執行記錄讀取器代理程式。 在 SQL Server 2005 (9.x) 之前的 SQL Server 版本中,這項作業會在建立交易式發行集時自動完成。

  • sp_addqreader_agent

    您現在應該執行 sp_addqreader_agent (Transact-SQL) 以手動新增作業並提供 Windows 認證,藉此在散發者端執行佇列讀取器代理程式。 在 SQL Server 2005 (9.x) 之前的 SQL Server 版本中,這項作業會在建立支援佇列更新的交易式發行集時自動完成。

在 SQL Server 2005 (9.x) 所引入的安全性模型中,複寫代理程式一律會使用 @job_name@job_password 中所提供的認證,與具有 Windows 驗證的 SQL Server 本機執行個體建立連線。 如需有關在執行複寫代理程式作業時所使用 Windows 帳戶之需求的資訊,請參閱< Replication Agent Security Model>。

重要

可能的話,會在執行階段提示使用者輸入安全性認證。 如果將認證儲存在指令碼檔案中,請確定該檔案本身受到安全保護。

若要升級設定快照式或交易式發行集的指令碼

  1. 在現有指令碼的 sp_addpublication (Transact-SQL) 之前,於發行集資料庫的發行者端執行 sp_addlogreader_agent (Transact-SQL)。 指定 Windows 認證,記錄讀取器代理程式執行時會將該認證用於 @job_name@job_password。 如果代理程式會在連接到發行者時使用 SQL Server 驗證,則您也必須為 @publisher_security_mode 指定值 0,並為 @publisher_login@publisher_password 指定 SQL Server 登入資訊。 如此會建立發行集資料庫的「記錄讀取器代理程式」作業。

    注意

    只有交易式發行集才需要這個步驟,快照式發行集不需要。

  2. (選擇性) 在 sp_addpublication (Transact-SQL) 之前,於散發資料庫的散發者端執行 sp_addqreader_agent (Transact-SQL)。 指定 Windows 認證,佇列讀取器代理程式執行時會將該認證用於 @job_name@job_password。 如此會為「散發者」建立「佇列讀取器代理程式」作業。

    注意

    只有支援佇列更新訂閱者的交易式發行集才需要這個步驟。

  3. (選擇性) 更新 sp_addpublication (Transact-SQL) 的執行,以針對實作新複寫功能的參數設定任何非預設的值。

  4. sp_addpublication (Transact-SQL) 之後,於發行集資料庫的發行者端執行 sp_addpublication_snapshot (Transact-SQL)。 指定 @publication 和 Windows 認證,快照集代理程式執行時會將該認證用於 @job_name@job_password。 如果代理程式會在連接到發行者時使用 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)。 指定 @publication 和 Windows 認證,快照集代理程式執行時會將該認證用於 @job_name@job_password。 如果代理程式會在連接到發行者時使用 SQL Server 驗證,則您也必須為 @publisher_security_mode 指定值 0,並為 @publisher_login@publisher_password 指定 SQL Server 登入資訊。 這麼做會為發行集建立快照集代理程式作業。

  3. (選擇性) 更新 sp_addmergearticle (Transact-SQL) 的執行,以針對實作新複寫功能的參數設定任何非預設的值。

若要升級將訂閱加入至合併式發行集的指令碼

  1. 在執行建立訂閱的預存程序之後,請務必執行建立「合併代理程式」作業的預存程序,以同步處理訂閱。 您使用的預存程序將依照訂閱類型而定。

範例

A. 用於建立交易式發行集的 SQL Server 2000 指令碼

下列的 SQL Server 2000 (8.x) 指令碼範例會針對 Product 資料表建立交易式發行集。 此發行集支援立即更新,且使用佇列更新做為容錯移轉。 為了利於閱讀,已經移除了預設的參數。

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

B. 用於建立交易式發行集的 SQL Server 2005 與更新版本指令碼

下列是前述指令碼的升級範例 (此指令碼會建立交易式發行集),能成功地執行於 SQL Server 2005 (9.x) 和更新版本。 此發行集支援立即更新,且使用佇列更新做為容錯移轉。 已明確地宣告新參數的預設值。

注意

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

C. 用於建立合併式發行集的 SQL Server 2000 指令碼

下列的 SQL Server 2000 (8.x) 指令碼範例會針對 Customers 資料表建立合併式發行集。 為了利於閱讀,已經移除了預設的參數。

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

D. 用於建立合併式發行集的 SQL Server 2005 與更新版本指令碼

下列是前述指令碼 (建立合併式發行集)的升級範例,能成功地執行於 SQL Server 2005 (9.x) 和更新版本。 已明確地宣告新參數的預設值。

注意

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

E. 用於建立目標為交易式發行集之發送訂閱的 SQL Server 2000 指令碼

下列的 SQL Server 2000 (8.x) 指令碼範例會建立交易式發行集的發送訂閱。 為了利於閱讀,已經移除了預設的參數。

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

F. 用於建立目標為交易式發行集之發送訂閱的 SQL Server 2005 與更新版本指令碼

下列是前述指令碼 (建立交易式發行集的發送訂閱) 的升級範例,能成功地執行於 SQL Server 2005 (9.x) 和更新版本。 已明確地宣告新參數的預設值。

注意

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

G. 用於建立目標為合併式發行集之發送訂閱的 SQL Server 2000 指令碼

下列的 SQL Server 2000 (8.x) 指令碼範例會建立合併式發行集的發送訂閱。 為了利於閱讀,已經移除了預設的參數。

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

H. 用於建立目標為合併式發行集之發送訂閱的 SQL Server 2005 與更新版本指令碼

下列是前述指令碼 (建立合併式發行集的發送訂閱) 的升級範例,能成功地執行於 SQL Server 2005 (9.x) 和更新版本。 已明確地宣告新參數的預設值。

注意

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

I. 用於建立目標為交易式發行集之提取訂閱的 SQL Server 2000 指令碼

下列的 SQL Server 2000 (8.x) 指令碼範例會建立交易式發行集的提取訂閱。 為了利於閱讀,已經移除了預設的參數。

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

J. 用於建立目標為交易式發行集之提取訂閱的 SQL Server 2005 與更新版本指令碼

下列是前述指令碼 (建立交易式發行集的提取訂閱) 的升級範例,能成功地執行於 SQL Server 2005 (9.x) 和更新版本。 已明確地宣告新參數的預設值。

注意

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

K. 用於建立目標為合併式發行集之提取訂閱的 SQL Server 2000 指令碼

下列的 SQL Server 2000 (8.x) 指令碼範例會建立合併式發行集的提取訂閱。 為了利於閱讀,已經移除了預設的參數。

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

L. 用於建立目標為合併式發行集之提取訂閱的 SQL Server 2005 與更新版本指令碼

下列是前述指令碼 (建立合併式發行集的提取訂閱) 的升級範例,能成功地執行於 SQL Server 2005 (9.x) 和更新版本。 已明確地宣告新參數的預設值。

注意

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
升級複寫的資料庫