語言

刪除發行集

本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 來刪除 SQL Server 2012 中的發行集。

本主題內容

  • 若要刪除發行集,請使用:

    SQL Server Management Studio

    Transact-SQL

    Replication Management Objects (RMO)

使用 SQL Server Management Studio

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

若要刪除出版品

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

  2. 展開 [複寫] 資料夾,然後展開 [本機發行集] 資料夾。

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

搭配回到頁首連結使用的箭頭圖示[Top]

使用 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)

此範例會示範如何移除交易式發行集,並針對資料庫停用交易式發行。 這個範例假設之前已移除所有的訂閱。 如需詳細資訊,請參閱<刪除提取訂閱>或<刪除發送訂閱>。

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

-- Remove a transactional publication.
USE [AdventureWorks2012]
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

此範例會示範如何移除合併式發行集,並針對資料庫停用合併式發行。 這個範例假設之前已移除所有的訂閱。 如需詳細資訊,請參閱<刪除提取訂閱>或<刪除發送訂閱>。

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

搭配回到頁首連結使用的箭頭圖示[Top]

使用 Replication Management Objects (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 = "AdventureWorks2012";

            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 = "AdventureWorks2012"

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 = "AdventureWorks2012";

            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 = "AdventureWorks2012"

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

搭配回到頁首連結使用的箭頭圖示[Top]

請參閱

概念

複寫系統預存程序概念

發行資料和資料庫物件