如何:定義發行項 (複寫 Transact-SQL 程式設計)

在建立發行集之後,可以使用複寫預存程序來以程式設計的方式建立發行項。 要使用哪些預存程序來建立發行項,將取決於定義此發行項的發行集類型而定。 如需詳細資訊,請參閱<如何:建立發行集 (複寫 Transact-SQL 程式設計)>。

[!附註]

發行項名稱不能包含下列任何字元: % , * , [ , ] , | , : , " , ? , ' , \ , / , < , >. 如果資料庫中的物件包含這些字元的任何一個,而且您要複寫它們,則必須指定一個不同於物件名稱的發行項名稱。

為快照式或交易式發行集定義發行項

  1. 在發行集資料庫的發行者上,執行 sp_addarticle。 為 @publication 指定發行項所屬的發行集名稱、為 @article 指定發行項名稱、為 @source_object 指定發行的資料庫物件,以及指定其他任何選擇性參數。 使用 @source_owner 來指定此物件的結構描述擁有權 (如果不是 dbo)。 如果此發行項不是記錄式資料表發行項,請為 @type 指定發行項類型。如需詳細資訊,請參閱<如何:指定發行項類型 (複寫 Transact-SQL 程式設計)>。

  2. 若要以水平方式篩選資料表中的資料列或是檢視發行項,請使用 sp_articlefilter 來定義篩選子句。 如需詳細資訊,請參閱<如何:定義及修改靜態資料列篩選 (複寫 Transact-SQL 程式設計)>。

  3. 若要以垂直方式篩選資料表中的資料行或是檢視發行項,請使用 sp_articlecolumn。 如需詳細資訊,請參閱<如何:定義及修改資料行篩選 (複寫 Transact-SQL 程式設計)>。

  4. 如果發行項已經過篩選,請執行 sp_articleview

  5. 如果發行集有現有的訂閱,而且 sp_helppublicationimmediate_sync 資料行中傳回 0 的值,您就必須呼叫 sp_addsubscription,將此發行項加入到每一個現有的訂閱中。

  6. 如果發行集有現有的提取訂閱,請在發行者上執行 sp_refreshsubscriptions,針對只包含新發行項的現有提取訂閱建立新的快照集。

    [!附註]

    如果是未使用快照集初始化的訂閱,您就不需要執行 sp_refreshsubscriptions,因為這個程序是由 sp_addarticle 所執行。

為合併式發行集定義發行項

  1. 在發行集資料庫的發行者上,執行 sp_addmergearticle。 為 @publication 指定發行集的名稱、為 @article 指定發行項的名稱,以及為 @source_object 指定發行的物件。 若要以水平方式篩選資料表資料列,請指定 @subset_filterclause 的值。 如需詳細資訊,請參閱<如何:定義和修改合併發行項的參數化資料列篩選器 (複寫 Transact-SQL 程式設計)>和<如何:定義及修改靜態資料列篩選 (複寫 Transact-SQL 程式設計)>。 如果此發行項不是資料表發行項,請為 @type 指定發行項類型。 如需詳細資訊,請參閱<如何:指定發行項類型 (複寫 Transact-SQL 程式設計)>。

  2. (選擇性) 在發行集資料庫的發行者上,執行 sp_addmergefilter,以定義兩個發行項之間的聯結篩選。 如需詳細資訊,請參閱<如何:定義和修改合併發行項之間的聯結篩選 (複寫 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.EmployeeID = 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