删除出版物

本主题介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象(RMO)在 SQL Server 2014 中删除发布。

本主题内容

使用 SQL Server Management Studio

从 SQL Server Management Studio 中的 “本地发布” 文件夹中删除发布。

删除发布的文件

  1. 在 Management Studio 中连接到发布服务器,然后展开服务器节点。

  2. 展开 “复制 ”文件夹,然后展开 “本地发布” 文件夹。

  3. 右键单击要删除的出版物,然后单击“ 删除”。

使用 Transact-SQL

可以使用复制存储过程以编程方式删除出版物。 使用的存储过程取决于要删除的发布类型。

注释

删除发布不会从发布数据库中删除已发布的对象,也不会从订阅数据库中删除相应的对象。 如有必要,请使用 DROP <object> 命令手动删除这些对象。

删除快照或事务性发布

  1. 执行下列操作之一:

    • 若要删除单个发布,请在发布服务器上对发布数据库执行 sp_droppublication

    • 若要删除已发布数据库中的所有发布并删除所有复制对象,请在发布服务器上执行 sp_removedbreplication 。 指定tran作为@type的值。 (可选)如果无法访问分发服务器或数据库的状态可疑或脱机,请为 @force 指定值 1。 (可选)如果未对发布数据库执行 sp_removedbreplication ,请为 @dbname 指定数据库的名称。

      注释

      @force指定值1可能导致与复制相关的发布对象保留在数据库中。

  2. (可选)如果此数据库没有其他发布,请执行 sp_replicationdboption(Transact-SQL), 以使用快照或事务复制禁用当前数据库的发布。

  3. (可选)在订阅数据库上的订阅服务器,执行sp_subscription_cleanup以清除订阅数据库中的剩余复制元数据。

删除合并发布

  1. 执行下列操作之一:

    • 若要删除单个发布,请在发布服务器上对发布数据库执行sp_dropmergepublication(Transact-SQL)。

    • 若要删除已发布数据库中的所有发布内容并移除所有复制对象,请在发布服务器上执行 sp_removedbreplication 。 指定merge作为@type的值。 (可选)如果无法访问分发服务器或数据库的状态可疑或脱机,请为 @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

使用复制管理对象 (RMO)

可以通过使用复制管理对象(RMO)以编程方式删除发布。 用于删除发布的 RMO 类取决于删除的发布类型。

删除快照或事务发布

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 TransPublication 类的一个实例。

  3. 设置发布的NameDatabaseName属性,并将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. 设置发布的NameDatabaseName属性,并将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

另请参阅

复制系统存储过程概念
发布数据和数据库对象