刪除出版物

適用於:SQL ServerAzure SQL 受控執行個體

本主題說明如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 在 SQL Server 中刪除發行集。

使用 SQL Server Management Studio

從 SQL Server Management Studio 的 [本機發行集] 資料夾中刪除發行集。

若要刪除出版品

  1. 在 Management Studio 中連線到發行者,然後展開伺服器節點。

  2. 展開 複寫 資料夾,然後展開 本機發佈 資料夾。

  3. 以滑鼠右鍵按一下您想刪除的發行集,然後按一下 [刪除] 。

使用 TRANSACT-SQL

您可以使用複寫預存程序以程式設計的方式刪除發行集。 使用哪些預存程序取決於所要刪除的發行集類型而定。

注意

刪除發行集不會從發行集資料庫中移除發行的物件,或是從訂閱資料庫中移除對應物件。 必要的話,請使用 DROP <object> 命令來手動移除這些物件。

刪除快照式或交易式發行集

  1. 執行下列其中一個動作:

    • 若要刪除單一發行集,請在發行集資料庫的發行者上執行 sp_droppublication 。

    • 若要刪除已發行資料庫中的所有發行項目,並移除其中所有複寫物件,請在發行者端執行 sp_removedbreplication。 針對 @type 指定 tran的值。 (選擇性) 如果無法存取散發者,或資料庫狀態不明或已離線,請將 @force 指定為 1。 (選擇性) 如果未在發行資料庫上執行 sp_removedbreplication,請為 @dbname 指定資料庫名稱。

      注意

      針對 @force 指定 1 的值可能會將與複寫有關的發行物件留在資料庫中。

  2. (選用)如果此資料庫沒有其他發行集,請執行 sp_replicationdboption (Transact-SQL),以停用目前資料庫使用快照式或交易式複寫的發行功能。

  3. (選擇性) 在訂閱資料庫的訂閱者上,執行 sp_subscription_cleanup 來移除訂閱資料庫中任何剩餘的複寫中繼資料。

刪除合併式發行集

  1. 執行下列其中一個動作:

    • 若要刪除單一發行集,請在發行集資料庫的發行者端執行 sp_dropmergepublication (Transact-SQL)。

    • 若要在已發行的資料庫中刪除所有發行,並移除所有複寫物件,請在發行者端執行 sp_removedbreplication。 為 @type 指定 merge 的值。 (選用)如果無法存取散發者,或資料庫狀態可疑或已離線,請將 @force 指定為 1。 (選擇性) 如果未在發行資料庫上執行 sp_removedbreplication,請為 @dbname 指定資料庫名稱。

      注意

      針對 @force 指定 1 的值可能會將與複寫有關的發行物件留在資料庫中。

  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)

您可以使用 Replication Management Objects (RMO) 以程式設計的方式刪除發行集。 用來移除發行集的 RMO 類別,將取決於所移除的發行集類型而定。

移除快照式或交易式發行集

  1. 使用 ServerConnection 類別建立與發行者的連接。

  2. 建立 TransPublication 類別的執行個體。

  3. 設定發行集的 Name 和 DatabaseName 屬性,並將 ConnectionContext 屬性設定為在步驟 1 中建立的連接。

  4. 檢查 IsExistingObject 屬性,確認該發行集存在。 如果這個屬性的值為 false,則表示步驟 3 中的發行集屬性定義錯誤或是此發行集不存在。

  5. 呼叫 Remove 方法。

  6. (選用)如果此資料庫沒有其他交易式發行,則可如下停用此資料庫的交易式發行功能:

    1. 建立 ReplicationDatabase 類別的執行個體。 將 ConnectionContext 屬性設定為步驟 1 中 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