このトピックでは、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_dropsubscription (Transact-SQL) を実行します。 @publicationと@subscriberを指定します。 @articleの all の値を指定します。 (省略可能)ディストリビューターにアクセスできない場合は、ディストリビューターで関連オブジェクトを削除せずにサブスクリプションを削除する@ignore_distributorに値 1 を指定します。
サブスクリプション データベースのサブスクライバーで 、sp_subscription_cleanup (Transact-SQL) を実行して、サブスクリプション データベース内のレプリケーション メタデータを削除します。
マージ パブリケーションへのプッシュ サブスクリプションを削除するには
パブリッシャーで、 sp_dropmergesubscription (Transact-SQL) を実行し、 @publication、 @subscriber 、 および@subscriber_dbを指定します。 (省略可能)ディストリビューターにアクセスできない場合は、ディストリビューターで関連オブジェクトを削除せずにサブスクリプションを削除する@ignore_distributorに値 1 を指定します。
サブスクリプション データベースのサブスクライバーで、 sp_mergesubscription_cleanup (Transact-SQL) を実行します。 @publisher、@publisher_db、および@publicationを指定します。 これにより、マージ メタデータがサブスクリプション データベースから削除されます。
例 (Transact-SQL)
次の使用例は、トランザクション パブリケーションへのプッシュ サブスクリプションを削除します。
-- 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
次の使用例は、マージ パブリケーションへのプッシュ サブスクリプションを削除します。
-- 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 クラスは、プッシュ サブスクリプションをサブスクライブするパブリケーションの種類によって異なります。
スナップショット パブリケーションまたはトランザクション パブリケーションへのプッシュ サブスクリプションを削除するには
ServerConnection クラスを使用してサブスクライバーへの接続を作成します。
TransSubscription クラスのインスタンスを作成します。
PublicationName、SubscriptionDBName、SubscriberName、およびDatabaseNameのプロパティを設定します。
ConnectionContext プロパティの手順 1 のServerConnectionを設定します。
IsExistingObject プロパティを調べて、サブスクリプションが存在することを確認します。 このプロパティの値が
false
場合は、手順 2 のサブスクリプション プロパティが正しく定義されていないか、サブスクリプションが存在しません。Remove メソッドを呼び出します。
マージ パブリケーションへのプッシュ サブスクリプションを削除するには
ServerConnection クラスを使用してサブスクライバーへの接続を作成します。
MergeSubscription クラスのインスタンスを作成します。
PublicationName、SubscriptionDBName、SubscriberName、およびDatabaseNameのプロパティを設定します。
ConnectionContext プロパティの手順 1 のServerConnectionを設定します。
IsExistingObject プロパティを調べて、サブスクリプションが存在することを確認します。 このプロパティの値が
false
場合は、手順 2 のサブスクリプション プロパティが正しく定義されていないか、サブスクリプションが存在しません。Remove メソッドを呼び出します。
例 (RMO)
レプリケーション管理オブジェクト (RMO) を使用して、プッシュ サブスクリプションをプログラムで削除できます。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
//Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);
// Create the objects that we need.
TransSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define the pull subscription.
subscription = new TransSubscription();
subscription.ConnectionContext = conn;
subscription.SubscriberName = subscriberName;
subscription.PublicationName = publicationName;
subscription.SubscriptionDBName = subscriptionDbName;
subscription.DatabaseName = publicationDbName;
// Delete the pull subscription, if it exists.
if (subscription.IsExistingObject)
{
// 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
{
conn.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 a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)
' Create the objects that we need.
Dim subscription As TransSubscription
Try
' Connect to the Subscriber.
conn.Connect()
' Define the pull subscription.
subscription = New TransSubscription()
subscription.ConnectionContext = conn
subscription.SubscriberName = subscriberName
subscription.PublicationName = publicationName
subscription.SubscriptionDBName = subscriptionDbName
subscription.DatabaseName = publicationDbName
' Delete the pull subscription, if it exists.
If subscription.IsExistingObject Then
' 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
conn.Disconnect()
End Try