如何:定義和修改合併發行項的參數化資料列篩選器 (複寫 Transact-SQL 程式設計)
在建立資料表發行項時,您可以使用參數化資料列篩選器。 這些篩選器會使用 WHERE 子句來選取要發行的適當資料。 您可以指定以下系統函數的其中一個或兩個,而不要在子句中指定常值 (如同對靜態資料列篩選的處理一樣): <SUSER_SNAME>和<HOST_NAME>。 如需詳細資訊,請參閱<參數化資料列篩選器>。 您可以使用複寫預存程序來以程式設計的方式建立及修改參數化資料列篩選器。
針對合併式發行集中的發行項定義參數化資料列篩選器
在發行集資料庫的發行者上,執行 sp_addmergearticle (Transact-SQL)。 指定 @publication、針對 @article 指定發行項的名稱、針對 @source_object 指定發行的資料表、針對 @subset_filterclause (不包括 WHERE) 指定定義參數化篩選器的 WHERE 子句,以及針對 @partition_options (用來描述從參數化資料列篩選器產生的資料分割類型) 指定下列其中一個值:
0 - 發行項的篩選是靜態的,或是不產生每個資料分割的唯一資料子集 (也就是「重疊」的資料分割)。
1 - 產生的資料分割是重疊的,而且在訂閱者上進行的更新並不會變更資料列所屬的資料分割。
2 - 發行項的篩選會產生非重疊的資料分割,但多個訂閱者可以接收相同的資料分割。
3 - 發行項的篩選會產生對每項訂閱而言都是唯一的非重疊資料分割。
針對合併式發行集中的發行項變更參數化資料列篩選器
在發行集資料庫的發行者上,執行 sp_changemergearticle。 指定 @publication、指定 @article、針對 @property 指定 subset_filterclause 值、針對 @value (不包括 WHERE) 指定定義參數化篩選器的運算式、針對 @force_invalidate_snapshot 和 @force_reinit_subscription 指定 1 的值。
如果此變更會產生不同的資料分割行為,則再次執行 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