次の方法で共有


レプリケーション スクリプトのアップグレード (レプリケーション Transact-SQL プログラミング)

Transact-SQL スクリプト ファイルを使用して、レプリケーション トポロジをプログラムで構成できます。 詳細については、「Replication System Stored Procedures Concepts」 (レプリケーション システム ストアド プロシージャの概念) を参照してください。

重要

sysadmin ロールのメンバーが実行するスクリプトについては、アップグレードは必須ではありませんが、このトピックの説明に従って既存のスクリプトを変更することをお勧めします。 「 Replication Agent Security Model」の「エージェントに必要な権限」に記載されている各レプリケーション エージェントの最小権限を持つアカウントを指定します。

このようにセキュリティが強化されたことで、レプリケーション エージェント ジョブの実行に使用される Microsoft Windows アカウントを明示的に指定できるため、権限をより詳細に設定できます。既存のスクリプトに含まれる次のストアド プロシージャは、このセキュリティ強化の影響を受けます。

  • sp_addpublication_snapshot

    sp_addpublication_snapshot (Transact-SQL) を実行するときに、@job_loginとして Windows 資格情報を指定し、@job_passwordして、ディストリビューターでスナップショット エージェントが実行されるジョブを作成する必要があります。

  • sp_addpushsubscription_agent

    ここで、sp_addpushsubscription_agent (Transact-SQL) を実行してジョブを明示的に追加し、ディストリビューション エージェント ジョブがディストリビューターで実行される Windows 資格情報 (@job_login@job_password) を指定する必要があります。 2005 年SQL Serverより前のバージョンのSQL Serverでは、これはプッシュ サブスクリプションが作成されたときに自動的に行われました。

  • sp_addmergepushsubscription_agent

    ここで、sp_addmergepushsubscription_agent (Transact-SQL) を実行してジョブを明示的に追加し、マージ エージェント ジョブがディストリビューターで実行される Windows 資格情報 (@job_login@job_password) を指定する必要があります。 2005 年SQL Serverより前のバージョンのSQL Serverでは、これはプッシュ サブスクリプションが作成されたときに自動的に行われました。

  • sp_addpullsubscription_agent

    sp_addpullsubscription_agent (Transact-SQL) を実行するときに、@job_loginとして Windows 資格情報を指定し、@job_passwordして、サブスクライバーでディストリビューション エージェントが実行されるジョブを作成する必要があります。

  • sp_addmergepullsubscription_agent

    sp_addmergepullsubscription_agent (Transact-SQL) を実行するときに、@job_loginとして Windows 資格情報を指定し、@job_passwordして、サブスクライバーでマージ エージェントが実行されるジョブを作成する必要があります。

  • sp_addlogreader_agent

    ここで、sp_addlogreader_agent (Transact-SQL) を実行してジョブを手動で追加し、ディストリビューターでログ リーダー エージェントが実行される Windows 資格情報を指定する必要があります。 2005 年SQL Serverより前のバージョンの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 アカウントの要件の詳細については、「 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. サブスクリプションを作成するストアド プロシージャを実行した後、マージ エージェント ジョブを作成するストアド プロシージャを実行して、サブスクリプションを同期するようにしてください。 使用するストアド プロシージャは、サブスクリプションの種類に応じて変わります。

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

2005 以降のバージョンで正常に実行されるようにアップグレードされたマージ パブリケーションを作成する前のスクリプトの例SQL Server次に示します。 新しいパラメーターの既定値は、明示的に宣言されています。

注意

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

参照

パブリケーションを作成する
プッシュ サブスクリプションを作成する
Create a Pull Subscription
レプリケーションのセキュリティ設定の表示および変更
MSSQL_ENG021797
MSSQL_ENG021798
Replication System Stored Procedures Concepts
レプリケートされたデータベースのアップグレード