パブリケーションの削除

適用対象: SQL ServerAzure SQL Managed Instance

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

このトピックの内容

SQL Server Management Studio を使用する

SQL Server Management Studio 内の [ローカル パブリケーション] フォルダーから出版物を削除します。

パブリケーションを削除するには

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

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

  3. 削除するパブリケーションを右クリックし、 [削除]をクリックします。

Transact-SQL の使用

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

Note

パブリケーションを削除しても、パブリッシュされたオブジェクトはパブリケーション データベースから削除されず、対応するオブジェクトはサブスクリプション データベースから削除されません。 これらのオブジェクトは、必要に応じて DROP <object> コマンドを使用し、手動で削除します。

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

  1. 次のいずれかの操作を行います。

    • 単一のパブリケーションを削除するには、パブリッシャーのパブリケーション データベースで sp_droppublication を実行します。

    • すべてのパブリケーションを削除し、パブリッシュされたデータベースからすべてのレプリケーション オブジェクトを削除するには、パブリッシャーで sp_removedbreplication を実行します。 @type には tranを指定します。 (省略可) ディストリビューターにアクセスできない場合や、ディストリビューターのデータベース ステータスがオフラインになっている可能性がある場合は、 @force1を指定します。 (省略可) パブリケーション データベースに対して sp_removedbreplication を実行しない場合は、 @dbname にデータベースの名前を指定します。

      Note

      @force1 を指定すると、レプリケーション関連のパブリッシング オブジェクトがデータベース上に残されます。

  2. (省略可) このデータベースに他の出版物が存在しない場合は、sp_replicationdboption (Transact-SQL) を実行し、スナップショット レプリケーションまたはトランザクション レプリケーションを使用した、現在のデータベースのパブリケーションを無効にします。

  3. (省略可) サブスクライバーのサブスクリプション データベースで sp_subscription_cleanup を実行して、サブスクリプション データベースに残っているレプリケーション メタデータをすべて削除します。

マージ パブリケーションを削除するには

  1. 次のいずれかの操作を行います。

    • 単一の出版物を削除するには、パブリッシャーのパブリケーション データベースで sp_dropmergepublication (Transact-SQL) を実行します。

    • すべてのパブリケーションを削除し、パブリッシュされたデータベースからすべてのレプリケーション オブジェクトを削除するには、パブリッシャーで sp_removedbreplication を実行します。 @type には mergeを指定します。 (省略可) ディストリビューターにアクセスできない場合や、ディストリビューターのデータベース ステータスがオフラインになっている可能性がある場合は、 @force1を指定します。 (省略可) パブリケーション データベースに対して sp_removedbreplication を実行しない場合は、 @dbname にデータベースの名前を指定します。

      Note

      @force1 を指定すると、レプリケーション関連のパブリッシング オブジェクトがデータベース上に残されます。

  2. (省略可) このデータベースに他の出版物が存在しない場合は、sp_replicationdboption (Transact-SQL) を実行し、マージ レプリケーションを使用した、現在のデータベースのパブリケーションを無効にします。

  3. (省略可) サブスクライバー側のサブスクリプション データベースに対して sp_mergesubscription_cleanup (Transact-SQL) を実行し、サブスクリプション データベースに残っているレプリケーション メタデータをすべて削除します。

例 (Transact-SQL)

次の例は、トランザクション パブリケーションを削除し、データベースのトランザクション パブリッシングを無効にする方法を示しています。 すべてのサブスクリプションがあらかじめ削除されていることを想定しています。 詳細については、「 Delete a Pull Subscription 」または「 Delete a Push Subscription」を参照してください。

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
SET @publicationDB = N'AdventureWorks'; 
SET @publication = N'AdvWorksProductTran'; 

-- Remove a transactional publication.
USE [AdventureWorks2022]
EXEC sp_droppublication @publication = @publication;

-- Remove replication objects from the database.
USE [master]
EXEC sp_replicationdboption 
  @dbname = @publicationDB, 
  @optname = N'publish', 
  @value = N'false';
GO

次の例は、マージ パブリケーションを削除し、データベースのマージ パブリッシングを無効にする方法を示しています。 すべてのサブスクリプションがあらかじめ削除されていることを想定しています。 詳細については、「 Delete a Pull Subscription 」または「 Delete a Push Subscription」を参照してください。

