如何为合并项目定义和修改参数化行筛选器(复制 Transact-SQL 编程)

创建表项目时,可以使用参数化行筛选器。 这些筛选器使用 WHERE 子句来选择要发布的相应数据。 不要在该子句中指定文字值(像在静态行筛选器中那样),而是指定以下一个或两个系统函数:SUSER_SNAMEHOST_NAME。 有关详细信息,请参阅参数化行筛选器。 可以使用复制存储过程以编程的方式创建和修改参数化行筛选器。

为合并发布中的项目定义参数化行筛选器

  • 在发布服务器的发布数据库中,执行 sp_addmergearticle (Transact-SQL)。 指定 @publication,为 @article 指定项目名称,为 @source_object 指定要发布的表,为 @subset_filterclause 指定定义参数化筛选器的 WHERE 子句(不包括 WHERE),并为 @partition_options(说明从参数化行筛选器得出的分区类型)指定下列值之一:

    • 0 - 对项目的筛选是静态的,或者不为每个分区生成唯一的数据子集(“重叠”分区)。

    • 1 - 导致分区重叠,并且在订阅服务器上所做的更新不能更改行所属的分区。

    • 2 - 对项目的筛选将生成不重叠分区,但多个订阅服务器可以接收相同的分区。

    • 3 - 对项目的筛选将为每个订阅生成唯一的不重叠分区。

更改合并发布中的项目的参数化行筛选器

  1. 在发布服务器上,对发布数据库执行 sp_changemergearticle。 指定 @publication@article,为 @property 指定 subset_filterclause 值,为 @value 指定定义参数化筛选器的表达式(不包括 WHERE),并将 @force_invalidate_snapshot@force_reinit_subscription 的值指定为 1

  2. 如果此更改导致不同的分区行为,则再次执行 sp_changemergearticle。 指定 @publication@article,为 @property 指定 partition_options 值,并为 @value 指定最适合的选项,该选项可以为下列选项之一:

    • 0 - 对项目的筛选是静态的,或者不为每个分区生成唯一的数据子集(“重叠”分区)。

    • 1 - 导致分区重叠,并且在订阅服务器上所做的更新不能更改行所属的分区。

    • 2 - 对项目的筛选将生成不重叠分区,但多个订阅服务器可以接收相同的分区。

    • 3 - 对项目的筛选将为每个订阅生成唯一的不重叠分区。

示例

此示例在合并发布中定义一组项目,其中的项目是使用一系列联接筛选器基于 Employee 表筛选的,而该表则是使用参数化行筛选器基于 LoginID 列进行自身筛选的。 在同步期间,由 HOST_NAME 函数返回的值将被覆盖。 有关详细信息,请参阅主题参数化行筛选器中的“覆盖 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'AdventureWorks';
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 [AdventureWorks];

-- Enable AdventureWorks 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 AdventureWorks.', 
  @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].[EmployeeID] = [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