sp_addmergefilter (Transact-SQL)

适用于:SQL ServerAzure SQL 托管实例

添加新合并筛选以创建基于与另一个表的联接的分区。 此存储过程在发布服务器上对发布数据库执行。

Transact-SQL 语法约定

语法

sp_addmergefilter
    [ @publication = ] N'publication'
    , [ @article = ] N'article'
    , [ @filtername = ] N'filtername'
    , [ @join_articlename = ] N'join_articlename'
    , [ @join_filterclause = ] N'join_filterclause'
    [ , [ @join_unique_key = ] join_unique_key ]
    [ , [ @force_invalidate_snapshot = ] force_invalidate_snapshot ]
    [ , [ @force_reinit_subscription = ] force_reinit_subscription ]
    [ , [ @filter_type = ] filter_type ]
[ ; ]

参数

[ @publication = ] N'publication'

要在其中添加合并筛选器的发布的名称。 @publicationsysname,无默认值。

[ @article = ] N'article'

正在添加合并筛选器的项目的名称。 @articlesysname,无默认值。

[ @filtername = ] N'filtername'

筛选器的名称。 @filtername 是必需参数。 @filtername为 sysname,无默认值。

[ @join_articlename = ] N'join_articlename'

子项目由@article指定的父项目必须使用由@join_filterclause指定的联接子句联接,以确定符合合并筛选器筛选条件的子项目中的行。 @join_articlename为 sysname,无默认值。 项目必须位于@publication提供的出版物中。

[ @join_filterclause = ] N'join_filterclause'

必须用于联接由@article指定的子项目和由@join_article指定的父项的联接子项目,以确定符合合并筛选器的行。 @join_filterclause为 nvarchar(1000),没有默认值。

[ @join_unique_key = ] join_unique_key

指定子项目@article与父项目之间的联接@join_article是一对多、一对一、多对一或多对多。 @join_unique_key为 int,默认值为 0.

  • 0 指示多对一或多对多联接。

  • 1 指示一对一或一对多联接。

1联接列在@join_article中形成唯一键时,或者如果@join_filterclause位于@article中的外键和@join_article的主键之间。

注意

仅当父项目的基础表中的联接列具有保证唯一性的约束时,才将此参数 1 设置为此参数。 如果 @join_unique_key 设置为 1 错误,则可能会发生数据非收敛。

[ @force_invalidate_snapshot = ] force_invalidate_快照

确认此存储过程执行的操作可能会使现有快照失效。 @force_invalidate_快照为,默认值为 0.

  • 0指定对合并项目所做的更改不会导致快照无效。 如果该存储过程检测到更改确实需要新的快照,则会发生错误,并且不进行任何更改。

  • 1指定对合并项目所做的更改可能会导致快照无效,如果存在需要新快照的现有订阅,则授予现有快照标记为已过时和生成的新快照的权限。

[ @force_reinit_subscription = ] force_reinit_subscription

确认此存储过程所执行的操作是否需要重新初始化现有订阅。 @force_reinit_subscription为,默认值为 0.

  • 0 指定对合并项目所做的更改不会导致重新初始化订阅。 如果该存储过程检测到更改将需要重新初始化订阅,则会发生错误,并且不进行任何更改。

  • 1 指定对合并项目所做的更改会导致现有订阅重新初始化,并授予订阅重新初始化的权限。

[ @filter_type = ] filter_type

指定要添加的筛选类型。 @filter_typetinyint,可以是以下值之一。

说明
1 仅为联接筛选器。 支持 SQL Server Compact 订阅服务器所必需的。
2 仅为逻辑记录关系。
3 同时为联接筛选器和逻辑记录关系。

有关详细信息,请参阅通过逻辑记录对相关行的更改进行分组

返回代码值

0(成功)或 1(失败)。

备注

sp_addmergefilter用于合并副本 (replica)。

