本主题介绍如何使用 SQL Server Management Studio 或 Transact-SQL 在 SQL Server 2014 中定义和修改列筛选器。
本主题内容
准备工作:
若要定义和修改列筛选器,请使用:
在您开始之前
局限性与限制
- 无法筛选某些列;有关详细信息,请参阅 “筛选已发布数据”。 如果在初始化订阅后修改列筛选器,则必须在进行更改后生成新的快照并重新初始化所有订阅。 有关属性更改要求的详细信息,请参阅 更改发布属性和文章属性。
使用 SQL Server Management Studio
在新出版物向导的“文章”页上定义列筛选器。 有关使用新建发布向导的详细信息,请参阅 “创建发布”。
在“发布属性 - <发布>”对话框的“文章”页上定义和修改列筛选器。 有关发布和项目属性的详细信息,请参阅 “查看和修改发布属性”。
定义列筛选器
在“新出版物向导”的“文章”页上,展开“要发布的对象”窗格中要筛选的表。
清除要筛选的每个列旁边的复选框。
修改列筛选
在“发布属性 - <发布>”对话框的“文章”页面上,展开要筛选的表以在“要发布的对象”窗格中。
清除要筛选的每个列旁边的复选框,并确保为应包含在文章中的每个列选中该复选框。
单击 “确定” 。
使用 Transact-SQL
创建表项目时,可以定义要在项目中包含的列,并在定义项目后更改列。 可以使用复制存储过程以编程方式创建和修改筛选列。
注释
以下过程假定基础表未更改。 有关将数据定义语言(DDL)更改复制到已发布表的信息,请参阅 对发布数据库进行架构更改。
为在快照或事务发布中发布的文章定义列筛选器
定义要筛选的项目。 有关详细信息,请参阅 定义项目。
在发布者的发布数据库中,执行 sp_articlecolumn。 这将定义要包含或从文章中删除的列。
如果只发布一个包含许多列的表中的几列,请针对每个要添加的列执行一次 sp_articlecolumn。 指定@column的列名,并将添加作为@operation的值。
如要发布包含多个列的表中的大部分列,请执行sp_articlecolumn,为@column指定null值,并为@operation指定add值,以添加所有列。 然后对要排除的每个列分别执行sp_articlecolumn,为@operation指定删除值,并为@column指定排除的列名。
在发布者的发布数据库上,执行 sp_articleview。 指定 @publication 的发布名称,并为 @article指定筛选项目的名称。 这将为筛选的项目创建同步对象。
更改列筛选器以在快照或事务性发布中包含文章的其他列。
在发布者处的发布数据库上,对要添加的每个列执行 sp_articlecolumn 一次。 指定@column的列名,并为@operation指定值添加。
在发布服务器上,对发布数据库执行 sp_articleview。 指定 @publication 的发布名称,并为 @article指定筛选项目的名称。 如果出版物具有现有订阅,请为@change_active指定值 1。 这会为筛选的文章重新创建同步对象。
重新运行发布的快照代理作业以生成更新的快照。
重新初始化订阅。 有关详细信息,请参阅 重新初始化订阅。
将列筛选器更改为删除快照发布或事务发布中的文章列
在发布服务器的发布数据库上,针对每个要删除的列执行一次sp_articlecolumn。 指定@column的列名,并为@operation指定删除值。
在发布服务器上,对出版物数据库执行 sp_articleview。 指定 @publication 的发布名称,并为 @article指定筛选项目的名称。 如果该出版物具有现有订阅,请为@change_active指定值1。 这会为筛选的文章重新创建同步对象。
重新运行发布物的快照代理作业以生成更新的快照。
重新初始化订阅。 有关详细信息,请参阅 重新初始化订阅。
为合并发布中的文章定义列筛选器
定义要筛选的项目。 有关详细信息,请参阅 定义项目。
在发布者的发布数据库上,执行 sp_mergearticlecolumn。 这将定义要包含或从文章中删除的列。
如果只从包含多个列的表中发布几列,请对要添加的每一列执行 sp_mergearticlecolumn 一次。 指定@column的列名,并为@operation指定值添加。
如果要发布具有多列的表中的大多数列,请执行sp_mergearticlecolumn,为@column指定null值,并为@operation指定add值以添加所有列。 然后对要排除的每个列单独执行sp_mergearticlecolumn,指定值为删除给@operation,并为@column指定排除的列名称。
更改列筛选器以添加更多列来包含在合并出版物中发布的文章
在发布服务器上,对要添加的每个列执行 sp_mergearticlecolumn 一次。 指定@column的列名称,@operation的值为add,以及@force_invalidate_snapshot和@force_reinit_subscription的值为1。
重新运行发布的快照代理任务以生成更新的快照。
重新初始化订阅。 有关详细信息,请参阅 重新初始化订阅。
更改列筛选器以删除合并发布中发布的项目的列
在发布者的发布数据库上,每删除一列执行一次 sp_mergearticlecolumn。 指定@column的列名称、@operation的值为drop,@force_invalidate_snapshot和@force_reinit_subscription的值为1。
重新运行发布的快照代理作业以生成更新的快照。
重新初始化订阅。 有关详细信息,请参阅 重新初始化订阅。
示例 (Transact-SQL)
在此事务复制示例中, DaysToManufacture
该列将从基于 Product
表的文章中删除。
DECLARE @publication AS sysname;
DECLARE @table AS sysname;
DECLARE @filterclause AS nvarchar(500);
DECLARE @filtername AS nvarchar(386);
DECLARE @schemaowner AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @table = N'Product';
SET @filterclause = N'[DiscontinuedDate] IS NULL';
SET @filtername = N'filter_out_discontinued';
SET @schemaowner = N'Production';
-- Add a horizontally and vertically filtered article for the Product table.
-- Manually set @schema_option to ensure that the Production schema
-- is generated at the Subscriber (0x8000000).
EXEC sp_addarticle
@publication = @publication,
@article = @table,
@source_object = @table,
@source_owner = @schemaowner,
@schema_option = 0x80030F3,
@vertical_partition = N'true',
@type = N'logbased',
@filter_clause = @filterclause;
-- (Optional) Manually call the stored procedure to create the
-- horizontal filtering stored procedure. Since the type is
-- 'logbased', this stored procedures is executed automatically.
EXEC sp_articlefilter
@publication = @publication,
@article = @table,
@filter_clause = @filterclause,
@filter_name = @filtername;
-- Add all columns to the article.
EXEC sp_articlecolumn
@publication = @publication,
@article = @table;
-- Remove the DaysToManufacture column from the article
EXEC sp_articlecolumn
@publication = @publication,
@article = @table,
@column = N'DaysToManufacture',
@operation = N'drop';
-- (Optional) Manually call the stored procedure to create the
-- vertical filtering view. Since the type is 'logbased',
-- this stored procedures is executed automatically.
EXEC sp_articleview
@publication = @publication,
@article = @table,
@filter_clause = @filterclause;
GO
在此合并复制示例中, CreditCardApprovalCode
该列将从基于 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