共用方式為


刪除提取訂閱

本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 刪除 SQL Server 2014 中的提取訂閱。

本主題內容

使用 SQL Server Management Studio

刪除發行者端的提取訂閱(從 SQL Server Management Studio 中的 [ 本機發行集 ] 資料夾)或刪除訂閱者端的提取訂閱(從 [ 本機訂閱 ] 資料夾)。 刪除訂用帳戶並不會從訂用帳戶中移除對象或數據;必須手動移除它們。

若要在發行者端刪除提取訂閱

  1. 連線到 Microsoft SQL Server Management Studio 的發行者,然後展開伺服器節點。

  2. 展開 複寫 資料夾,然後展開 本機發行集 資料夾。

  3. 展開與您想要刪除的訂閱相關聯的出版物。

  4. 以滑鼠右鍵按兩下訂用帳戶,然後按兩下 [ 刪除]。

  5. 在確認對話框中,選取是否要連線到訂閱者以刪除訂閱資訊。 如果您取消選取 [ 連接至訂閱者 ] 的選項框,您應該稍後連接至訂閱者以刪除資訊。

若要刪除訂閱者端的提取訂閱

  1. 連線到 SQL Server Management Studio 中的訂閱者,然後展開伺服器節點。

  2. 展開複寫資料夾,然後展開本機訂閱資料夾。

  3. 以滑鼠右鍵按下您要刪除的訂用帳戶,然後按兩下 [ 刪除]。

  4. 在確認對話框中,選取是否要連線到發行者以刪除訂閱資訊。 如果您清除 [ 連接到發行者 ] 複選框,您應該稍後聯機到發行者以刪除資訊。

使用 Transact-SQL

您可以使用複寫儲存過程以程式化方式刪除提取訂閱。 使用的預存程式將取決於訂閱所屬的發行集類型。

若要刪除快照式或交易式發行集的提取訂閱

  1. 在訂閱資料庫的訂閱者端,執行 sp_droppullsubscription (Transact-SQL) 。 指定 @publication@publisher@publisher_db

  2. 在發行者的出版資料庫上,執行 sp_dropsubscription(Transact-SQL)。 指定 @publication@subscriber。 針對 @article 指定all的值。 (選擇性)如果無法存取散發者,請為 @ignore_distributor指定 1 的值,以刪除訂閱,而不移除散發者端的相關物件。

若要刪除針對合併式發行集的提取訂閱

  1. 在訂閱資料庫的訂閱者端,執行 sp_dropmergepullsubscription(Transact-SQL)。 指定 @publication@publisher@publisher_db

  2. 在發行資料庫的發佈者中,執行 sp_dropmergesubscription(Transact-SQL)。 指定 @publication@subscriber@subscriber_db。 為@subscription_type指定值pull。 (選擇性)如果無法存取散發者,請為 @ignore_distributor指定 1 的值,以刪除訂閱,而不移除散發者端的相關物件。

範例 (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 is the batch executed at the Subscriber to drop 
-- a pull subscription to a transactional publication.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB     AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @publisher = $(PubServer);
SET @publicationDB = N'AdventureWorks2012';

USE [AdventureWorks2012Replica]
EXEC sp_droppullsubscription 
  @publisher = @publisher, 
  @publisher_db = @publicationDB, 
  @publication = @publication;
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 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 Subscriber to remove 
-- a merge pull subscription.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publication_db AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @publisher = $(PubServer);
SET @publication_db = N'AdventureWorks2012';

USE [AdventureWorks2012Replica]
EXEC sp_dropmergepullsubscription 
  @publisher = @publisher, 
  @publisher_db = @publication_db, 
  @publication = @publication;
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)以程式化方式刪除拉取訂閱。 您用來刪除提取訂閱的 RMO 類別取決於提取訂閱的發行集類型。

若要刪除快照式或交易式發行集的提取訂閱

  1. 使用 ServerConnection 類別建立訂閱者和發行者的連線。

  2. 建立 類別的 TransPullSubscription 實例,並設定 PublicationNameDatabaseNamePublisherNamePublicationDBName 屬性。 使用步驟 1 中的訂閱者連線來設定 ConnectionContext 屬性。

  3. 檢查IsExistingObject屬性以確認訂用帳戶是否存在。 如果這個屬性的值是 false,則步驟 2 中的訂閱屬性定義不正確或訂閱不存在。

  4. 呼叫 Remove 方法。

  5. 使用步驟 1 中的發行者連線,建立 類別的 TransPublication 實例。 指定 NameDatabaseNameConnectionContext

  6. 呼叫 LoadProperties 方法。 如果這個方法傳 false回 ,則步驟 5 中指定的屬性不正確,或發行集不存在於伺服器上。

  7. 呼叫 RemovePullSubscription 方法。 指定 subscribersubscriberDB 參數的訂閱者名稱和訂閱資料庫。

若要刪除合併式發行集的提取訂閱

  1. 使用 ServerConnection 類別建立訂閱者和發行者的連線。

  2. 建立 類別的 MergePullSubscription 實例,並設定 PublicationNameDatabaseNamePublisherNamePublicationDBName 屬性。 使用步驟 1 的連線來設定 ConnectionContext 屬性。

  3. IsExistingObject檢查 屬性以確認訂用帳戶是否存在。 如果這個屬性的值是 false,則步驟 2 中的訂閱屬性定義不正確或訂閱不存在。

  4. 呼叫 Remove 方法。

  5. 使用步驟 1 中的發行者連線,建立 類別的 MergePublication 實例。 指定 NameDatabaseNameConnectionContext

  6. 呼叫 LoadProperties 方法。 如果這個方法傳 false回 ,則步驟 5 中指定的屬性不正確,或發行集不存在於伺服器上。

  7. 呼叫 RemovePullSubscription 方法。 指定 subscribersubscriberDB 參數的訂閱者名稱和訂閱資料庫。

