如何:使用參數化篩選建立合併式發行集的快照集 (複寫 Transact-SQL 程式設計)

在使用參數化篩選產生發行集的快照集時,您必須先產生一個標準或結構描述、包含所有發行之資料的快照集及訂閱的訂閱者中繼資料。如需詳細資訊,請參閱<如何:建立初始快照集 (複寫 Transact-SQL 程式設計)>。在您建立結構描述快照集之後,您可以產生包含發行資料之訂閱者特有資料分割的快照集。

您可以使用預存程序和快照集代理程式進行以下作業:

  • 允許訂閱者在首次執行同步處理時,要求產生和套用快照集。

  • 為每個資料分割預先產生快照集。

  • 以手動方式為每一個訂閱者產生快照集。

    安全性注意事項:可能的話,會在執行階段提示使用者輸入安全性認證。如果您必須將認證儲存在指令碼檔案中,則必須維護這個檔案的安全性,使他人無法在未獲授權的情況下擅自存取。

建立可讓訂閱者初始化快照集產生和傳遞的發行集

  1. 在發行集資料庫的發行者上,執行 sp_addmergepublication (Transact-SQL)。指定下列參數:

    • @publication 設定為發行集的名稱。

    • @allow_subscriber_initiated_snapshot 設定為 true 的值,這樣可讓訂閱者起始快照集處理。

    • (選擇性) 將 @max_concurrent_dynamic_snapshots 設定為可以並行執行的動態快照集處理序數目。如果正在執行最大的處理序數目,而且訂閱者嘗試產生快照集,則會將此處理序置於佇列中。根據預設,並行處理序的數目沒有任何限制。

  2. 在發行者上,執行 sp_addpublication_snapshot (Transact-SQL)。針對 @publication 指定步驟 1 中所用的發行集名稱,並針對 @job_login@password 指定執行複寫快照集代理程式所用的 Microsoft Windows 認證。如果代理程式會在與「發行者」時連接時使用「SQL Server 驗證」,則也必須指定 @publisher_security_mode 的值 0,以及 @publisher_login@publisher_password 的 Microsoft SQL Server 登入資訊。這麼做會為發行集建立快照集代理程式作業。如需有關產生初始快照集以及為快照集代理程式定義自訂排程的詳細資訊,請參閱<如何:建立初始快照集 (複寫 Transact-SQL 程式設計)>。

    安全性注意事項安全性注意事項

     當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_login 和 job_password) 都會以純文字的方式傳給散發者。您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。如需詳細資訊,請參閱<加密 SQL Server 的連接>。

  3. 執行 sp_addmergearticle (Transact-SQL) 將發行項加入發行集中。必須針對發行集中的每一個發行項執行一次此預存程序。在使用參數化篩選時,您必須使用 @subset_filterclause 參數來指定一或多個發行項的參數化資料列篩選器。如需詳細資訊,請參閱<如何:定義和修改合併發行項的參數化資料列篩選器 (複寫 Transact-SQL 程式設計)>。

  4. 如果將根據參數化資料列篩選器來篩選其他發行項,請執行 sp_addmergefilter (Transact-SQL) 來定義兩個發行項之間的聯結或邏輯記錄關聯性。必須針對每一個定義的關聯性執行一次此預存程序。如需詳細資訊,請參閱<如何:定義和修改合併發行項之間的聯結篩選 (複寫 Transact-SQL 程式設計)>。

  5. 當合併代理程式要求此快照集初始化訂閱者時,會自動產生要求之訂閱資料分割的快照集。

