Udostępnij za pośrednictwem


Jak Definiowanie i modyfikowanie filtru sprzężenia między artykuły korespondencji seryjnej (Programowanie replikacji Transact-SQL)

Scalania replikacja obsługuje łączyć filtrów, które są zazwyczaj używane w połączeniu z filtrami sparametryzowana rozszerzyć partycjonowanie do innych artykułów tabela pokrewnej tabela.Ten temat zawiera sparametryzowana filtr artykuł nadrzędnej z filtrami łączyć między niniejszego artykuł i artykuły pokrewne podrzędność.Dołącz filtry mogą być definiowane i zmodyfikowane programowo za pomocą procedur przechowywanych replikacja.

Aby zdefiniować filtr łączyć rozszerzyć filtr artykuł pokrewne artykuły w publikacja seryjnej

  1. Zdefiniować filtrowanie przyłączeniem się do artykułu, która jest również znany jako artykuł nadrzędnej.

  2. Wydawca na baza danych publikacja, wykonanie sp_addmergearticle (języka Transact-SQL) do zdefiniowania jednego lub więcej pokrewne artykuły, które są znane również jako podrzędność artykuły dla publikacja.Aby uzyskać więcej informacji, zobacz Jak Definiowanie artykuł (Programowanie replikacji Transact-SQL).

  3. Wydawca na baza danych publikacja, wykonanie sp_addmergefilter (języka Transact-SQL).Określ publikacja @, unikatową nazwę dla tego filtru dla @ NazwaFiltru, nazwę artykuł podrzędność utworzony w kroku 2 dla @ artykuł, nazwę artykuł nadrzędnego jest dołączana @ join_articlename, i jeden z następujących wartości @ join_unique_key:

    • 0 -wskazuje łączyć wiele do jednego lub wiele do wielu między artykułami nadrzędne i podrzędność.

    • 1 -wskazuje łączyć jednego lub jeden do wielu między artykułami nadrzędne i podrzędność.

    Definiuje to filtr łączyć między dwoma artykułów.

    PrzestrogaPrzestroga

    Ustawić tylko @ join_unique_key do 1 Jeśli masz ograniczenie na przyłączanie do kolumna w tabela źródłowa artykuł nadrzędnego, który gwarantuje unikatowość.Jeśli @ join_unique_key jest zestaw do 1 niepoprawnie, może nastąpić bez zbieżności danych.

Przykład

W tym przykładzie definiuje artykuł do publikacja korespondencji seryjnej, gdzie SalesOrderDetail artykuł tabeli jest filtrowana przeciwko SalesOrderHeader tabela jest sam przefiltrowane przy użyciu statyczny filtr wierszy.Aby uzyskać więcej informacji, zobacz Jak Definiowanie i modyfikowanie filtru wiersz statycznego (Programowanie replikacji Transact-SQL).

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

W tym przykładzie definiuje grupę artykuły w publikacja seryjnej gdzie artykuły są filtrowane z serii filtrów łączyć przeciwko pracownika tabela jest sam przefiltrowane przy użyciu sparametryzowany filtr wierszy na wartość HOST_NAME w LoginID kolumna.Aby uzyskać więcej informacji, zobacz Jak Definiowanie i modyfikowanie filtru sparametryzowana wiersz artykuł korespondencji seryjnej (Programowanie replikacji Transact-SQL).

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