範例 (RMO)

此範例會刪除交易式發行集的提取訂閱,並移除發行者端的訂閱註冊。

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

//Create connections to the Publisher and Subscriber.
ServerConnection subscriberConn = new ServerConnection(subscriberName);
ServerConnection publisherConn = new ServerConnection(publisherName);

// Create the objects that we need.
TransPublication publication;
TransPullSubscription subscription;

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

    // Define the pull subscription.
    subscription = new TransPullSubscription();
    subscription.ConnectionContext = subscriberConn;
    subscription.PublisherName = publisherName;
    subscription.PublicationName = publicationName;
    subscription.PublicationDBName = publicationDbName;
    subscription.DatabaseName = subscriptionDbName;

    // Define the publication.
    publication = new TransPublication();
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;
    publication.ConnectionContext = publisherConn;

    // Delete the pull subscription, if it exists.
    if (subscription.IsExistingObject)
    {
        if (publication.LoadProperties())
        {
            // Remove the pull subscription registration at the Publisher.
            publication.RemovePullSubscription(subscriberName, subscriptionDbName);
        }
        else
        {
            // Do something here if the publication does not exist.
            throw new ApplicationException(String.Format(
                "The publication '{0}' does not exist on {1}.",
                publicationName, publisherName));
        }
        // 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
{
    subscriberConn.Disconnect();
    publisherConn.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 connections to the Publisher and Subscriber.
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)

' Create the objects that we need.
Dim publication As TransPublication
Dim subscription As TransPullSubscription

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

    ' Define the pull subscription.
    subscription = New TransPullSubscription()
    subscription.ConnectionContext = subscriberConn
    subscription.PublisherName = publisherName
    subscription.PublicationName = publicationName
    subscription.PublicationDBName = publicationDbName
    subscription.DatabaseName = subscriptionDbName

    ' Define the publication.
    publication = New TransPublication()
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName
    publication.ConnectionContext = publisherConn

    ' Delete the pull subscription, if it exists.
    If subscription.IsExistingObject Then
       
        If publication.LoadProperties() Then
            ' Remove the pull subscription registration at the Publisher.
            publication.RemovePullSubscription(subscriberName, subscriptionDbName)
        Else
            ' Do something here if the publication does not exist.
            Throw New ApplicationException(String.Format( _
             "The publication '{0}' does not exist on {1}.", _
             publicationName, publisherName))
        End If
        ' 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
    subscriberConn.Disconnect()
    publisherConn.Disconnect()
End Try

此範例會刪除合併式發行集的提取訂閱,並在發行者端移除訂閱註冊。

// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksSalesOrdersMerge";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";

//Create connections to the Publisher and Subscriber.
ServerConnection subscriberConn = new ServerConnection(subscriberName);
ServerConnection publisherConn = new ServerConnection(publisherName);

// Create the objects that we need.
MergePublication publication;
MergePullSubscription subscription;

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

    // Define the pull subscription.
    subscription = new MergePullSubscription();
    subscription.ConnectionContext = subscriberConn;
    subscription.PublisherName = publisherName;
    subscription.PublicationName = publicationName;
    subscription.PublicationDBName = publicationDbName;
    subscription.DatabaseName = subscriptionDbName;

    // Define the publication.
    publication = new MergePublication();
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;
    publication.ConnectionContext = publisherConn;

    // Delete the pull subscription, if it exists.
    if (subscription.IsExistingObject)
    {
        // Delete the pull subscription at the Subscriber.
        subscription.Remove();

        if (publication.LoadProperties())
        {
            publication.RemovePullSubscription(subscriberName, subscriptionDbName);
        }
        else
        {
            // Do something here if the publication does not exist.
            throw new ApplicationException(String.Format(
                "The publication '{0}' does not exist on {1}.",
                publicationName, publisherName));
        }
    }
    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
{
    subscriberConn.Disconnect();
    publisherConn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"

'Create connections to the Publisher and Subscriber.
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)

' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergePullSubscription

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

    ' Define the pull subscription.
    subscription = New MergePullSubscription()
    subscription.ConnectionContext = subscriberConn
    subscription.PublisherName = publisherName
    subscription.PublicationName = publicationName
    subscription.PublicationDBName = publicationDbName
    subscription.DatabaseName = subscriptionDbName

    ' Define the publication.
    publication = New MergePublication()
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName
    publication.ConnectionContext = publisherConn

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

        ' Delete the pull subscription at the Subscriber.
        subscription.Remove()

        If publication.LoadProperties() Then
            publication.RemovePullSubscription(subscriberName, subscriptionDbName)
        Else
            ' Do something here if the publication does not exist.
            Throw New ApplicationException(String.Format( _
             "The publication '{0}' does not exist on {1}.", _
             publicationName, publisherName))
        End If
    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
    subscriberConn.Disconnect()
    publisherConn.Disconnect()
End Try

另請參閱

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