次の方法で共有


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

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

このトピックの内容

  • プッシュ サブスクリプションを削除するために使用するもの:

    SQL Server Management Studio

    Transact-SQL

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

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

[トップに戻る] リンクで使用される矢印アイコン[Top]

Transact-SQL の使用

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

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

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

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

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

  1. パブリッシャーで sp_dropmergesubscription (Transact-SQL) を実行します。@publication@subscriber@subscriber_db を指定します。 (省略可) ディストリビューターにアクセスできない場合、@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 [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

[トップに戻る] リンクで使用される矢印アイコン[Top]

レプリケーション管理オブジェクト (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 = "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

[トップに戻る] リンクで使用される矢印アイコン[Top]

関連項目

概念

パブリケーションのサブスクライブ

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