アーティクルを定義する方法 (レプリケーション Transact-SQL プログラミング)

パブリケーションが作成された後、プログラムでレプリケーション ストアド プロシージャを使用してアーティクルを作成できます。アーティクルの作成に使用するストアド プロシージャは、定義するアーティクルのパブリケーションの種類によって決まります。詳細については、「パブリケーションを作成する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

注意

アーティクルの名前には % * [ ] | : " ? ' \ / < > を使用できません。データベース内のオブジェクトにこれらの文字が使用されているときに、そのオブジェクトをレプリケートする場合、そのオブジェクト名とは異なる名前をアーティクルに指定する必要があります。

スナップショット パブリケーションまたはトランザクション パブリケーションのアーティクルを定義するには

  1. パブリッシャー側のパブリケーション データベースに対して、sp_addarticle を実行します。アーティクルが属しているパブリケーションの名前を @publication に、アーティクルの名前を @article に、パブリッシュされるデータベース オブジェクトを @source_object に指定し、さらにその他のオプション パラメーターを指定します。dbo 以外の場合、@source_owner を使用してオブジェクトのスキーマ所有権を指定します。アーティクルがログベースのテーブル アーティクルでない場合、アーティクルの種類を @type に指定します。詳細については、「アーティクルの種類を指定する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

  2. テーブル内の行を行方向にフィルター選択する場合、またはアーティクルを表示する場合は、sp_articlefilter を使用してフィルター句を定義します。詳細については、「静的行フィルタを定義および変更する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

  3. テーブル内の列を列方向にフィルター選択する場合、またはアーティクルを表示する場合は、sp_articlecolumn を使用します。詳細については、「列フィルターを定義および変更する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

  4. アーティクルをフィルター選択する場合、sp_articleview を実行します。

  5. パブリケーションに既存のサブスクリプションがあり、immediate_sync 列の sp_helppublication0 を返す場合、sp_addsubscription を呼び出して既存の各サブスクリプションにアーティクルを追加する必要があります。

  6. パブリケーションに既存のプル サブスクリプションがある場合、パブリッシャーで sp_refreshsubscriptions を実行し、既存のプル サブスクリプションに対して新しいアーティクルのみを含む新しいスナップショットを作成します。

    注意

    スナップショットを使用して初期化しないサブスクリプションの場合、この処理は sp_addarticle によって実行されるため、sp_refreshsubscriptions を実行する必要はありません。

マージ パブリケーションのアーティクルを定義するには

  1. パブリッシャー側のパブリケーション データベースに対して、sp_addmergearticle を実行します。パブリケーションの名前を @publication に、アーティクルの名前を @article に、パブリッシュするオブジェクトを @source_object に指定します。テーブル行を行方向にフィルター選択する場合は、@subset_filterclause に値を指定します。詳細については、「マージ アーティクルに対してパラメーター化した行フィルターを定義および変更する方法 (レプリケーション Transact-SQL プログラミング)」および「静的行フィルタを定義および変更する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。アーティクルがテーブル アーティクルでない場合、@type にアーティクルの種類を指定します。詳細については、「アーティクルの種類を指定する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

  2. (省略可) パブリッシャー側のパブリケーション データベースに対して sp_addmergefilter を実行し、2 つのアーティクル間に結合フィルターを定義します。詳細については、「マージ アーティクル間の結合フィルターを定義および変更する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

  3. (省略可) パブリッシャー側のパブリケーション データベースに対して sp_mergearticlecolumn を実行し、テーブル列をフィルター選択します。詳細については、「列フィルターを定義および変更する方法 (レプリケーション Transact-SQL プログラミング)」を参照してください。

使用例

この例では、Product テーブルに基づいてトランザクション パブリケーションにアーティクルを定義します。アーティクルは行方向および列方向にフィルター選択されます。

DECLARE @publication    AS sysname;
DECLARE @table AS sysname;
DECLARE @filterclause AS nvarchar(500);
DECLARE @filtername AS nvarchar(386);
DECLARE @schemaowner AS sysname;
SET @publication = N'AdvWorksProductTran'; 
SET @table = N'Product';
SET @filterclause = N'[DiscontinuedDate] IS NULL'; 
SET @filtername = N'filter_out_discontinued';
SET @schemaowner = N'Production';

-- Add a horizontally and vertically filtered article for the Product table.
-- Manually set @schema_option to ensure that the Production schema 
-- is generated at the Subscriber (0x8000000).
EXEC sp_addarticle 
    @publication = @publication, 
    @article = @table, 
    @source_object = @table,
    @source_owner = @schemaowner, 
    @schema_option = 0x80030F3,
    @vertical_partition = N'true', 
    @type = N'logbased',
    @filter_clause = @filterclause;

-- (Optional) Manually call the stored procedure to create the 
-- horizontal filtering stored procedure. Since the type is 
-- 'logbased', this stored procedures is executed automatically.
EXEC sp_articlefilter 
    @publication = @publication, 
    @article = @table, 
    @filter_clause = @filterclause, 
    @filter_name = @filtername;

-- Add all columns to the article.
EXEC sp_articlecolumn 
    @publication = @publication, 
    @article = @table;

-- Remove the DaysToManufacture column from the article
EXEC sp_articlecolumn 
    @publication = @publication, 
    @article = @table, 
    @column = N'DaysToManufacture', 
    @operation = N'drop';

-- (Optional) Manually call the stored procedure to create the 
-- vertical filtering view. Since the type is 'logbased', 
-- this stored procedures is executed automatically.
EXEC sp_articleview 
    @publication = @publication, 
    @article = @table,
    @filter_clause = @filterclause;
GO

この例では、マージ パブリケーションのアーティクルを定義します。SalesOrderHeader アーティクルは SalesPersonID に基づいて静的にフィルター選択され、SalesOrderDetail アーティクルは SalesOrderHeader に基づいて結合フィルターが適用されます。

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