本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 在 SQL Server 2014 中刪除發送訂閱。
本主題內容
刪除推送訂閱的方法:
使用 SQL Server Management Studio
刪除在發行者端的推送訂閱(從 SQL Server Management Studio 中的 [本機發行集] 資料夾)或在訂閱者端(從 [本機訂閱] 資料夾)。 刪除訂用帳戶並不會從訂用帳戶中移除對象或數據;必須手動移除它們。
若要在發行者系統中刪除推送訂閱
連線到 Microsoft 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
使用 Replication Management Objects (RMO)
您用來刪除推送訂閱的 RMO 類別取決於推送訂閱所屬的發行集類型。
若要刪除快照式或交易式發行集的推送訂閱
使用 ServerConnection 類別建立與「訂閱者」的連接。
建立 TransSubscription 類別的執行個體。
PublicationName設定、SubscriptionDBName、 SubscriberName和 DatabaseName 屬性。
將步驟 1 中的 ServerConnection 設定為 ConnectionContext 屬性。
檢查IsExistingObject屬性以確認訂用帳戶是否存在。 如果這個屬性的值是
false,則步驟 2 中的訂閱屬性定義不正確或訂閱不存在。呼叫 Remove 方法。
若要刪除合併出版物的推送訂閱
使用 ServerConnection 類別建立與「訂閱者」的連接。
建立 MergeSubscription 類別的執行個體。
PublicationName設定、SubscriptionDBName、 SubscriberName和 DatabaseName 屬性。
在步驟 1 中設定 ServerConnection 為 ConnectionContext 屬性。
檢查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