删除推送订阅

适用于:SQL ServerAzure SQL 数据库

本主题介绍如何通过使用 SQL Server Management Studio、Transact-SQL 或复制管理对象 (RMO) 在 SQL Server 中删除推送订阅。

本主题内容

使用 SQL Server Management Studio

在发布服务器上删除推送订阅(从 SQL Server Management Studio 的“本地发布”文件夹中),或在订阅服务器上删除推送订阅(从“本地订阅”文件夹中)。 删除订阅不会从订阅中删除对象或数据,必须对其手动删除。

删除发布服务器上的推送订阅

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

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

  3. 展开与要删除的订阅关联的发布。

  4. 右键单击该订阅,再单击 “删除”

  5. 在确认对话框中,选择是否连接到订阅服务器以删除订阅信息。 如果清除了 “连接到订阅服务器” 复选框,则应以后连接到订阅服务器删除信息。

删除订阅服务器上的推送订阅

  1. 在 SQL Server Management Studio 中连接到订阅服务器,然后展开服务器节点。

  2. 展开 “复制” 文件夹,再展开 “本地订阅” 文件夹。

  3. 右键单击要删除的订阅,再单击 “删除”

  4. 在确认对话框中,选择是否连接到发布服务器以删除订阅信息。 如果清除 “连接到发布服务器” 复选框,则应在以后连接到发布服务器以删除订阅信息。

“使用 Transact-SQL”

可以使用复制存储过程以编程方式删除推送订阅。 所用的存储过程取决于订阅所属的发布的类型。

删除快照发布或事务发布的推送订阅

  1. 在发布数据库的发布服务器上,执行 sp_dropsubscription (Transact-SQL)。 指定 @publication@subscriber。 将 @article 的值指定为 all。 (可选)如果无法访问分发服务器,将 @ignore_distributor 的值指定为 1 ,以便在不删除分发服务器上相关对象的情况下删除订阅。

  2. 在订阅服务器上,对订阅数据库执行 sp_subscription_cleanup (Transact-SQL) 以移除订阅数据库中的复制元数据。

删除合并发布的推送订阅

  1. 在发布服务器上,执行 sp_dropmergesubscription (Transact-SQL),并指定 @publication@subscriber@subscriber_db。 (可选)如果无法访问分发服务器,将 @ignore_distributor 的值指定为 1 ,以便在不删除分发服务器上相关对象的情况下删除订阅。

  2. 在订阅服务器上,对订阅数据库执行 sp_mergesubscription_cleanup (Transact-SQL)。 指定 @publisher@publisher_db@publication。 这将会删除订阅数据库中的合并元数据。

示例 (Transact-SQL)

该示例删除对事务发布的推送订阅。

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

-- This batch is executed at the Publisher to remove 
-- a pull or push subscription to a transactional publication.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @subscriber = $(SubServer);

USE [AdventureWorks2022]
EXEC sp_dropsubscription 
  @publication = @publication, 
  @article = N'all',
  @subscriber = @subscriber;
GO

该示例删除对合并发布的推送订阅。

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

-- This batch is executed at the Publisher to remove 
-- a pull or push subscription to a merge publication.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorks2022Replica';

USE [AdventureWorks2022]
EXEC sp_dropmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB;
GO

使用复制管理对象 (RMO)

用于删除推送订阅的 RMO 类取决于订阅推送订阅的发布的类型。

删除快照发布或事务发布的推送订阅

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

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

  3. 设置 PublicationNameSubscriptionDBNameSubscriberNameDatabaseName 属性。

  4. ServerConnection 属性设置为步骤 1 中的 ConnectionContext

  5. 检查 IsExistingObject 属性以确保该订阅存在。 如果此属性的值为 false,则步骤 2 中所定义的订阅属性不正确或者该订阅不存在。

  6. 调用 Remove 方法。

删除合并发布的推送订阅

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

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

  3. 设置 PublicationNameSubscriptionDBNameSubscriberNameDatabaseName 属性。

  4. ServerConnection 属性设置为步骤 1 中的 ConnectionContext

  5. 检查 IsExistingObject 属性以确保该订阅存在。 如果此属性的值为 false,则步骤 2 中所定义的订阅属性不正确或者该订阅不存在。

  6. 调用 Remove 方法。

示例 (RMO)

可通过使用复制管理对象 (RMO) 以编程方式删除推送订阅。

// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

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

// Create the objects that we need.
TransSubscription subscription;

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

    // Define the pull subscription.
    subscription = new TransSubscription();
    subscription.ConnectionContext = conn;
    subscription.SubscriberName = subscriberName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.DatabaseName = publicationDbName;

    // Delete the pull subscription, if it exists.
    if (subscription.IsExistingObject)
    {
        // Delete the pull subscription at the Subscriber.
        subscription.Remove();
    }
    else
    {
        throw new ApplicationException(String.Format(
            "The subscription to {0} does not exist on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement the appropriate error handling here.
    throw new ApplicationException(String.Format(
        "The subscription to {0} could not be deleted.", publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksProductTran"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

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

' Create the objects that we need.
Dim subscription As TransSubscription

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

    ' Define the pull subscription.
    subscription = New TransSubscription()
    subscription.ConnectionContext = conn
    subscription.SubscriberName = subscriberName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.DatabaseName = publicationDbName

    ' Delete the pull subscription, if it exists.
    If subscription.IsExistingObject Then

        ' Delete the pull subscription at the Subscriber.
        subscription.Remove()
    Else
        Throw New ApplicationException(String.Format( _
         "The subscription to {0} does not exist on {1}", _
         publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
        "The subscription to {0} could not be deleted.", publicationName), ex)
Finally
    conn.Disconnect()
End Try

另请参阅

Subscribe to Publications
复制安全最佳做法