방법: 병합 아티클에 대해 매개 변수가 있는 행 필터 정의 및 수정(복제 Transact-SQL 프로그래밍)

테이블 아티클을 만들 때 매개 변수가 있는 행 필터를 사용할 수 있습니다. 이러한 필터에서는 게시할 해당 데이터를 선택하기 위해 WHERE 절을 사용합니다. 정적 행 필터와는 달리 해당 절에 리터럴 값을 지정하는 대신 SUSER_SNAMEHOST_NAME 시스템 함수를 하나 또는 둘 모두 지정합니다. 자세한 내용은 매개 변수가 있는 행 필터를 참조하십시오. 복제 저장 프로시저를 사용하여 매개 변수가 있는 행 필터를 프로그래밍 방식으로 만들거나 수정할 수 있습니다.

병합 게시에서 아티클에 대한 매개 변수가 있는 행 필터를 정의하려면

  • 게시 데이터베이스의 게시자에서 sp_add_targetservergroup(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 - 아티클을 필터링하면 각 구독에 고유한 겹치지 않는 파티션이 생성됩니다.

다음 예에서는 병합 게시의 아티클 그룹을 정의합니다. 이 병합 게시에서는 LoginID 열을 기준으로 매개 변수가 있는 행 필터를 사용하여 자체 필터링되는 Employee 테이블에 대해 일련의 조인 필터로 아티클이 필터링됩니다. 동기화 중에 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'AdventureWorks2008R2';
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 [AdventureWorks2008R2];

-- Enable AdventureWorks2008R2 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 AdventureWorks2008R2.', 
  @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