如何:定義及修改資料行篩選 (複寫 Transact-SQL 程式設計)

在建立資料表發行項時,您可以定義要將哪些資料行包含在發行項中,並在定義發行項之後變更資料行。您可以使用複寫預存程序來以程式設計的方式建立及修改篩選的資料行。某些類型的資料行 (如 identityrowguid 資料行) 無法從已發行的發行項中移除。如需詳細資訊,請參閱<篩選發行的資料>主題中的「資料行篩選」。

[!附註]

下列程序假設基礎資料表尚未變更。如需有關複寫已發行之資料表的資料定義語言 (DDL) 變更的詳細資訊,請參閱<對發行集資料庫進行結構描述變更>。

針對快照式或交易式發行集中發行的發行項定義資料行篩選

  1. 定義要篩選的發行項。如需詳細資訊,請參閱<如何:定義發行項 (複寫 Transact-SQL 程式設計)>。

  2. 在發行集資料庫的發行者上,執行 sp_articlecolumn。這樣會定義要在發行項中包含或移除的資料行。

    • 如果只要從包含許多資料行的資料表發行一些資料行,請針對加入的每一個資料行執行 sp_articlecolumn 一次。針對 @column 指定資料行名稱,並針對 @operation 指定 add 的值。

    • 如果要在包含許多資料行的資料表中發行大多數的資料行,請執行 sp_articlecolumn、針對 @column 指定 null 的值,以及針對 @operation 指定 add 的值來加入所有資料行。然後針對每一個排除的資料行執行 sp_articlecolumn 一次,針對 @operation 指定 drop 的值,並針對 @column 指定排除的資料行名稱。

  3. 在發行集資料庫的發行者上,執行 sp_articleview。針對 @publication 指定發行集名稱,並針對 @article 指定篩選的發行項名稱。這樣會針對篩選的發行項建立同步處理物件。

針對快照式或交易式發行集中發行的發行項變更要包含其他資料行的資料行篩選

  1. 在發行集資料庫的發行者上,針對每一個加入的資料行執行 sp_articlecolumn 一次。針對 @column 指定資料行名稱,並針對 @operation 指定 add 的值。

  2. 在發行集資料庫的發行者上,執行 sp_articleview。針對 @publication 指定發行集名稱,並針對 @article 指定篩選的發行項名稱。如果此發行集有現有的訂閱,請針對 @change_active 指定 1 的值。這樣會針對篩選的發行項重新建立同步處理物件。

  3. 針對此發行集重新執行快照集代理程式作業,以產生更新的快照集。

  4. 重新初始化訂閱。如需詳細資訊,請參閱<如何:重新初始化訂閱 (複寫 Transact-SQL 程式設計)>。

針對快照式或交易式發行集中發行的發行項變更要移除資料行的資料行篩選

  1. 在發行集資料庫的發行者上,針對每一個移除的資料行執行 sp_articlecolumn 一次。針對 @column 指定資料行名稱,並針對 @operation 指定 drop 的值。

  2. 在發行集資料庫的發行者上,執行 sp_articleview。針對 @publication 指定發行集名稱,並針對 @article 指定篩選的發行項名稱。如果此發行集有現有的訂閱,請針對 @change_active 指定 1 的值。這樣會針對篩選的發行項重新建立同步處理物件。

  3. 針對此發行集重新執行快照集代理程式作業,以產生更新的快照集。

  4. 重新初始化訂閱。如需詳細資訊,請參閱<如何:重新初始化訂閱 (複寫 Transact-SQL 程式設計)>。

針對合併式發行集中發行的發行項定義資料行篩選

  1. 定義要篩選的發行項。如需詳細資訊,請參閱<如何:定義發行項 (複寫 Transact-SQL 程式設計)>。

  2. 在發行集資料庫的發行者上,執行 sp_mergearticlecolumn。這樣會定義要在發行項中包含或移除的資料行。

    • 如果只要從包含許多資料行的資料表發行一些資料行,請針對加入的每一個資料行執行 sp_mergearticlecolumn 一次。針對 @column 指定資料行名稱,並針對 @operation 指定 add 的值。

    • 如果要在包含許多資料行的資料表中發行大多數的資料行,請執行 sp_mergearticlecolumn、針對 @column 指定 null 的值,以及針對 @operation 指定 add 的值來加入所有資料行。然後針對每一個排除的資料行執行 sp_mergearticlecolumn 一次,針對 @operation 指定 drop 的值,並針對 @column 指定排除的資料行名稱。

針對合併式發行集中發行的發行項變更要包含其他資料行的資料行篩選

  1. 在發行集資料庫的發行者上,針對每一個加入的資料行執行 sp_mergearticlecolumn 一次。針對 @column 指定資料行名稱、針對 @operation 指定 add 的值,以及針對 @force_invalidate_snapshot@force_reinit_subscription 指定 1 的值。

  2. 針對此發行集重新執行快照集代理程式作業,以產生更新的快照集。

  3. 重新初始化訂閱。如需詳細資訊,請參閱<如何:重新初始化訂閱 (複寫 Transact-SQL 程式設計)>。

針對合併式發行集中發行的發行項變更要移除資料行的資料行篩選

  1. 在發行集資料庫的發行者上,針對每一個移除的資料行執行 sp_mergearticlecolumn 一次。針對 @column 指定資料行名稱、針對 @operation 指定 drop 的值,以及針對 @force_invalidate_snapshot@force_reinit_subscription 指定 1 的值。

  2. 針對此發行集重新執行快照集代理程式作業,以產生更新的快照集。

  3. 重新初始化訂閱。如需詳細資訊,請參閱<如何:重新初始化訂閱 (複寫 Transact-SQL 程式設計)>。

範例

在此交易式複寫範例中,會從根據 Product 資料表的發行項中移除 DaysToManufacture 資料行。

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 資料表的發行項中移除 CreditCardApprovalCode 資料行。

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