DECLARE @publication AS sysname
DECLARE @publicationDB    AS sysname
SET @publication = N'AdvWorksSalesOrdersMerge' 
SET @publicationDB = N'AdventureWorks'

-- Remove the merge publication.
USE [AdventureWorks]
EXEC sp_dropmergepublication @publication = @publication;

-- Remove replication objects from the database.
USE master
EXEC sp_replicationdboption 
  @dbname = @publicationDB, 
  @optname = N'merge publish', 
  @value = N'false'
GO

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

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

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

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

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

  3. パブリケーションの Name プロパティおよび DatabaseName プロパティを設定し、 ConnectionContext プロパティに手順 1. で作成した接続を設定します。

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

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

  6. (省略可) このデータベースに他のトランザクション パブリケーションが存在する場合は、次に示すように、トランザクション パブリッシングに対してデータベースを無効にすることができます。

    1. ReplicationDatabase クラスのインスタンスを作成します。 手順 1. の ConnectionContext インスタンスを ServerConnection プロパティに設定します。

    2. LoadProperties メソッドを呼び出します。 このメソッドにより falseが返された場合は、データベースが存在することを確認してください。

    3. パブリケーションの EnabledTransPublishing プロパティを falseフォルダーからパブリケーションを削除します。

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

  7. 接続を閉じます。

マージ パブリケーションを削除するには

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

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

  3. パブリケーションの Name プロパティおよび DatabaseName プロパティを設定し、 ConnectionContext プロパティに手順 1. で作成した接続を設定します。

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

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

  6. (省略可) このデータベースに他のマージ パブリケーションが存在する場合は、次に示すように、マージ パブリッシングに対してデータベースを無効にすることができます。

    1. ReplicationDatabase クラスのインスタンスを作成します。 ConnectionContext プロパティを、手順 1. の ServerConnection のインスタンスに設定します。

    2. LoadProperties メソッドを呼び出します。 このメソッドが falseを返す場合、データベースが存在するかどうかを確認してください。

    3. パブリケーションの EnabledMergePublishing プロパティを falseフォルダーからパブリケーションを削除します。

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

  7. 接続を閉じます。

例 (RMO)

次の例では、トランザクション パブリケーションを削除します。 このデータベースに対して他のトランザクション パブリケーションが存在しない場合は、トランザクション パブリッシングも無効になります。

// Define the Publisher, publication database, 
// and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2022";

TransPublication publication;
ReplicationDatabase publicationDb;

// Create a connection to the Publisher 
// using Windows Authentication.
ServerConnection conn = new ServerConnection(publisherName);