建立發行集及預先產生或自動重新整理快照集

  1. 執行 sp_addmergepublication (Transact-SQL) 來建立發行集。如需詳細資訊,請參閱<如何:建立發行集 (複寫 Transact-SQL 程式設計)>。

  2. 在發行者上,執行 sp_addpublication_snapshot (Transact-SQL)。針對 @publication 指定步驟 1 中所使用的發行集名稱,以及針對 @job_login@password 指定快照集代理程式執行時所使用的 Windows 認證。如果代理程式會在與「發行者」時連接時使用「SQL Server 驗證」,則也必須指定 @publisher_security_mode 的值 0,以及 @publisher_login@publisher_password 的 SQL Server 登入資訊。這麼做會為發行集建立快照集代理程式作業。如需有關產生初始快照集以及為快照集代理程式定義自訂排程的詳細資訊,請參閱<如何:建立初始快照集 (複寫 Transact-SQL 程式設計)>。

    安全性注意事項安全性注意事項

     當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_login 和 job_password) 都會以純文字的方式傳給散發者。您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。如需詳細資訊,請參閱<加密 SQL Server 的連接>。

  3. 執行 sp_addmergearticle (Transact-SQL) 將發行項加入發行集中。必須針對發行集中的每一個發行項執行一次此預存程序。在使用參數化篩選時,您必須使用 @subset_filterclause 參數來指定一個發行項的參數化資料列篩選器。如需詳細資訊,請參閱<如何:定義和修改合併發行項的參數化資料列篩選器 (複寫 Transact-SQL 程式設計)>。

  4. 如果將根據參數化資料列篩選器來篩選其他發行項,請執行 sp_addmergefilter (Transact-SQL) 來定義兩個發行項之間的聯結或邏輯記錄關聯性。必須針對每一個定義的關聯性執行一次此預存程序。如需詳細資訊,請參閱<如何:定義和修改合併發行項之間的聯結篩選 (複寫 Transact-SQL 程式設計)>。

  5. 在發行集資料庫的發行者上執行 sp_helpmergepublication (Transact-SQL),指定步驟 1 中 @publication 的值。請注意結果集中 snapshot_jobid 的值。

  6. 將步驟 5 取得之 snapshot_jobid 的值轉換成 uniqueidentifier

  7. msdb 資料庫的發行者上,執行 sp_start_job (Transact-SQL),針對 @job_id 指定步驟 6 中所取得的轉換值。

  8. 在發行集資料庫的發行者上,執行 sp_addmergepartition (Transact-SQL)。針對 @publication 指定步驟 1 中的發行集名稱,並針對 @suser_sname (如果 SUSER_SNAME (Transact-SQL) 用於篩選子句) 或是針對 @host_name (如果 HOST_NAME (Transact-SQL) 用於篩選子句) 指定用於定義資料分割的值。

  9. 在發行集資料庫的發行者上,執行 sp_adddynamicsnapshot_job (Transact-SQL)。針對 @publication 指定步驟 1 中的發行集名稱、步驟 8 中的 @suser_sname@host_name 的值,以及此作業的排程。這樣會建立針對指定的資料分割產生參數化快照集的作業。如需詳細資訊,請參閱<如何:指定同步排程 (複寫 Transact-SQL 程式設計)>。

    [!附註]

    這個作業會使用與步驟 2 中定義的初始快照集作業相同的 Windows 帳戶來執行。若要移除參數化快照集作業及其相關的資料分割,請執行 sp_dropdynamicsnapshot_job (Transact-SQL)

  10. 在發行集資料庫的發行者上,執行 sp_helpmergepartition (Transact-SQL),指定步驟 1 中 @publication 的值以及步驟 8 中 @suser_sname@host_name 的值。請注意結果集中 dynamic_snapshot_jobid 的值。

  11. msdb 資料庫的散發者上,執行 sp_start_job (Transact-SQL),針對 @job_id 指定步驟 9 中所取得的值。這樣會啟動資料分割的參數化快照集作業。

  12. 針對每一個訂閱重複步驟 8-11 來產生分割快照集。