sp_addmergefilter 只能与表项目一起使用。 不支持查看和索引视图项目。

此过程还可用于在两篇文章之间添加逻辑关系,这些项目之间可能或不具有联接筛选器。 @filter_type 用于指定所添加的合并筛选器是联接筛选器、逻辑关系还是两者。

若要使用逻辑记录,发布和项目必须满足多个要求。 有关详细信息,请参阅通过逻辑记录对相关行的更改进行分组

通常,此选项用于对已发布的主键表的外键引用的项目,而主键表在其文章中定义了筛选器。 主键行的子集用于确定复制到订阅服务器的外键行。

当两个项目的源表共享同一表对象名称时,不能在两个已发布项目之间添加联接筛选器。 在这种情况下,即使这两个表都由不同的架构拥有并且具有唯一的项目名称,联接筛选器的创建也会失败。

当在一个表项目上同时使用参数化行筛选器和联接筛选器时,复制会确定行是否属于某个订阅服务器的分区。 它通过计算筛选函数或联接筛选器(使用 OR 运算符),而不是计算两个条件(使用 AND 运算符)的交集来执行此操作。

示例

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()';

-- Add a filtered article for the Employee table.
EXEC sp_addmergearticle 
  @publication = @publication, 
  @article = @table1, 
  @source_object = @table1, 
  @type = N'table', 
  @source_owner = @hrschema,
  @schema_option = 0x0004CF1,
  @description = N'article for the Employee table',
  @subset_filterclause = @filterclause;

-- Add an article for the SalesOrderHeader table that is filtered
-- based on Employee and horizontally filtered.
EXEC sp_addmergearticle 
  @publication = @publication, 
  @article = @table2, 
  @source_object = @table2, 
  @type = N'table', 
  @source_owner = @salesschema, 
  @vertical_partition = N'true',
  @schema_option = 0x0034EF1,
  @description = N'article for the SalesOrderDetail table';

-- Add an article for the SalesOrderDetail table that is filtered
-- based on SaledOrderHeader.
EXEC sp_addmergearticle 
  @publication = @publication, 
  @article = @table3, 
  @source_object = @table3, 
  @source_owner = @salesschema,
  @description = 'article for the SalesOrderHeader table', 
  @identityrangemanagementoption = N'auto', 
  @pub_identity_range = 100000, 
  @identity_range = 100, 
  @threshold = 80,
  @schema_option = 0x0004EF1;

-- Add all columns to the SalesOrderHeader article.
EXEC sp_mergearticlecolumn 
  @publication = @publication, 
  @article = @table2, 
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;

-- Remove the credit card Approval Code column.
EXEC sp_mergearticlecolumn 
  @publication = @publication, 
  @article = @table2, 
  @column = N'CreditCardApprovalCode', 
  @operation = N'drop', 
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;

-- Add a merge join filter between Employee and SalesOrderHeader.
EXEC sp_addmergefilter 
  @publication = @publication, 
  @article = @table2, 
  @filtername = N'SalesOrderHeader_Employee', 
  @join_articlename = @table1, 
  @join_filterclause = N'Employee.EmployeeID = SalesOrderHeader.SalesPersonID', 
  @join_unique_key = 1, 
  @filter_type = 1, 
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;

-- Add a merge join filter between SalesOrderHeader and SalesOrderDetail.
EXEC sp_addmergefilter 
  @publication = @publication, 
  @article = @table3, 
  @filtername = N'SalesOrderDetail_SalesOrderHeader', 
  @join_articlename = @table2, 
  @join_filterclause = N'SalesOrderHeader.SalesOrderID = SalesOrderDetail.SalesOrderID', 
  @join_unique_key = 1, 
  @filter_type = 1, 
  @force_invalidate_snapshot = 1, 
  @force_reinit_subscription = 1;
GO

权限

只有 sysadmin 固定服务器角色的成员db_owner固定数据库角色的成员才能执行sp_addmergefilter