如何:建立初始快照集 (複寫 Transact-SQL 程式設計)

可以透過程式設計方式建立初始快照集,其方式是建立及執行快照集代理程式作業,或是從批次檔執行快照集代理程式的可執行檔。在產生初始快照集之後,此快照集會在第一次同步處理訂閱時,傳送及套用到訂閱者。如果您從命令提示字元或批次檔執行快照集代理程式,每當現有的快照集無效時,您將需要重新執行此代理程式。

使用參數化篩選的合併式發行集需要一個兩段式快照集。如需詳細資訊,請參閱<如何:使用參數化篩選建立合併式發行集的快照集 (複寫 Transact-SQL 程式設計)>。

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

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

建立及執行快照集代理程式作業以產生初始快照集

  1. 建立快照式、交易式或合併式發行集。如需詳細資訊,請參閱<如何:建立發行集 (複寫 Transact-SQL 程式設計)>。

  2. 執行 sp_addpublication_snapshot (Transact-SQL)。指定 @publication 及下列參數:

    • @job_login,它會指定散發者上的快照集代理程式執行時所用的 Windows 驗證認證。

    • @job_password,它是提供之 Windows 認證的密碼。

    • (選擇性) 如果代理程式在連接到發行者時將使用「SQL Server 驗證」,會將 @publisher_security_mode 設定為 0 的值。在此情況下,您也必須針對 @publisher_login@publisher_password 指定「SQL Server 驗證」登入資訊。

    • (選擇性) 快照集代理程式作業的同步排程。如需詳細資訊,請參閱<如何:指定同步排程 (複寫 Transact-SQL 程式設計)>。

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

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

  3. 將發行項加入至發行集。如需詳細資訊,請參閱<如何:定義發行項 (複寫 Transact-SQL 程式設計)>。

  4. 在發行集資料庫的發行者上執行 sp_startpublication_snapshot (Transact-SQL),指定步驟 1 中 @publication 的值。

執行快照集代理程式來產生初始快照集

  1. 建立快照式、交易式或合併式發行集。如需詳細資訊,請參閱<如何:建立發行集 (複寫 Transact-SQL 程式設計)>。

  2. 將發行項加入至發行集。如需詳細資訊,請參閱<如何:定義發行項 (複寫 Transact-SQL 程式設計)>。

  3. 從命令提示字元或批次檔中,執行 snapshot.exe 來啟動複寫快照集代理程式,並指定下列命令列引數:

    • -Publication

    • -Publisher

    • -Distributor

    • -PublisherDB

    • -ReplicationType

    無果您正在使用「SQL Server 驗證」,您也必須指定下列引數:

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

範例

此範例會示範如何建立交易式發行集,並針對新的發行集加入快照集代理程式作業 (使用 sqlcmd 指令碼變數)。此範例也會啟動此作業。

-- To avoid storing the login and password in the script file, the values 
-- are passed into SQLCMD as scripting variables. 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".

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks2008R2'; --publication database
SET @publication = N'AdvWorksCustomerTran'; -- transactional publication name
SET @login = $(Login);
SET @password = $(Password);

USE [AdventureWorks2008R2]

-- Enable transactional and snapshot replication on the publication database.
EXEC sp_replicationdboption 
  @dbname = @publicationDB, 
  @optname = N'publish',
  @value = N'true';

-- Execute sp_addlogreader_agent to create the agent job. 
EXEC sp_addlogreader_agent 
  @job_login = @login, 
  @job_password = @password,
  -- Explicitly specify the security mode used when connecting to the Publisher.
  @publisher_security_mode = 1;

-- Create new transactional publication, using the defaults. 
USE [AdventureWorks2008R2]
EXEC sp_addpublication 
  @publication = @publication, 
  @description = N'transactional publication';

-- Create a new snapshot job for the publication, using the defaults.
EXEC sp_addpublication_snapshot 
  @publication = @publication,
  @job_login = @login,
  @job_password = @password;

-- Start the Snapshot Agent job.
EXEC sp_startpublication_snapshot @publication = @publication;
GO

此範例會建立合併式發行集,並針對此發行集加入快照集代理程式作業 (使用 sqlcmd 變數)。此範例也會啟動此作業。

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

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks2008R2'; 
SET @publication = N'AdvWorksSalesOrdersMerge'; 
SET @login = $(Login);
SET @password = $(Password);

-- Enable merge replication on the publication database.
USE master
EXEC sp_replicationdboption 
  @dbname = @publicationDB, 
  @optname=N'merge publish',
  @value = N'true';

-- Create new merge publication, using the defaults. 
USE [AdventureWorks2008R2]
EXEC sp_addmergepublication 
  @publication = @publication, 
  @description = N'Merge publication.';

-- Create a new snapshot job for the publication, using the defaults.
EXEC sp_addpublication_snapshot 
  @publication = @publication,
  @job_login = @login,
  @job_password = @password;

-- Start the Snapshot Agent job.
EXEC sp_startpublication_snapshot @publication = @publication;
GO

下列命令列引數會啟動快照集代理程式,以針對合併式發行集產生快照集。

[!附註]

加入了分行符號,以提升可讀性。在批次檔中,必須在單一行中撰寫命令。

REM -- Declare variables
SET Publisher=%InstanceName%
SET PublicationDB=AdventureWorks2008R2 
SET Publication=AdvWorksSalesOrdersMerge 

REM --Start the Snapshot Agent to generate the snapshot for AdvWorksSalesOrdersMerge.
"C:\Program Files\Microsoft SQL Server\100\COM\SNAPSHOT.EXE" -Publication %Publication% 
-Publisher %Publisher% -Distributor %Publisher% -PublisherDB %PublicationDB% 
-ReplicationType 2 -OutputVerboseLevel 1 -DistributorSecurityMode 1