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

您可以使用複寫預存程序來以程式設計的方式設定複寫發行和散發。

使用本機散發者設定發行

  1. 請執行sp_get_distributor (Transact-SQL)來判斷伺服器是否已經設定為散發者。

    • 如果結果集中 installed 的值是 0,請在 master 資料庫的散發者上執行 sp_adddistributor (Transact-SQL)

    • 如果結果集中 distribution db installed 的值是 0,請在 master 資料庫的散發者上執行 sp_adddistributiondb (Transact-SQL)。針對 @database 指定散發資料庫的名稱。您可以選擇針對 @max_distretention 指定最大交易保留期限,並針對 @history_retention 指定記錄保留期限。如果正在建立新的資料庫,請指定所要的資料庫屬性參數。

  2. 在散發者 (也是發行者) 上,執行 sp_adddistpublisher (Transact-SQL),針對 @working_directory 指定將當做預設快照集資料夾使用的 UNC 共用。

  3. 在發行者上,執行 sp_replicationdboption (Transact-SQL)。針對 @dbname 指定發行的資料庫、針對 @optname 指定複寫的類型,並針對 @value 指定 true 的值。

使用遠端散發者設定發行

  1. 請執行 sp_get_distributor (Transact-SQL) 來判斷伺服器是否已經設定為散發者。

    • 如果結果集中 installed 的值是 0,請在 master 資料庫的散發者上執行 sp_adddistributor (Transact-SQL)。針對 @password 指定增強式密碼。distributor_admin 帳戶的這個密碼將會由發行者連接到散發者時使用。

    • 如果結果集中 distribution db installed 的值是 0,請在 master 資料庫的散發者上執行 sp_adddistributiondb (Transact-SQL)。針對 @database 指定散發資料庫的名稱。您可以選擇針對 @max_distretention 指定最大交易保留期限,並針對 @history_retention 指定記錄保留期限。如果正在建立新的資料庫,請指定所要的資料庫屬性參數。

  2. 在散發者上,執行 sp_adddistpublisher (Transact-SQL),針對 @working_directory 指定將當做預設快照集資料夾使用的 UNC 共用。如果散發者會在與發行者連接時使用「SQL Server 驗證」,則也必須針對 @security_mode 指定 0 的值,並針對 @login@password 指定 Microsoft SQL Server 登入資訊。

  3. 在 master 資料庫的發行者上,執行 sp_adddistributor (Transact-SQL)。針對 @password 指定步驟 1 中所使用的增強式密碼。這個密碼將會由發行者連接到散發者時使用。

  4. 在發行者上,執行 sp_replicationdboption (Transact-SQL)。針對 @dbname 指定發行的資料庫、針對 @optname 指定複寫的類型,並針對 @value 指定 true 的值。

範例

下列範例會示範如何以程式設計方式設定發行和散發。在此範例中,會使用指令碼變數來提供設定為發行者和本機散發者的伺服器名稱。

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). 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".

-- Install the Distributor and the distribution database.
DECLARE @distributor AS sysname;
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
DECLARE @directory AS nvarchar(500);
DECLARE @publicationDB AS sysname;
-- Specify the Distributor name.
SET @distributor = $(DistPubServer);
-- Specify the distribution database.
SET @distributionDB = N'distribution';
-- Specify the Publisher name.
SET @publisher = $(DistPubServer);
-- Specify the replication working directory.
SET @directory = N'\\' + $(DistPubServer) + '\repldata';
-- Specify the publication database.
SET @publicationDB = N'AdventureWorks2008R2'; 

-- Install the server MYDISTPUB as a Distributor using the defaults,
-- including autogenerating the distributor password.
USE master
EXEC sp_adddistributor @distributor = @distributor;

-- Create a new distribution database using the defaults, including
-- using Windows Authentication.
USE master
EXEC sp_adddistributiondb @database = @distributionDB, 
    @security_mode = 1;
GO

-- Create a Publisher and enable AdventureWorks2008R2 for replication.
-- Add MYDISTPUB as a publisher with MYDISTPUB as a local distributor
-- and use Windows Authentication.
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
-- Specify the distribution database.
SET @distributionDB = N'distribution';
-- Specify the Publisher name.
SET @publisher = $(DistPubServer);

USE [distribution]
EXEC sp_adddistpublisher @publisher=@publisher, 
    @distribution_db=@distributionDB, 
    @security_mode = 1;
GO