このトピックでは、SQL Server Management Studio、Transact-SQL、またはレプリケーション管理オブジェクト (RMO) を使用して、SQL Server 2014 でプル サブスクリプションを削除する方法について説明します。
このトピックについて
プル サブスクリプションを削除するには、次のコマンドを使用します。
SQL Server Management Studio の使用
パブリッシャー (SQL Server Management Studio の ローカル パブリケーション フォルダーから) またはサブスクライバー ( ローカル サブスクリプション フォルダーから) でプル サブスクリプションを削除します。 サブスクリプションを削除しても、サブスクリプションからオブジェクトやデータは削除されません。手動で削除する必要があります。
パブリッシャーでプル サブスクリプションを削除するには
SQL Server Management Studio でパブリッシャーに接続し、サーバー ノードを展開します。
[レプリケーション] フォルダーを展開し、[ローカル パブリケーション] フォルダーを展開します。
削除するサブスクリプションに関連付けられているパブリケーションを展開します。
サブスクリプションを右クリックし、[削除] をクリック します。
確認ダイアログ ボックスで、サブスクライバーに接続してサブスクリプション情報を削除するかどうかを選択します。 [ サブスクライバーに接続 ] チェック ボックスをオフにした場合は、後でサブスクライバーに接続して情報を削除する必要があります。
サブスクライバーでプル サブスクリプションを削除する方法
SQL Server Management Studio でサブスクライバーに接続し、サーバー ノードを展開します。
[レプリケーション] フォルダーを展開し、[ローカル サブスクリプション] フォルダーを展開します。
削除するサブスクリプションを右クリックし、[削除] をクリック します。
確認ダイアログ ボックスで、パブリッシャーに接続してサブスクリプション情報を削除するかどうかを選択します。 [ パブリッシャーに接続 ] チェック ボックスをオフにした場合は、後でパブリッシャーに接続して情報を削除する必要があります。
Transact-SQL の使用
プル サブスクリプションは、レプリケーション ストアド プロシージャを使用してプログラムで削除できます。 使用されるストアド プロシージャは、サブスクリプションが属するパブリケーションの種類によって異なります。
スナップショット パブリケーションまたはトランザクション パブリケーションのプルサブスクリプションを削除する方法
サブスクリプション データベースのサブスクライバーで、 sp_droppullsubscription (Transact-SQL) を実行します。 @publication、@publisher、および@publisher_dbを指定します。
パブリッシャー側のパブリケーション データベースで、 sp_dropsubscription (Transact-SQL) を実行します。 @publicationと@subscriberを指定します。 @articleの all の値を指定します。 (省略可能)ディストリビューターにアクセスできない場合は、ディストリビューターで関連オブジェクトを削除せずにサブスクリプションを削除する@ignore_distributorに値 1 を指定します。
マージ パブリケーションのプル サブスクリプションを削除するには
サブスクリプション データベースのサブスクライバーで、 sp_dropmergepullsubscription (Transact-SQL) を実行します。 @publication、@publisher、および@publisher_dbを指定します。
パブリッシャー側のパブリケーション データベースで、 sp_dropmergesubscription (Transact-SQL) を実行します。 @publication、@subscriber、および@subscriber_dbを指定します。 @subscription_typeの pull の値を指定します。 (省略可能)ディストリビューターにアクセスできない場合は、ディストリビューターで関連オブジェクトを削除せずにサブスクリプションを削除する@ignore_distributorに値 1 を指定します。
例 (Transact-SQL)
次の例では、トランザクション パブリケーションへのプル サブスクリプションを削除します。 最初のバッチはサブスクライバーで実行され、2 番目のバッチはパブリッシャーで実行されます。
-- 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".
-- This is the batch executed at the Subscriber to drop
-- a pull subscription to a transactional publication.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @publisher = $(PubServer);
SET @publicationDB = N'AdventureWorks2012';
USE [AdventureWorks2012Replica]
EXEC sp_droppullsubscription
@publisher = @publisher,
@publisher_db = @publicationDB,
@publication = @publication;
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".
-- This batch is executed at the Publisher to remove
-- a pull or push subscription to a transactional publication.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @subscriber = $(SubServer);
USE [AdventureWorks2012]
EXEC sp_dropsubscription
@publication = @publication,
@article = N'all',
@subscriber = @subscriber;
GO
次の例では、マージ パブリケーションのプル サブスクリプションを削除します。 最初のバッチはサブスクライバーで実行され、2 番目のバッチはパブリッシャーで実行されます。
-- 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".
-- This batch is executed at the Subscriber to remove
-- a merge pull subscription.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publication_db AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @publisher = $(PubServer);
SET @publication_db = N'AdventureWorks2012';
USE [AdventureWorks2012Replica]
EXEC sp_dropmergepullsubscription
@publisher = @publisher,
@publisher_db = @publication_db,
@publication = @publication;
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".
-- This batch is executed at the Publisher to remove
-- a pull or push subscription to a merge publication.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorks2012Replica';
USE [AdventureWorks2012]
EXEC sp_dropmergesubscription
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriptionDB;
GO
レプリケーション管理オブジェクト (RMO) の使用
プル サブスクリプションは、レプリケーション管理オブジェクト (RMO) を使用してプログラムで削除できます。 プル サブスクリプションの削除に使用する RMO クラスは、プル サブスクリプションをサブスクライブするパブリケーションの種類によって異なります。
スナップショット パブリケーションまたはトランザクション パブリケーションのプル サブスクリプションを削除するには
ServerConnection クラスを使用してサブスクライバーとパブリッシャーの両方への接続を作成します。
TransPullSubscription クラスのインスタンスを作成し、PublicationName、DatabaseName、PublisherName、およびPublicationDBNameのプロパティを設定します。 手順 1 のサブスクライバー接続を使用して、 ConnectionContext プロパティを設定します。
IsExistingObject プロパティを調べて、サブスクリプションが存在することを確認します。 このプロパティの値が
false
場合は、手順 2 のサブスクリプション プロパティが正しく定義されていないか、サブスクリプションが存在しません。Remove メソッドを呼び出します。
手順 1. のパブリッシャー接続を使用して、 TransPublication クラスのインスタンスを作成します。 Name、DatabaseName、およびConnectionContextを指定します。
LoadProperties メソッドを呼び出します。 このメソッドが
false
を返す場合、手順 5 で指定したプロパティが正しくないか、パブリケーションがサーバーに存在しません。RemovePullSubscription メソッドを呼び出します。 サブスクライバー および サブスクライバーDB パラメーターのサブスクライバーとサブスクリプションデータベースの名前を指定します。
マージ パブリケーションのプル サブスクリプションを削除するには
ServerConnection クラスを使用してサブスクライバーとパブリッシャーの両方への接続を作成します。
MergePullSubscription クラスのインスタンスを作成し、PublicationName、DatabaseName、PublisherName、およびPublicationDBNameのプロパティを設定します。 手順 1 の接続を使用して、 ConnectionContext プロパティを設定します。
IsExistingObject プロパティを調べて、サブスクリプションが存在することを確認します。 このプロパティの値が
false
場合は、手順 2 のサブスクリプション プロパティが正しく定義されていないか、サブスクリプションが存在しません。Remove メソッドを呼び出します。
手順 1. のパブリッシャー接続を使用して、 MergePublication クラスのインスタンスを作成します。 Name、DatabaseName、およびConnectionContextを指定します。
LoadProperties メソッドを呼び出します。 このメソッドが
false
を返す場合、手順 5 で指定したプロパティが正しくないか、パブリケーションがサーバーに存在しません。RemovePullSubscription メソッドを呼び出します。 サブスクライバーおよびサブスクライバーDB パラメーターに対して、サブスクライバーとサブスクリプションデータベースの名前を指定します。
例 (RMO)
次の使用例は、トランザクション パブリケーションのプル サブスクリプションを削除し、パブリッシャーでのサブスクリプション登録を削除します。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
//Create connections to the Publisher and Subscriber.
ServerConnection subscriberConn = new ServerConnection(subscriberName);
ServerConnection publisherConn = new ServerConnection(publisherName);
// Create the objects that we need.
TransPublication publication;
TransPullSubscription subscription;
try
{
// Connect to the Subscriber.
subscriberConn.Connect();
// Define the pull subscription.
subscription = new TransPullSubscription();
subscription.ConnectionContext = subscriberConn;
subscription.PublisherName = publisherName;
subscription.PublicationName = publicationName;
subscription.PublicationDBName = publicationDbName;
subscription.DatabaseName = subscriptionDbName;
// Define the publication.
publication = new TransPublication();
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
publication.ConnectionContext = publisherConn;
// Delete the pull subscription, if it exists.
if (subscription.IsExistingObject)
{
if (publication.LoadProperties())
{
// Remove the pull subscription registration at the Publisher.
publication.RemovePullSubscription(subscriberName, subscriptionDbName);
}
else
{
// Do something here if the publication does not exist.
throw new ApplicationException(String.Format(
"The publication '{0}' does not exist on {1}.",
publicationName, publisherName));
}
// Delete the pull subscription at the Subscriber.
subscription.Remove();
}
else
{
throw new ApplicationException(String.Format(
"The subscription to {0} does not exist on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Implement the appropriate error handling here.
throw new ApplicationException(String.Format(
"The subscription to {0} could not be deleted.", publicationName), ex);
}
finally
{
subscriberConn.Disconnect();
publisherConn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksProductTran"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"
'Create connections to the Publisher and Subscriber.
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)
' Create the objects that we need.
Dim publication As TransPublication
Dim subscription As TransPullSubscription
Try
' Connect to the Subscriber.
subscriberConn.Connect()
' Define the pull subscription.
subscription = New TransPullSubscription()
subscription.ConnectionContext = subscriberConn
subscription.PublisherName = publisherName
subscription.PublicationName = publicationName
subscription.PublicationDBName = publicationDbName
subscription.DatabaseName = subscriptionDbName
' Define the publication.
publication = New TransPublication()
publication.Name = publicationName
publication.DatabaseName = publicationDbName
publication.ConnectionContext = publisherConn
' Delete the pull subscription, if it exists.
If subscription.IsExistingObject Then
If publication.LoadProperties() Then
' Remove the pull subscription registration at the Publisher.
publication.RemovePullSubscription(subscriberName, subscriptionDbName)
Else
' Do something here if the publication does not exist.
Throw New ApplicationException(String.Format( _
"The publication '{0}' does not exist on {1}.", _
publicationName, publisherName))
End If
' Delete the pull subscription at the Subscriber.
subscription.Remove()
Else
Throw New ApplicationException(String.Format( _
"The subscription to {0} does not exist on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Implement the appropriate error handling here.
Throw New ApplicationException(String.Format( _
"The subscription to {0} could not be deleted.", publicationName), ex)
Finally
subscriberConn.Disconnect()
publisherConn.Disconnect()
End Try
次の使用例は、マージ パブリケーションのプル サブスクリプションを削除し、パブリッシャーでのサブスクリプション登録を削除します。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksSalesOrdersMerge";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
//Create connections to the Publisher and Subscriber.
ServerConnection subscriberConn = new ServerConnection(subscriberName);
ServerConnection publisherConn = new ServerConnection(publisherName);
// Create the objects that we need.
MergePublication publication;
MergePullSubscription subscription;
try
{
// Connect to the Subscriber.
subscriberConn.Connect();
// Define the pull subscription.
subscription = new MergePullSubscription();
subscription.ConnectionContext = subscriberConn;
subscription.PublisherName = publisherName;
subscription.PublicationName = publicationName;
subscription.PublicationDBName = publicationDbName;
subscription.DatabaseName = subscriptionDbName;
// Define the publication.
publication = new MergePublication();
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
publication.ConnectionContext = publisherConn;
// Delete the pull subscription, if it exists.
if (subscription.IsExistingObject)
{
// Delete the pull subscription at the Subscriber.
subscription.Remove();
if (publication.LoadProperties())
{
publication.RemovePullSubscription(subscriberName, subscriptionDbName);
}
else
{
// Do something here if the publication does not exist.
throw new ApplicationException(String.Format(
"The publication '{0}' does not exist on {1}.",
publicationName, publisherName));
}
}
else
{
throw new ApplicationException(String.Format(
"The subscription to {0} does not exist on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Implement the appropriate error handling here.
throw new ApplicationException(String.Format(
"The subscription to {0} could not be deleted.", publicationName), ex);
}
finally
{
subscriberConn.Disconnect();
publisherConn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"
'Create connections to the Publisher and Subscriber.
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)
' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergePullSubscription
Try
' Connect to the Subscriber.
subscriberConn.Connect()
' Define the pull subscription.
subscription = New MergePullSubscription()
subscription.ConnectionContext = subscriberConn
subscription.PublisherName = publisherName
subscription.PublicationName = publicationName
subscription.PublicationDBName = publicationDbName
subscription.DatabaseName = subscriptionDbName
' Define the publication.
publication = New MergePublication()
publication.Name = publicationName
publication.DatabaseName = publicationDbName
publication.ConnectionContext = publisherConn
' Delete the pull subscription, if it exists.
If subscription.IsExistingObject Then
' Delete the pull subscription at the Subscriber.
subscription.Remove()
If publication.LoadProperties() Then
publication.RemovePullSubscription(subscriberName, subscriptionDbName)
Else
' Do something here if the publication does not exist.
Throw New ApplicationException(String.Format( _
"The publication '{0}' does not exist on {1}.", _
publicationName, publisherName))
End If
Else
Throw New ApplicationException(String.Format( _
"The subscription to {0} does not exist on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Implement the appropriate error handling here.
Throw New ApplicationException(String.Format( _
"The subscription to {0} could not be deleted.", publicationName), ex)
Finally
subscriberConn.Disconnect()
publisherConn.Disconnect()
End Try