刪除出版物

本主題描述如何透過使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 刪除 SQL Server 2014 中的發布。

本主題內容

使用 SQL Server Management Studio

在 SQL Server Management Studio 中,從 [ 本機發行集 ] 資料夾中移除刊物。

刪除出版物

  1. 連線至 Microsoft SQL Server 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)

此範例示範如何移除交易式出版物,並停用資料庫的交易式出版功能。 此範例假設先前已移除所有訂用帳戶。 如需詳細資訊,請參閱 刪除提取訂閱 或刪除 發送訂閱。

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

使用 Replication Management Objects (RMO)

您可以使用複寫管理物件 (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

另請參閱

復寫系統預存程式概念
發行資料和資料庫物件