共用方式為


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

Transact-SQL 指令碼檔案可以用於以程式設計的方式設定複寫拓撲。如需詳細資訊,請參閱<複寫系統預存程序概念>。如果您是 SQL Server 2005 中的 sysadmin 固定伺服器角色的成員,而且執行從 SQL Server 2000 建立的複寫指令碼,則指令碼可正確執行。如果您是 db_owner 固定資料庫角色或另一個角色的成員,則指令碼會失敗,發生錯誤 MSSQL_ENG021797MSSQL_ENG021798,必須升級才行。

安全性注意事項安全性注意事項

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

這些安全性改進會影響現有指令碼中的下列預存程序,讓您可以明確地指定 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 之前的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) 時,應該以 @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 之前的SQL Server 版本中,這項作業會在建立交易式發行集時自動完成。

  • sp_addqreader_agent

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

在 SQL Server 2005 所導入的安全性模型中,複寫代理程式一定會使用 @job_name@job_password 所提供的認證,與具有「Windows 驗證」的 SQL Server 執行個體建立連接。如需有關在執行複寫代理程式作業時所使用 Windows 帳戶之需求的資訊,請參閱<複寫代理程式安全性模型>。

安全性注意事項安全性注意事項

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

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

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

    [!附註]

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

  2. (選擇性) 在散發資料庫的「散發者」端先執行 sp_addqreader_agent (Transact-SQL),再執行 sp_addpublication (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. (選擇性) 在現有的指令碼中,更新 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) 的執行,以針對實作新複寫功能的參數設定預設以外的值。

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

範例

下列的 SQL Server 2000 指令碼範例會針對 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

下列是前述指令碼的升級範例 (此指令碼會建立交易式發行集),能成功地執行於 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

下列的 SQL Server 2000 指令碼範例會針對 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

下列是前述指令碼的升級範例 (此指令碼會建立合併式發行集),能成功地執行於 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