共用方式為


如何:刪除發送訂閱 (複寫 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。這樣會移除訂閱資料庫中的合併中繼資料。

範例

此範例會刪除交易式發行集的發送訂閱。

-- 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 [AdventureWorks2008R2;]
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'AdventureWorks2008R2Replica';

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