針對每一個資料分割建立發行集及手動建立快照集

  1. 執行 sp_addmergepublication (Transact-SQL) 來建立發行集。如需詳細資訊,請參閱<如何:建立發行集 (複寫 Transact-SQL 程式設計)>。

  2. 在發行者上,執行 sp_addpublication_snapshot (Transact-SQL)。針對 @publication 指定步驟 1 中所使用的發行集名稱,以及針對 @job_login@password 指定快照集代理程式執行時所使用的 Windows 認證。如果代理程式會在與「發行者」時連接時使用「SQL Server 驗證」,則也必須指定 @publisher_security_mode 的值 0,以及 @publisher_login@publisher_password 的 SQL Server 登入資訊。這麼做會為發行集建立快照集代理程式作業。如需有關產生初始快照集以及為快照集代理程式定義自訂排程的詳細資訊,請參閱<如何:建立初始快照集 (複寫 Transact-SQL 程式設計)>。

    安全性注意事項安全性注意事項

     當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_login 和 job_password) 都會以純文字的方式傳給散發者。您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。如需詳細資訊,請參閱<加密 SQL Server 的連接>。

  3. 執行 sp_addmergearticle (Transact-SQL) 將發行項加入發行集中。必須針對發行集中的每一個發行項執行一次此預存程序。在使用參數化篩選時,您必須使用 @subset_filterclause 參數,至少針對一個發行項指定參數化資料列篩選器。如需詳細資訊,請參閱<如何:定義和修改合併發行項的參數化資料列篩選器 (複寫 Transact-SQL 程式設計)>。

  4. 如果將根據參數化資料列篩選器來篩選其他發行項,請執行 sp_addmergefilter (Transact-SQL) 來定義兩個發行項之間的聯結或邏輯記錄關聯性。必須針對每一個定義的關聯性執行一次此預存程序。如需詳細資訊,請參閱<如何:定義和修改合併發行項之間的聯結篩選 (複寫 Transact-SQL 程式設計)>。

  5. 從命令提示字元啟動快照集作業或是執行複寫快照集代理程式,以產生標準快照集結構描述和其他檔案。如需詳細資訊,請參閱<如何:建立初始快照集 (複寫 Transact-SQL 程式設計)>。

  6. 再次從命令提示字元執行複寫快照集代理程式,以產生大量複製 (.bcp) 檔案,為 -DynamicSnapshotLocation 指定分割快照集的位置,以及指定可定義此資料分割的下列其中一個或兩個屬性:

  7. 針對每一個訂閱重複步驟 6 來產生分割快照集。

  8. 針對每一個訂閱執行合併代理程式,以便在訂閱者上套用初始分割快照集,並指定下列屬性:

    • -Hostname - 如果正在覆寫 HOST_NAME 的實際值,則表示用於定義資料分割的值。

    • -DynamicSnapshotLocation - 此資料分割的動態快照集位置。

[!附註]

如需有關複寫代理程式之程式設計的詳細資訊,請參閱<複寫代理程式可執行檔概念>。

範例

這個範例會使用參數化篩選來建立合併式發行集,其中的訂閱者會起始快照集的產生程序。@job_login@job_password 的值會使用指令碼變數來傳遞。

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

這個範例會使用參數化篩選建立發行集,其中的每一個訂閱者會藉由執行 sp_addmergepartition 來定義其資料分割,並藉由執行 sp_adddynamicsnapshot_job 來建立篩選的快照集作業,傳遞資料分割資訊。@job_login@job_password 的值會使用指令碼變數來傳遞。

-- 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.  
EXEC sp_addmergepublication 
  @publication = @publication, 
  @description = N'Merge publication of AdventureWorks2008R2.', 
  @allow_subscriber_initiated_snapshot = N'false';

-- 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 customer 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 snapshot agent job.
DECLARE @publication AS sysname;
SET @publication = N'AdvWorksSalesPersonMerge';

EXEC sp_startpublication_snapshot 
  @publication = @publication;
GO

PRINT '*** Waiting for the initial snapshot.';
GO

-- Create a temporary table to store the filtered data snapshot 
-- job information.
CREATE TABLE #temp (id int,
    job_name sysname,
    job_id uniqueidentifier,
    dynamic_filter_login sysname NULL,
    dynamic_filter_hostname sysname NULL,
    dynamic_snapshot_location nvarchar(255),
    frequency_type int, 
    frequency_interval int, 
    frequency_subday_type int,
    frequency_subday_interval int, 
    frequency_relative_interval int, 
    frequency_recurrence_factor int, 
    active_start_date int, 
    active_end_date int, 
    active_start_time int, 
    active_end_time int
)

-- Create each snapshot for a partition 
-- The initial snapshot must already be generated.
DECLARE @publication AS sysname;
DECLARE @jobname AS sysname
DECLARE @hostname AS sysname
SET @publication = N'AdvWorksSalesPersonMerge';
SET @hostname = N'adventure-works\Fernando';

