Aracılığıyla paylaş


Nasıl yapılır: Tanımla ve birleştirme makaleleri (çoğaltma Transact-SQL programlama) arasındaki birleşim filtre Değiştir

Genellikle birlikte parametreli filtreleri ile genişletmek için diğer ilişkili tablo makaleleri bölümleme tablosu için kullanılan çoğaltma destekler birleştirmek filtreleri birleştirmek.Bu konu, bu makale ve ilgili alt makaleleri arasında birleştirmek filtre ile üst makale üzerinde parametreli filtre gösterir.Birleştirme filtreleri tanımlanabilir ve çoğaltma depolanmış yordamları kullanarak programsal yoldan değiştirilebilir.

Tanımlamak için bir birleştirmek filtresi ilgili makalelerde birleştirmek yayın bir makale filtre genişletmek için

  1. Üst makale olarak da bilinen olan için birleştirilmiş makale için süzme tanımlayın.

  2. yayın veritabanı üzerinde Yayımcı tarafında yürütmek sp_addmergearticle (Transact-sql) bir veya daha fazla ilgili makaleler, yayın alt makaleleri olarak da bilinen olduğu tanımlamak içinDaha fazla bilgi için bkz: Nasıl yapılır: Bir makalesi (çoğaltma Transact-SQL programlama) tanımla.

  3. yayın veritabanı üzerinde Yayımcı tarafında yürütmek sp_addmergefilter (Transact-sql).Belirtmek @ yayın, bu filtre için benzersiz bir ad @ FiltreAdı, oluşturulan alt makale adı için adım 2'de @ makale, için için Katılınan üst makale adını @ join_articlename, ve değerleri için aşağıdakilerden birini @ join_unique_key:

    • 0 -üst ve alt makaleleri arasında bir çok bir veya çok çok birleştirmek gösterir.

    • 1 -üst ve alt makaleleri arasında bire bir veya bir-çok birleştirmek gösterir.

    Bu tanımlar bir birleşim filtresi iki makaleleri arasında.

    Dikkat notuDikkat

    Sadece @ join_unique_key için 1 katılan sütun temel sınırlama varsa, temel alınan tablo üst makale, Benzersizliği güvence altına alır.If @join_unique_key is set to 1 incorrectly, non-convergence of data may occur.

Örnek

Bu örnek, birleştirme yayın için bir makale tanımlar nerede SalesOrderDetail tablo makale filtre karşı SalesOrderHeader tablo yani kendisini statik satır filtresi kullanarak filtre uygulanmış.Daha fazla bilgi için bkz: Nasıl yapılır: Tanımla ve statik satır filtresi (çoğaltma Transact-SQL programlama) değiştirme.

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

Bu örnek nerede makaleleri filtre bir dizi karşı birleştirmek filtreleri ile birleştirmek yayın makaleleri grubu tanımlayan çalışan tablo yani kendisini değerine bağlı parametreli satır filtresi kullanarak filtre host_name içinde LoginID sütun.Daha fazla bilgi için bkz: Nasıl yapılır: Tanımla ve birleştirme makalesi (çoğaltma Transact-SQL programlama) parametreli satır filtre Değiştir.

-- 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