プッシュ サブスクリプションの削除

適用対象:SQL ServerAzure SQL Database

このトピックでは、SQL Server Management Studio、Transact-SQL、または レプリケーション管理オブジェクト (RMO) を使用して、SQL Server でプッシュ サブスクリプションを削除する方法について説明します。

このトピックの内容

SQL Server Management Studio を使用する

パブリッシャーまたはサブスクライバーで、プッシュ サブスクリプションを削除します (SQL Server Management Studioの場合は の [ローカル パブリケーション] フォルダーから、サブスクライバーの場合は [ローカル サブスクリプション] フォルダーから削除します)。 サブスクリプションを削除しても、サブスクリプションのオブジェクトやデータは削除されません。これらは手動で削除する必要があります。

パブリッシャーでプッシュ サブスクリプションを削除するには

  1. SQL Server Management Studio でパブリッシャーに接続し、サーバー ノードを展開します。

  2. [レプリケーション] フォルダーを展開し、 [ローカル パブリケーション] フォルダーを展開します。

  3. 削除するサブスクリプションに関連付けられているパブリケーションを展開します。

  4. サブスクリプションを右クリックし、 [削除]をクリックします。

  5. 確認のダイアログ ボックスで、サブスクライバーに接続してサブスクリプション情報を削除するかどうかを選択します。 [サブスクライバーへの接続] チェック ボックスをオフにした場合は、後でサブスクライバーに接続してこの情報を削除する必要があります。

サブスクライバーでプッシュ サブスクリプションを削除するには

  1. SQL Server Management Studio でサブスクライバ―に接続し、サーバー ノードを展開します。

  2. [レプリケーション] フォルダーを展開し、 [ローカル サブスクリプション] フォルダーを展開します。

  3. 削除するサブスクリプションを右クリックし、 [削除]をクリックします。

  4. 確認のダイアログ ボックスで、パブリッシャーに接続してサブスクリプション情報を削除するかどうかを選択します。 [パブリッシャーへの接続] チェック ボックスをオフにした場合は、後でパブリッシャーに接続してこの情報を削除する必要があります。

Transact-SQL の使用

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

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

  1. パブリッシャー側のパブリケーション データベースに対して、sp_dropsubscription (Transact-SQL) を実行します。 @publication@subscriberを指定します。 @articleallを指定します。 (省略可) ディストリビューターにアクセスできない場合、 @ignore_distributor1 を指定して、ディストリビューターの関連オブジェクトを削除せずにサブスクリプションを削除します。

  2. サブスクライバー側のサブスクリプション データベースで sp_subscription_cleanup (Transact-SQL) を実行して、サブスクリプション データベース内のレプリケーション メタデータを削除します。

マージ パブリケーションに対するプッシュ サブスクリプションを削除するには

  1. パブリッシャー側で、@publication@subscriber@subscriber_db を指定して、sp_dropmergesubscription (Transact-SQL) を実行します。 (省略可) ディストリビューターにアクセスできない場合、 @ignore_distributor1 を指定して、ディストリビューターの関連オブジェクトを削除せずにサブスクリプションを削除します。

  2. サブスクライバー側のサブスクリプション データベースに対して、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 [AdventureWorks2022]
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'AdventureWorks2022Replica';

USE [AdventureWorks2022]
EXEC sp_dropmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB;
GO

レプリケーション管理オブジェクト (RMO) の使用

プッシュ サブスクリプションの削除に使用する RMO クラスは、プッシュ サブスクリプションをサブスクライブするパブリケーションの種類によって異なります。

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

  1. ServerConnection クラスを使用して、パブリッシャーへの接続を作成します。

  2. TransSubscription クラスのインスタンスを作成します。

  3. PublicationNameSubscriptionDBNameSubscriberNameDatabaseName の各プロパティを設定します。

  4. 手順 1. の ServerConnectionConnectionContext プロパティに設定します。

  5. IsExistingObject プロパティをチェックして、サブスクリプションが存在することを確認します。 このプロパティの値が falseの場合、手順 2. でサブスクリプション プロパティが不適切に定義されたか、サブスクリプションが存在していません。

  6. Remove メソッドを呼び出します。

マージ パブリケーションに対するプッシュ サブスクリプションを削除するには

  1. ServerConnection クラスを使用して、パブリッシャーへの接続を作成します。

  2. MergeSubscription クラスのインスタンスを作成します。

  3. PublicationNameSubscriptionDBNameSubscriberNameDatabaseName の各プロパティを設定します。

  4. 手順 1. の ServerConnectionConnectionContext プロパティに設定します。

  5. IsExistingObject プロパティをチェックして、サブスクリプションが存在することを確認します。 このプロパティの値が falseの場合、手順 2. でサブスクリプション プロパティが不適切に定義されたか、サブスクリプションが存在していません。

  6. Remove メソッドを呼び出します。

例 (RMO)

レプリケーション管理オブジェクト (RMO) を使用することで、プログラムによってプッシュ サブスクリプションを削除できます。

// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

//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 = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

'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

参照

Subscribe to Publications
レプリケーション セキュリティの推奨事項