WHILE NOT EXISTS(SELECT * FROM sysmergepublications 
    WHERE [name] = @publication 
    AND snapshot_ready = 1)
BEGIN
    WAITFOR DELAY '00:00:05'
END

-- Create a data partition by overriding HOST_NAME().
EXEC sp_addmergepartition 
  @publication = @publication,
  @host_name = @hostname;

-- Create the filtered data snapshot job, and use the returned 
-- information to start the job.
EXEC sp_adddynamicsnapshot_job 
  @publication = @publication,
  @host_name = @hostname;

INSERT INTO #temp (id, job_name, job_id, dynamic_filter_login,
    dynamic_filter_hostname, dynamic_snapshot_location,
    frequency_type, frequency_interval, frequency_subday_type,
    frequency_subday_interval, frequency_relative_interval, 
    frequency_recurrence_factor, active_start_date, active_end_date, 
    active_start_time,active_end_time)
EXEC sp_helpdynamicsnapshot_job;

SELECT @jobname = (SELECT DISTINCT job_name FROM #temp WHERE dynamic_filter_hostname = @hostname);

EXEC msdb..sp_start_job @job_name = @jobname;
DROP TABLE #temp;
GO

這個範例會使用參數化篩選建立發行集,其中的每一個訂閱者必須藉由提供資料分割資訊,以建立其資料分割和篩選的快照集作業。訂閱者在手動執行複寫代理程式時,會使用命令列參數提供資料分割資訊。此範例假設也已經建立發行集的訂閱。

-- 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.  
EXEC sp_addmergepublication 
  @publication = @publication, 
  @description = N'Merge publication of AdventureWorks2008R2.', 
  @allow_subscriber_initiated_snapshot = N'false';

-- 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 customer 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
REM Line breaks are added to improve readability. 
REM In a batch file, commands must be made in a single line.
REM Run the Snapshot agent from the command line to generate the standard snapshot 
REM schema and other files. 
SET DistPub=%computername%
SET PubDB=AdventureWorks2008R2 
SET PubName=AdvWorksSalesPersonMerge

"C:\Program Files\Microsoft SQL Server\100\COM\SNAPSHOT.EXE" -Publication %PubName%  
-Publisher %DistPub% -Distributor  %DistPub%  -PublisherDB %PubDB%  -ReplicationType 2  
-OutputVerboseLevel 1  -DistributorSecurityMode 1

PAUSE
REM Run the Snapshot agent from the command line, this time to generate 
REM the bulk copy (.bcp) data for each Subscriber partition.  
SET DistPub=%computername%
SET PubDB=AdventureWorks2008R2 
SET PubName=AdvWorksSalesPersonMerge
SET SnapshotDir=\\%DistPub%\repldata\unc\fernando

MD %SnapshotDir%

"C:\Program Files\Microsoft SQL Server\100\COM\SNAPSHOT.EXE" -Publication %PubName%  
-Publisher %DistPub%  -Distributor  %DistPub%  -PublisherDB %PubDB%  -ReplicationType 2  
-OutputVerboseLevel 1  -DistributorSecurityMode 1  -DynamicFilterHostName "adventure-works\Fernando"  
-DynamicSnapshotLocation %SnapshotDir%

PAUSE
REM Run the Merge Agent for each subscription to apply the partitioned 
REM snapshot for each Subscriber.  
SET Publisher = %computername%
SET Subscriber = %computername%
SET PubDB = AdventureWorks2008R2 
SET SubDB = AdventureWorks2008R2Replica 
SET PubName = AdvWorksSalesPersonMerge 
SET SnapshotDir=\\%DistPub%\repldata\unc\fernando

"C:\Program Files\Microsoft SQL Server\100\COM\REPLMERG.EXE" -Publisher  %Publisher%  
-Subscriber  %Subscriber%  -Distributor %Publisher%  -PublisherDB %PubDB%  
-SubscriberDB %SubDB% -Publication %PubName%  -PublisherSecurityMode 1  -OutputVerboseLevel 3  
-Output -SubscriberSecurityMode 1  -SubscriptionType 3 -DistributorSecurityMode 1  
-Hostname "adventure-works\Fernando"  -DynamicSnapshotLocation %SnapshotDir%

PAUSE