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

適用対象: SQL ServerAzure SQL Managed Instance

このトピックでは、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_droppullsubscription (Transact-SQL) を実行します。 @publication@publisher、および @publisher_dbを指定します。

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

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

  1. サブスクライバー側のサブスクリプション データベースに対して、sp_dropmergepullsubscription (Transact-SQL) を実行します。 @publication@publisher、および @publisher_dbを指定します。

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

例 (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 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'AdventureWorks2022';

USE [AdventureWorks2022Replica]
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 [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 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'AdventureWorks2022';

USE [AdventureWorks2022Replica]
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'AdventureWorks2022Replica';

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

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

レプリケーション管理オブジェクト (RMO) を使用することで、プログラムによってプル サブスクリプションを削除できます。 プル サブスクリプションの削除に使用する RMO クラスは、プル サブスクリプションをサブスクライブするパブリケーションの種類によって決まります。

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

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

  2. TransPullSubscription クラスのインスタンスを作成し、 PublicationNameDatabaseNamePublisherName、および PublicationDBName の各プロパティを設定します。 手順 1. のサブスクライバー接続を使用して、 ConnectionContext プロパティを設定します。

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

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

  5. 手順 1. のパブリッシャー接続を使用して、 TransPublication クラスのインスタンスを作成します。 NameDatabaseName 、および ConnectionContextを指定します。

  6. LoadProperties メソッドを呼び出します。 このメソッドが falseを返す場合、手順 5. で指定したプロパティが誤っているか、サーバーにパブリケーションが存在していません。

  7. RemovePullSubscription メソッドを呼び出します。 subscriber パラメーターと subscriberDB パラメーターに、サブスクライバーの名前とサブスクリプション データベースを指定します。

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

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

  2. MergePullSubscription クラスのインスタンスを作成し、 PublicationNameDatabaseNamePublisherName、および PublicationDBName の各プロパティを設定します。 手順 1. の接続を使用して、 ConnectionContext プロパティを設定します。

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

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

  5. 手順 1. のパブリッシャー接続を使用して、 MergePublication クラスのインスタンスを作成します。 NameDatabaseName 、および ConnectionContextを指定します。

  6. LoadProperties メソッドを呼び出します。 このメソッドが falseを返す場合、手順 5. で指定したプロパティが誤っているか、サーバーにパブリケーションが存在していません。

  7. RemovePullSubscription メソッドを呼び出します。 subscriber パラメーターと subscriberDB パラメーターに、サブスクライバーの名前とサブスクリプション データベースを指定します。

例 (RMO)

次の例では、トランザクション パブリケーションに対するプル サブスクリプションを削除し、パブリッシャー側のサブスクリプション登録を削除します。

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

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

'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 = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

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

'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

参照

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