如何删除项目(复制 Transact-SQL 编程)

可以使用复制存储过程以编程方式来删除项目。 使用的存储过程取决于项目所属的发布的类型。 有关删除项目时使用的条件以及删除项目是否需要新的快照或重新初始化订阅的信息,请参阅向现有发布添加项目和从中删除项目

从快照或事务发布中删除项目

  1. 执行 sp_droparticle (Transact-SQL) 以从发布(由 @publication 指定)中删除项目(由 @article 指定)。 将 @force_invalidate_snapshot 的值指定为 1

  2. (可选)若要从数据库完全删除已发布的对象,请在发布服务器上对发布数据库执行 DROP <objectname> 命令。

从合并发布删除项目

  1. 执行 sp_dropmergearticle (Transact-SQL) 以从发布(由 @publication 指定)中删除项目(由 @article 指定)。 如有必要,可将 @force_invalidate_snapshot 的值指定为 1 并将 @force_reinit_subscription 的值指定为 1

  2. (可选)若要从数据库完全删除已发布的对象,请在发布服务器上对发布数据库执行 DROP <objectname> 命令。

示例

下面的示例将从事务发布中删除项目。 因为此更改使现有快照失效,所以 @force_invalidate_snapshot 参数的值将会指定为 1

DECLARE @publication AS sysname;
DECLARE @article AS sysname;
SET @publication = N'AdvWorksProductTran'; 
SET @article = N'Product'; 

-- Drop the transactional article.
USE [AdventureWorks]
EXEC sp_droparticle 
  @publication = @publication, 
  @article = @article,
  @force_invalidate_snapshot = 1;
GO

下面的示例将从合并发布中删除两个项目。 因为这些更改使现有快照失效,所以 @force_invalidate_snapshot 参数的值将会指定为 1

DECLARE @publication AS sysname;
DECLARE @table1 AS sysname;
DECLARE @table2 AS sysname;
DECLARE @table3 AS sysname;
DECLARE @salesschema AS sysname;
DECLARE @hrschema AS sysname;
DECLARE @filterclause AS nvarchar(1000);
SET @publication = N'AdvWorksSalesOrdersMerge'; 
SET @table1 = N'Employee'; 
SET @table2 = N'SalesOrderHeader'; 
SET @table3 = N'SalesOrderDetail'; 
SET @salesschema = N'Sales';
SET @hrschema = N'HumanResources';
SET @filterclause = N'Employee.LoginID = HOST_NAME()';

-- Drop the merge join filter between SalesOrderHeader and SalesOrderDetail.
EXEC sp_dropmergefilter 
  @publication = @publication, 
  @article = @table3, 
  @filtername = N'SalesOrderDetail_SalesOrderHeader', 
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;

-- Drops the merge join filter between Employee and SalesOrderHeader.
EXEC sp_dropmergefilter 
  @publication = @publication, 
  @article = @table2, 
  @filtername = N'SalesOrderHeader_Employee', 
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;

-- Drops the article for the SalesOrderDetail table.
EXEC sp_dropmergearticle 
  @publication = @publication, 
  @article = @table3,
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;

-- Drops the article for the SalesOrderHeader table.
EXEC sp_dropmergearticle 
  @publication = @publication, 
  @article = @table2, 
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;

-- Drops the article for the Employee table.
EXEC sp_dropmergearticle 
  @publication = @publication, 
  @article = @table1,
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;
GO

DECLARE @publication AS sysname;
DECLARE @article1 AS sysname;
DECLARE @article2 AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @article1 = N'SalesOrderDetail'; 
SET @article2 = N'SalesOrderHeader'; 

-- Remove articles from a merge publication.
USE [AdventureWorks]
EXEC sp_dropmergearticle 
  @publication = @publication, 
  @article = @article1,
  @force_invalidate_snapshot = 1;
EXEC sp_dropmergearticle 
  @publication = @publication, 
  @article = @article2,
  @force_invalidate_snapshot = 1;
GO