try
{
    conn.Connect();

    // Set the required properties for the transactional publication.
    publication = new TransPublication();
    publication.ConnectionContext = conn;
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;

    // Delete the publication, if it exists and has no subscriptions.
    if (publication.LoadProperties() && !publication.HasSubscription)
    {
        publication.Remove();
    }
    else
    {
        // Do something here if the publication does not exist
        // or has subscriptions.
        throw new ApplicationException(String.Format(
            "The publication {0} could not be deleted. " +
            "Ensure that the publication exists and that all " +
            "subscriptions have been deleted.",
            publicationName, publisherName));
    }

    // If no other transactional publications exists,
    // disable publishing on the database.
    publicationDb = new ReplicationDatabase(publicationDbName, conn);
    if (publicationDb.LoadProperties())
    {
        if (publicationDb.TransPublications.Count == 0)
        {
            publicationDb.EnabledTransPublishing = false;
        }
    }
    else
    {
        // Do something here if the database does not exist.
        throw new ApplicationException(String.Format(
            "The database {0} does not exist on {1}.",
            publicationDbName, publisherName));
    }
}
catch (Exception ex)
{
    // Implement application error handling here.
    throw new ApplicationException(String.Format(
        "The publication {0} could not be deleted.",
        publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' Define the Publisher, publication database, 
' and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2022"

Dim publication As TransPublication
Dim publicationDb As ReplicationDatabase

' Create a connection to the Publisher 
' using Windows Authentication.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Try
    conn.Connect()

    ' Set the required properties for the transactional publication.
    publication = New TransPublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' Delete the publication, if it exists and has no subscriptions.
    If publication.LoadProperties() And Not publication.HasSubscription Then
        publication.Remove()
    Else
        ' Do something here if the publication does not exist
        ' or has subscriptions.
        Throw New ApplicationException(String.Format( _
         "The publication {0} could not be deleted. " + _
         "Ensure that the publication exists and that all " + _
         "subscriptions have been deleted.", _
         publicationName, publisherName))
    End If

    ' If no other transactional publications exists,
    ' disable publishing on the database.
    publicationDb = New ReplicationDatabase(publicationDbName, conn)
    If publicationDb.LoadProperties() Then
        If publicationDb.TransPublications.Count = 0 Then
            publicationDb.EnabledTransPublishing = False
        End If
    Else
        ' Do something here if the database does not exist.
        Throw New ApplicationException(String.Format( _
         "The database {0} does not exist on {1}.", _
         publicationDbName, publisherName))
    End If
Catch ex As Exception
    ' Implement application error handling here.
    Throw New ApplicationException(String.Format( _
     "The publication {0} could not be deleted.", _
     publicationName), ex)
Finally
    conn.Disconnect()
End Try

次の例では、マージ パブリケーションを削除します。 このデータベースに対して他のマージ パブリケーションが存在しない場合は、マージ パブリッシングも無効になります。

// Define the Publisher, publication database, 
// and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks2022";

MergePublication publication;
ReplicationDatabase publicationDb;

// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Set the required properties for the merge publication.
    publication = new MergePublication();
    publication.ConnectionContext = conn;
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;

    // Delete the publication, if it exists and has no subscriptions.
    if (publication.LoadProperties() && !publication.HasSubscription)
    {
        publication.Remove();
    }
    else
    {
        // Do something here if the publication does not exist
        // or has subscriptions.
        throw new ApplicationException(String.Format(
            "The publication {0} could not be deleted. " +
            "Ensure that the publication exists and that all " +
            "subscriptions have been deleted.",
            publicationName, publisherName));
    }

    // If no other merge publications exists,
    // disable publishing on the database.
    publicationDb = new ReplicationDatabase(publicationDbName, conn);
    if (publicationDb.LoadProperties())
    {
        if (publicationDb.MergePublications.Count == 0 && publicationDb.EnabledMergePublishing)
        {
            publicationDb.EnabledMergePublishing = false;
        }
    }
    else
    {
        // Do something here if the database does not exist.
        throw new ApplicationException(String.Format(
            "The database {0} does not exist on {1}.",
            publicationDbName, publisherName));
    }
}
catch (Exception ex)
{
    // Implement application error handling here.
    throw new ApplicationException(String.Format(
        "The publication {0} could not be deleted.",
        publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' Define the Publisher, publication database, 
' and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2022"

Dim publication As MergePublication
Dim publicationDb As ReplicationDatabase

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Set the required properties for the merge publication.
    publication = New MergePublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' Delete the publication, if it exists and has no subscriptions.
    If (publication.LoadProperties() And Not publication.HasSubscription) Then
        publication.Remove()
    Else
        ' Do something here if the publication does not exist
        ' or has subscriptions.
        Throw New ApplicationException(String.Format( _
         "The publication {0} could not be deleted. " + _
         "Ensure that the publication exists and that all " + _
         "subscriptions have been deleted.", _
         publicationName, publisherName))
    End If

    ' If no other merge publications exists,
    ' disable publishing on the database.
    publicationDb = New ReplicationDatabase(publicationDbName, conn)
    If publicationDb.LoadProperties() Then
        If publicationDb.MergePublications.Count = 0 _
        And publicationDb.EnabledMergePublishing Then
            publicationDb.EnabledMergePublishing = False
        End If
    Else
        ' Do something here if the database does not exist.
        Throw New ApplicationException(String.Format( _
         "The database {0} does not exist on {1}.", _
         publicationDbName, publisherName))
    End If
Catch ex As Exception
    ' Implement application error handling here.
    Throw New ApplicationException(String.Format( _
     "The publication {0} could not be deleted.", _
     publicationName), ex)
Finally
    conn.Disconnect()
End Try

参照

Replication System Stored Procedures Concepts
データとデータベース オブジェクトのパブリッシュ