如何:建立發行集 (複寫 Transact-SQL 程式設計)

您可以使用複寫預存程序以程式設計的方式建立發行集。使用哪些預存程序取決於所要建立的發行集類型而定。

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

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

建立快照式或交易式發行集

  1. 在發行集資料庫的發行者上執行 sp_replicationdboption (Transact-SQL),以便使用快照式或交易式複寫來啟用目前資料庫的發行集。

  2. 如果是交易式發行集,請判斷發行集資料庫是否有記錄讀取器代理程式作業存在(快照式發行集不需要這個步驟)。

    • 如果記錄讀取器代理程式作業已存在發行集資料庫中,請繼續進行步驟 3。

    • 如果您不確定發行的資料庫是否有記錄讀取器代理程式作業存在,請在發行集資料庫的發行者上執行 sp_helplogreader_agent (Transact-SQL)

    • 如果結果集是空的,請建立記錄讀取器代理程式作業。在發行者上,執行 sp_addlogreader_agent (Transact-SQL)。針對 @job_name@password 指定執行此代理程式所使用的 Microsoft Windows 認證。如果代理程式將會在連接到發行者時使用 SQL Server 驗證,也必須針對 @publisher_security_mode 指定 0 的值,以及針對 @publisher_login@publisher_password 指定 Microsoft SQL Server 登入資訊。請繼續進行步驟 3。

  3. 在發行者上,執行 sp_addpublication (Transact-SQL)。針對 @publication 指定發行集名稱,並針對 @repl_freq 參數,指定 snapshot (適用於快照式發行集) 或 continuous (適用於交易式發行集) 的值。指定任何其他發行集選項。這樣會定義此發行集。

    [!附註]

    發行集名稱不能包含下列字元:

    % * [ ] | : " ? \ / < >

  4. 在發行者上,執行 sp_addpublication_snapshot (Transact-SQL)。針對 @publication 指定步驟 3 中所使用的發行集名稱,以及針對 @snapshot_job_name@password 指定快照集代理程式執行時所使用的 Windows 認證。如果代理程式會在連接到發行者時使用 SQL Server 驗證,則也必須針對 @publisher_security_mode 指定 0 的值,以及針對 @publisher_login@publisher_password 指定 SQL Server 登入資訊。這麼做會為發行集建立快照集代理程式作業。

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

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

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

  6. 啟動快照集代理程式作業來產生此發行集的初始快照集。如需詳細資訊,請參閱<如何:建立初始快照集 (複寫 Transact-SQL 程式設計)>。

建立合併式發行集

  1. 在發行者上執行 sp_replicationdboption (Transact-SQL),以便使用合併式複寫來啟用目前資料庫的發行集。

  2. 在發行集資料庫的發行者上,執行 sp_addmergepublication (Transact-SQL)。針對 @publication 指定發行集的名稱,並指定其他任何發行集選項。這樣會定義此發行集。

    [!附註]

    發行集名稱不能包含下列字元:

    % * [ ] | : " ? \ / < >

  3. 在發行者上,執行 sp_addpublication_snapshot (Transact-SQL)。針對 @publication 指定步驟 2 中所使用的發行集名稱,以及針對 @snapshot_job_name@password 指定快照集代理程式執行時所使用的 Windows 認證。如果代理程式會在連接到發行者時使用 SQL Server 驗證,則也必須針對 @publisher_security_mode 指定 0 的值,以及針對 @publisher_login@publisher_password 指定 SQL Server 登入資訊。這麼做會為發行集建立快照集代理程式作業。

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

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

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

  5. 啟動快照集代理程式作業來產生此發行集的初始快照集。如需詳細資訊,請參閱<如何:建立初始快照集 (複寫 Transact-SQL 程式設計)>。

範例

此範例會建立交易式發行集。指令碼變數是用來傳遞建立快照集代理程式作業和記錄讀取器代理程式作業所需的 Windows 認證。

-- 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'; 
SET @publication = N'AdvWorksProductTran'; 
-- Windows account used to run the Log Reader and Snapshot Agents.
SET @login = $(Login); 
-- This should be passed at runtime.
SET @password = $(Password); 

-- Enable transactional or 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 use of Windows Integrated Authentication (default) 
    -- when connecting to the Publisher.
    @publisher_security_mode = 1;

-- Create a new transactional publication with the required properties. 
EXEC sp_addpublication 
    @publication = @publication, 
    @status = N'active',
    @allow_push = N'true',
    @allow_pull = N'true',
    @independent_agent = N'true';

-- Create a new snapshot job for the publication, using a default schedule.
EXEC sp_addpublication_snapshot 
    @publication = @publication, 
    @job_login = @login, 
    @job_password = @password,
    -- Explicitly specify the use of Windows Integrated Authentication (default) 
    -- when connecting to the Publisher.
    @publisher_security_mode = 1;
GO

此範例會建立合併式發行集。指令碼變數是用來傳遞建立快照集代理程式作業所需的 Windows 認證。

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

--Declarations for adding a merge publication
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, using defaults.
USE master
EXEC sp_replicationdboption 
  @dbname=@publicationDB, 
  @optname=N'merge publish',
  @value = N'true' 

-- Create a new merge publication, explicitly setting the defaults. 
USE [AdventureWorks2008R2]
EXEC sp_addmergepublication 
-- These parameters are optional.
  @publication = @publication,
  -- optional parameters 
  @description = N'Merge publication of AdventureWorks2008R2.',
  @publication_compatibility_level  = N'90RTM';

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