刪除發送訂閱

適用於:SQL ServerAzure SQL Database

本主題說明如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (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

使用 Replication Management Objects (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)

您可以使用 Replication Management Objects (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

另請參閱

訂閱發行集
複寫安全性最佳作法