Share via


プル サブスクリプションを作成する方法 (レプリケーション Transact-SQL プログラミング)

レプリケーション ストアド プロシージャを使用して、プル サブスクリプションをプログラムで作成できます。使用するストアド プロシージャは、サブスクリプションが属するパブリケーションの種類によって変わります。

スナップショット パブリケーションまたはトランザクション パブリケーションに対するプル サブスクリプションを作成するには

  1. パブリッシャで、sp_helppublication (Transact-SQL) を実行して、パブリケーションがプル サブスクリプションをサポートしていることを確認します。

    • 結果セットの allow_pull の値が 1 である場合、パブリケーションはプル サブスクリプションをサポートします。

    • allow_pull の値が 0 である場合は、@propertyallow_pull を、@value に true を指定して、sp_changepublication (Transact-SQL) を実行します。

  2. サブスクライバで、sp_addpullsubscription (Transact-SQL) を実行します。@publisher@publication を指定します。サブスクリプションの更新の詳細については、「トランザクション パブリケーションに対して更新可能なサブスクリプションを作成する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

  3. サブスクライバで、sp_addpullsubscription_agent (Transact-SQL) を実行します。次の指定を行います。

    • @publisher@publisher_db、および @publication の各パラメータ。

    • @job_login および @job_password に、サブスクライバでディストリビューション エージェントを実行するときに使用する Microsoft Windows 資格情報。

      注意

      Windows 統合認証を使用して行われる接続では、常に @job_login および @job_password で指定された Windows 資格情報が使用されます。ディストリビューション エージェントは、常に Windows 統合認証を使用してサブスクライバへのローカル接続を作成します。既定では、エージェントは Windows 統合認証を使用してディストリビュータに接続します。

    • (省略可) ディストリビュータへの接続時に SQL Server 認証を使用する必要がある場合は、@distributor_security_mode0@distributor_login および @distributor_password に Microsoft SQL Server ログイン情報。

    • このサブスクリプションでのディストリビューション エージェント ジョブのスケジュール。詳細については、「同期スケジュールを指定する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

  4. パブリッシャで、sp_addsubscription (Transact-SQL) を実行してプル サブスクリプションを登録します。@publication@subscriber、および @destination_db を指定します。@subscription_type には pull を指定します。

マージ パブリケーションに対するプル サブスクリプションを作成するには

  1. パブリッシャで、sp_helpmergepublication (Transact-SQL) を実行して、パブリケーションがプル サブスクリプションをサポートしていることを確認します。

    • 結果セットの allow_pull の値が 1 である場合、パブリケーションはプル サブスクリプションをサポートします。

    • allow_pull の値が 0 である場合は、@propertyallow_pull を、@value に true を指定して、sp_changemergepublication (Transact-SQL) を実行します。

  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 の各パラメータ。

    • @job_login および @job_password に、サブスクライバでマージ エージェントを実行するときに使用する Windows 資格情報。

      注意

      Windows 統合認証を使用して行われる接続では、常に @job_login および @job_password で指定された Windows 資格情報が使用されます。マージ エージェントは、常に Windows 統合認証を使用してサブスクライバへのローカル接続を作成します。既定では、エージェントは Windows 統合認証を使用してディストリビュータおよびパブリッシャに接続します。

    • (省略可) ディストリビュータへの接続時に SQL Server 認証を使用する必要がある場合は、@distributor_security_mode0@distributor_login および @distributor_password に SQL Server ログイン情報。

    • (省略可) パブリッシャへの接続時に SQL Server 認証を使用する必要がある場合は、@publisher_security_mode0@publisher_login および @publisher_password に SQL Server ログイン情報。

    • このサブスクリプションでのマージ エージェント ジョブのスケジュール。詳細については、「同期スケジュールを指定する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

  4. パブリッシャで、sp_addmergesubscription (Transact-SQL) を実行します。@publication@subscriber@subscriber_db を指定し、@subscription_typepull を指定します。これにより、プル サブスクリプションが登録されます。

使用例

次の例では、トランザクション パブリケーションに対するプル サブスクリプションを作成します。最初のバッチはサブスクライバで実行され、2 番目のバッチはパブリッシャで実行されます。ログインとパスワードの値は、実行時に 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'AdventureWorks2008R2';

-- At the subscription database, create a pull subscription 
-- to a transactional publication.
USE [AdventureWorks2008R2Replica]
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'AdventureWorks2008R2Replica';

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

次の例では、マージ パブリケーションに対するプル サブスクリプションを作成します。最初のバッチはサブスクライバで実行され、2 番目のバッチはパブリッシャで実行されます。ログインとパスワードの値は、実行時に 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'AdventureWorks2008R2';
SET @hostname = N'adventure-works\david8';

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

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