本主题介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象(RMO)在 SQL Server 2014 中删除推送订阅。
本主题内容
若要删除推送订阅,请使用:
使用 SQL Server Management Studio
在发布服务器上(从 SQL Server Management Studio 中的 本地发布 文件夹)或在订阅服务器上(从 本地订阅 文件夹)删除推送订阅。 删除订阅不会从订阅中删除对象或数据;必须手动删除它们。
在发布者上删除推送订阅
在 SQL Server Management Studio 中连接到发布服务器,然后展开服务器节点。
展开 “复制 ”文件夹,然后展开 “本地发布” 文件夹。
展开与该订阅关联的出版物,以便删除。
右键单击订阅,然后单击“ 删除”。
在确认对话框中,选择是否连接到订阅服务器以删除订阅信息。 如果清除“ 连接到订阅服务器 ”复选框,则应稍后连接到订阅服务器以删除信息。
在订阅者处删除推送订阅
在 SQL Server Management Studio 中连接到订阅服务器,然后展开服务器节点。
展开 “复制 ”文件夹,然后展开 “本地订阅 ”文件夹。
右键单击要删除的订阅,然后单击“ 删除”。
在确认对话框中,选择是否连接到发布服务器以删除订阅信息。 如果清除“ 连接到发布服务器 ”复选框,则应稍后连接到发布服务器以删除信息。
使用 Transact-SQL
可以使用复制存储过程以编程方式删除推送订阅。 使用的存储过程取决于订阅所属的发布类型。
删除快照或事务发布的推送订阅
在发布者的发布数据库上,执行sp_dropsubscription(Transact-SQL)。 指定 @publication 和 @subscriber。 为@article指定all的值。 (可选)如果无法访问分发服务器,请为@ignore_distributor指定值 1 以删除订阅,而无需在分发服务器上删除相关对象。
在订阅数据库的订阅服务器上,执行 sp_subscription_cleanup(Transact-SQL) 以清除订阅数据库中的复制元数据。
删除合并发布的推送订阅
在发布服务器上,执行 sp_dropmergesubscription(Transact-SQL),指定 @publication、 @subscriber 和 @subscriber_db。 (可选)如果无法访问分发服务器,请为@ignore_distributor指定值 1 以删除订阅,而无需在分发服务器上删除相关对象。
在订阅者上的订阅数据库中,执行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 [AdventureWorks2012]
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'AdventureWorks2012Replica';
USE [AdventureWorks2012]
EXEC sp_dropmergesubscription
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriptionDB;
GO
使用复制管理对象 (RMO)
删除推送订阅时使用的 RMO 类取决于推送订阅关联的发布类型。
删除快照或事务发布的推送订阅
使用 ServerConnection 类创建与订阅服务器的连接。
创建 TransSubscription 类的一个实例。
设置PublicationName、SubscriptionDBName、SubscriberName和DatabaseName属性。
将步骤 1 中的 ServerConnection 设置为 ConnectionContext 属性。
检查属性 IsExistingObject 以验证订阅是否存在。 如果此属性的值是
false
,则步骤 2 中的订阅属性定义不正确或订阅不存在。调用 Remove 方法。
删除针对合并刊物的推送订阅
使用 ServerConnection 类创建与订阅服务器的连接。
创建 MergeSubscription 类的一个实例。
设置PublicationName、SubscriptionDBName、SubscriberName 和DatabaseName属性。
根据步骤 1 设置 ConnectionContext 属性的 ServerConnection。
检查属性 IsExistingObject 以验证订阅是否存在。 如果此属性的值是
false
,则步骤 2 中的订阅属性定义不正确或订阅不存在。调用 Remove 方法。
示例 (RMO)
可以使用复制管理对象(RMO)以编程方式删除推送订阅。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
//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 = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"
'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