本主题介绍如何使用 SQL Server Management Studio 或 Transact-SQL 在 SQL Server 2014 中的合并项目之间定义和修改联接筛选器。 合并复制支持联接筛选器,这些筛选器通常与参数化筛选器结合使用,以将表分区扩展到其他相关表项目。
本主题内容
准备工作:
若要定义和修改合并项目之间的联接筛选器,请使用:
在您开始之前
局限性与限制
若要创建联接筛选器,发布必须至少包含两个相关表。 联接筛选器扩展行筛选器;因此,必须先对一个表定义行筛选器,然后才能使用联接将筛选器扩展到另一个表。 定义一个联接筛选器后,如果发布包含其他相关表,则可以将此联接筛选器与其他联接筛选器一起扩展。
如果在初始化发布订阅后添加、修改或删除联接筛选器,则必须在进行更改后生成新的快照并重新初始化所有订阅。 有关属性更改要求的详细信息,请参阅 更改发布属性和文章属性。
建议
- 可以为一组表手动创建联接筛选器,也可以根据表上定义的外键和主键之间的关系自动生成筛选器。 有关自动生成一组联接筛选器的详细信息,请参阅 “在合并项目(SQL Server Management Studio)之间自动生成一组联接筛选器”。
使用 SQL Server Management Studio
在新发布向导的“筛选表行”页上定义、修改和删除联接筛选器,或“发布属性 - <发布>”对话框的“筛选行”页。 有关使用向导和访问对话框的详细信息,请参阅 创建发布 和 查看和修改发布属性。
定义联接筛选器
在“新建发布向导”的“筛选表行”页或“发布属性 - <发布>”的“筛选行”页中,在“筛选表”窗格中选择现有的行筛选器或联接筛选器。
单击“ 添加”,然后单击“ 添加联接”以扩展所选筛选器。
创建联接语句:选择 “使用生成器创建语句 ”或 手动写入该语句的联接。
如果选择使用生成器,请使用网格中的列(联接、 筛选表列、 运算符和 联接表列)生成联接语句。
网格中的每一列都包含一个下拉组合框,允许你选择两列和一个运算符(=、<>、<=、<、>=、>和like)。 结果显示在 “预览” 文本区域中。 如果联接涉及多个列对,请从 “组合 ”列中选择一个联接(AND 或 OR),然后输入两个列和一个运算符。
如果选择手动编写语句,请在 Join 语句文本区域中编写 join 语句 。 使用筛选表列列表框和联接表列列表框将列拖放到连接语句文本区域。
完整的联接语句如下所示:
SELECT <published_columns> FROM [Sales].[SalesOrderHeader] INNER JOIN [Sales].[SalesOrderDetail] ON [SalesOrderHeader].[SalesOrderID] = [SalesOrderDetail].[SalesOrderID]
JOIN 子句应使用由两部分构成的命名;不支持三部分命名和四部分命名。
指定联接选项:
如果在筛选表(父表)中用于联接的列是唯一的,请选择唯一键。
谨慎
选择此选项表示连接过滤器中的子表和父表之间存在一对一或一对多的关系。 只有在对子表中的联接列有一个保证唯一性的约束时,才选择此选项。 如果选项设置不正确,可能会发生数据无法收敛。
默认情况下,合并复制在同步期间以逐行方式处理更改。 若要对筛选表和作为单元处理的联接表的行进行相关更改,请选择 “逻辑记录 ”(仅Microsoft SQL Server 2005 及更高版本)。 仅当满足使用逻辑记录的项目和发布要求时,此选项才可用。 有关详细信息,请参阅“对 逻辑记录的相关行的分组更改”中的“使用逻辑记录的注意事项”。
单击 “确定” 。
如果位于 “发布属性 - <发布> ”对话框中,请单击“ 确定 ”以保存并关闭对话框。
修改联接筛选器
在“新建发布向导”的“筛选表行”页或“发布属性 - <发布>”的“筛选行”页上,在“筛选表”窗格中选择一个筛选器,然后单击“编辑”。
在 “编辑联接 ”对话框中,修改筛选器。
单击 “确定” 。
删除联接筛选器
- 在“新建发布向导”的“筛选表行”页或“发布属性 - <发布>”的“筛选行”页上,在“筛选表”窗格中选择一个筛选器,然后单击“删除”。 如果删除的联接筛选器本身由其他联接扩展,则也会删除这些联接。
使用 Transact-SQL
这些过程显示在父文章上的参数化过滤器,其中包含这篇文章与相关子文章之间的连接筛选器。 可以使用复制存储过程以编程方式定义和修改联接筛选器。
定义联接筛选器以将文章筛选器扩展到合并文献中的相关文章
定义要联接到的项目的筛选,也称为父项目。
有关使用参数化行筛选器筛选的项目,请参阅 “定义和修改合并项目的参数化行筛选器”。
有关使用静态行筛选器筛选的文章,请参阅 “定义和修改静态行筛选器”。
在发布服务器上,对发布数据库执行 sp_addmergearticle(Transact-SQL) 以定义发布中一个或多个相关项目(也称为子项目)。 有关详细信息,请参阅 定义项目。
在发布服务器上,对发布数据库执行sp_addmergefilter(Transact-SQL)。 指定 @publication 的唯一名称,此筛选器的唯一名称为 @filtername,步骤 2 中创建的子文章名称为 @article,要联接的父文章名称为 @join_articlename,以及 @join_unique_key 的以下值之一:
0 - 表示父项目和子项目之间的多对一或多对多联接。
1 - 指示父文章和子文章之间的一对一或一对多连接。
这定义了两篇文章之间的联接筛选器。
谨慎
如果父项目的基础表中的联接列具有保证唯一性的约束,则仅将 @join_unique_key 设置为 1 。 如果将 @join_unique_key 错误地设置为 1 ,则可能会导致数据不收敛。
示例 (Transact-SQL)
此示例定义了一个用于合并发布的项目,其中 SalesOrderDetail
表项目由 SalesOrderHeader
表进行筛选,而 SalesOrderHeader
表本身是使用静态行筛选器进行筛选的。 有关详细信息,请参阅 “定义和修改静态行筛选器”。
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.BusinessEntityID = 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
本示例在合并发布中定义了一组文章,其中文章通过一系列联接筛选器进行筛选,而Employee
表本身则使用参数化行筛选器根据LoginID列中的HOST_NAME值进行筛选。 有关详细信息,请参阅 “定义和修改合并项目的参数化行筛选器”。
-- To avoid storing the login and password in the script file, the value
-- is passed into SQLCMD as a scripting variable. 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".
--Add a new merge publication.
DECLARE @publicationdb AS sysname;
DECLARE @publication AS sysname;
DECLARE @table1 AS sysname;
DECLARE @table2 AS sysname;
DECLARE @filter AS sysname;
DECLARE @schema_hr AS sysname;
DECLARE @schema_sales AS sysname;
SET @publicationdb = N'AdventureWorks2012';
SET @publication = N'AdvWorksSalesPersonMerge';
SET @table1 = N'Employee';
SET @table2 = N'SalesPerson';
SET @filter = N'SalesPerson_Employee';
SET @schema_hr = N'HumanResources';
SET @schema_sales = N'Sales';
USE [AdventureWorks2012];
-- Enable AdventureWorks2012 for merge replication.
EXEC sp_replicationdboption
@dbname = @publicationdb,
@optname = N'merge publish',
@value = N'true';
-- Create new merge publication with Subscriber requested snapshot
-- and using the default agent schedule.
EXEC sp_addmergepublication
@publication = @publication,
@description = N'Merge publication of AdventureWorks2012.',
@allow_subscriber_initiated_snapshot = N'true',
@publication_compatibility_level = N'90RTM';
-- Create a new snapshot job for the publication, using the default schedule.
-- Pass credentials at runtime using sqlcmd scripting variables.
EXEC sp_addpublication_snapshot
@publication = @publication,
@job_login = $(login),
@job_password = $(password);
-- Add an article for the Employee table,
-- which is horizontally partitioned using
-- a parameterized row filter.
EXEC sp_addmergearticle
@publication = @publication,
@article = @table1,
@source_owner = @schema_hr,
@source_object = @table1,
@type = N'table',
@description = 'contains employee information',
@subset_filterclause = N'[LoginID] = HOST_NAME()';
-- Add an article for the SalesPerson table,
-- which is partitioned based on a join filter.
EXEC sp_addmergearticle
@publication = @publication,
@article = @table2,
@source_owner = @schema_sales,
@source_object = @table2,
@type = N'table',
@description = 'contains salesperson information';
-- Add a join filter between the two articles.
EXEC sp_addmergefilter
@publication = @publication,
@article = @table1,
@filtername = @filter,
@join_articlename = @table2,
@join_filterclause = N'[Employee].[BusinessEntityID] = [SalesPerson].[SalesPersonID]',
@join_unique_key = 1,
@filter_type = 1;
GO
-- Start the agent job to generate the full snapshot for the publication.
-- The filtered data snapshot is generated automatically the first time
-- the subscription is synchronized.
DECLARE @publication AS sysname;
SET @publication = N'AdvWorksSalesPersonMerge';
EXEC sp_startpublication_snapshot
@publication = @publication;
GO
另请参阅
连接过滤器
参数化行筛选器
更改发布和项目属性
筛选用于合并复制的发布数据
复制系统存储过程概念
定义合并表项目间的逻辑记录关系
定义和修改合并项目的参数化行筛选器