共用方式為


如何:建立交易式發行集的可更新訂閱 (複寫 Transact-SQL 程式設計)

[!附註]

未來的 Microsoft SQL Server 版本將移除這項功能。請避免在新的開發工作中使用這項功能,並規劃修改目前使用這項功能的應用程式。

交易式複寫可使用立即或佇列更新訂閱,讓訂閱者上所做的變更傳播回到發行者。您可以使用複寫預存程序以程式設計的方式建立更新訂閱。

建立立即更新提取訂閱

  1. 在發行者上,執行 sp_helppublication 來確認發行集可支援立即更新訂閱。

  2. 在發行者上,執行 sp_helppublication 來確認發行集可支援提取訂閱。

    • 如果結果集中 allow_pull 的值為 1,則表示發行集支援提取訂閱。

    • 如果 allow_pull 的值為 0,請執行 sp_changepublication,將 @property 指定為 allow_pull,並將 @value 指定為 true。

  3. 在訂閱者上,執行 sp_addpullsubscription。指定 @publisher@publication,並針對 @update_mode 指定下列其中一個值:

    • synctran - 啟用立即更新的訂閱。

    • failover - 啟用立即更新的訂閱,並將佇列更新當做容錯移轉選項。

      [!附註]

      failover - 要求也要針對佇列更新訂閱啟用發行集。

  4. 在訂閱者上,執行 sp_addpullsubscription_agent。指定下列項目:

    • @publisher@publisher_db@publication 參數。

    • Microsoft Windows 認證,「訂閱者」上的「散發代理程式」執行時會針對 @job_login@job_password 使用該認證。

      [!附註]

      使用「Windows 整合式驗證」建立的連接一律使用由 @job_login@job_password 指定的 Windows 認證。散發代理程式一律使用「Windows 整合式驗證」建立與訂閱者的本機連接。依預設,代理程式會使用「Windows 整合式驗證」連接到散發者。

    • (選擇性) @distributor_security_mode 的值 0,以及 @distributor_login@distributor_password 的 Microsoft SQL Server 登入資訊 (如果您在連接到散發者時需要使用「SQL Server 驗證」)。

    • 此訂閱之散發代理程式作業的排程。如需詳細資訊,請參閱<如何:指定同步排程 (複寫 Transact-SQL 程式設計)>。

  5. 在訂閱資料庫的訂閱者上,執行 sp_link_publication。指定 @publisher@publication,以及針對 @publisher_db 指定發行集資料庫名稱並針對 @security_mode 指定下列其中一個值:

    • 0 - 在發行者上進行更新時使用「SQL Server 驗證」。此選項要求您在發行者上指定對 @login@password 有效的登入。

    • 1 - 在連接到發行者時,使用在訂閱者上進行變更之使用者的安全性內容。如需與此安全性模式有關的限制,請參閱<sp_link_publication>。

    • 2 - 使用透過 sp_addlinkedserver 所建立之現有使用者定義的連結伺服器登入。

  6. 在發行者上,執行 sp_addsubscription,指定 @publication@subscriber@destination_db,並針對 @subscription_type 指定 pull 的值及針對 @update_mode 指定步驟 3 中指定的相同值。

    這樣會在發行者上註冊提取訂閱。

建立立即更新發送訂閱

  1. 在發行者上,執行 sp_helppublication 來確認發行集可支援立即更新訂閱。

  2. 在發行者上,執行 sp_helppublication 來確認發行集可支援發送訂閱。

    • 如果結果集中 allow_push 的值為 1,則表示發行集可支援發送訂閱。

    • 如果 allow_push 的值為 0,請執行 sp_changepublication,將 @property 指定為 allow_push,並將 @value 指定為 true。

  3. 在發行者上,執行 sp_addsubscription。指定 @publication@subscriber@destination_db,並針對 @update_mode 指定下列其中一個值:

    • synctran - 啟用立即更新的支援。

    • failover - 啟用立即更新的支援,並將佇列更新當做容錯移轉選項。

      [!附註]

      failover - 要求也要針對佇列更新訂閱啟用發行集。

  4. 在發行者上,執行 sp_addpushsubscription_agent。指定下列參數:

    • @subscriber@subscriber_db@publication

    • 「散發者」上的「散發代理程式」針對 @job_login@job_password 而執行的 Windows 認證。

      [!附註]

      使用「Windows 整合式驗證」建立的連接一律使用由 @job_login@job_password 指定的 Windows 認證。散發代理程式一律使用「Windows 整合式驗證」建立與散發者的本機連接。依預設,代理程式會使用「Windows 整合式驗證」連接到訂閱者。

    • (選擇性) 針對 @subscriber_security_mode 的值 0,以及針對 @subscriber_login@subscriber_password 的 SQL Server 登入資訊 (如果您在連接到訂閱者時需要使用「SQL Server 驗證」)。

    • 此訂閱之散發代理程式作業的排程。如需詳細資訊,請參閱<如何:指定同步排程 (複寫 Transact-SQL 程式設計)>。

  5. 在訂閱資料庫的訂閱者上,執行 sp_link_publication。指定 @publisher@publication,以及針對 @publisher_db 指定發行集資料庫名稱並針對 @security_mode 指定下列其中一個值:

    • 0 - 在發行者上進行更新時使用「SQL Server 驗證」。此選項要求您在發行者上指定對 @login@password 有效的登入。

    • 1 - 在連接到發行者時,使用在訂閱者上進行變更之使用者的安全性內容。如需與此安全性模式有關的限制,請參閱<sp_link_publication>。

    • 2 - 使用透過 sp_addlinkedserver 所建立之現有使用者定義的連結伺服器登入。

建立佇列更新提取訂閱

  1. 在發行者上,執行 sp_helppublication 來確認發行集可支援佇列更新訂閱。

  2. 在發行者上,執行 sp_helppublication 來確認發行集可支援提取訂閱。

    • 如果結果集中 allow_pull 的值為 1,則表示發行集支援提取訂閱。

    • 如果 allow_pull 的值為 0,請執行 sp_changepublication,將 @property 指定為 allow_pull,並將 @value 指定為 true。

  3. 在訂閱者上,執行 sp_addpullsubscription。指定 @publisher@publication,並針對 @update_mode 指定下列其中一個值:

    • queued tran - 啟用佇列更新的訂閱。

    • queued failover - 啟用佇列更新的支援,並將立即更新當做容錯移轉選項。

      [!附註]

      queued failover - 要求也要針對立即更新訂閱啟用發行集。若要容錯移轉到立即更新,您必須使用 sp_link_publication,以定義訂閱者上的變更複寫到發行者時所用的認證。

  4. 在訂閱者上,執行 sp_addpullsubscription_agent。指定下列參數:

    • @publisher@publisher_db@publication

    • 「訂閱者」上的「散發代理程式」針對 @job_login@job_password 而執行的 Windows 認證。

      [!附註]

      使用「Windows 整合式驗證」建立的連接一律使用由 @job_login@job_password 指定的 Windows 認證。散發代理程式一律使用「Windows 整合式驗證」建立與訂閱者的本機連接。依預設,代理程式會使用「Windows 整合式驗證」連接到散發者。

    • (選擇性) @distributor_security_mode 的值 0,以及 @distributor_login@distributor_password 的 SQL Server 登入資訊 (如果您在連接到散發者時需要使用「SQL Server 驗證」)。

    • 此訂閱之散發代理程式作業的排程。如需詳細資訊,請參閱<如何:指定同步排程 (複寫 Transact-SQL 程式設計)>。

  5. 在發行者上,執行 sp_addsubscriber (Transact-SQL),以便在發行者上註冊訂閱者。

    在發行者上,執行 sp_addsubscription,指定 @publication@subscriber@destination_db,並針對 @subscription_type 指定 pull 的值及針對 @update_mode 指定步驟 3 中指定的相同值。

    這樣會在發行者上註冊提取訂閱。

建立佇列更新發送訂閱

  1. 在發行者上,執行 sp_helppublication 來確認發行集可支援佇列更新訂閱。

  2. 在發行者上,執行 sp_helppublication 來確認發行集可支援發送訂閱。

    • 如果結果集中 allow_push 的值為 1,則表示發行集可支援發送訂閱。

    • 如果 allow_push 的值為 0,請執行 sp_changepublication,將 @property 指定為 allow_push,並將 @value 指定為 true。

  3. 在發行者上,執行 sp_addsubscription。指定 @publication@subscriber@destination_db,並針對 @update_mode 指定下列其中一個值:

    • queued tran - 啟用佇列更新的訂閱。

    • queued failover - 啟用佇列更新的支援,並將立即更新當做容錯移轉選項。

      [!附註]

      queued failover 選項要求也要針對立即更新訂閱啟用發行集。若要容錯移轉到立即更新,您必須使用 sp_link_publication,以定義訂閱者上的變更複寫到發行者時所用的認證。

  4. 在發行者上,執行 sp_addpushsubscription_agent。指定下列參數:

    • @subscriber@subscriber_db@publication

    • 「散發者」上的「散發代理程式」針對 @job_login@job_password 而執行的 Windows 認證。

      [!附註]

      使用「Windows 整合式驗證」建立的連接一律使用由 @job_login@job_password 指定的 Windows 認證。散發代理程式一律使用「Windows 整合式驗證」建立與散發者的本機連接。依預設,代理程式會使用「Windows 整合式驗證」連接到訂閱者。

    • (選擇性) 針對 @subscriber_security_mode 的值 0,以及針對 @subscriber_login@subscriber_password 的 SQL Server 登入資訊 (如果您在連接到訂閱者時需要使用「SQL Server 驗證」)。

    • 此訂閱之散發代理程式作業的排程。如需詳細資訊,請參閱<如何:指定同步排程 (複寫 Transact-SQL 程式設計)>。

範例

此範例會建立發行集的立即更新提取訂閱,此發行集可支援立即更新訂閱。登入和密碼值是在執行階段使用 sqlcmd 指令碼變數所提供。

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

-- Execute this batch at the Subscriber.
DECLARE @publication AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @publisher AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS nvarchar(512);
SET @publication = N'AdvWorksProductTran';
SET @publicationDB = N'AdventureWorks2008R2';
SET @publisher = $(PubServer);
SET @login = $(Login);
SET @password = $(Password);

-- At the subscription database, create a pull subscription to a transactional 
-- publication using immediate updating with queued updating as a failover.
EXEC sp_addpullsubscription 
    @publisher = @publisher, 
    @publication = @publication, 
    @publisher_db = @publicationDB, 
    @update_mode = N'failover', 
    @subscription_type = N'pull';

-- Add an agent job to synchronize the pull subscription, 
-- which uses Windows Authentication when connecting to the Distributor.
EXEC sp_addpullsubscription_agent 
    @publisher = @publisher, 
    @publisher_db = @publicationDB, 
    @publication = @publication,
    @job_login = @login,
    @job_password = @password; 

-- Add a Windows Authentication-based linked server that enables the 
-- Subscriber-side triggers to make updates at the Publisher. 
EXEC sp_link_publication 
    @publisher = @publisher, 
    @publication = @publication,
    @publisher_db = @publicationDB, 
    @security_mode = 0,
    @login = @login,
    @password = @password;
GO

USE AdventureWorks2008R2;
GO

-- Execute this batch at the Publisher.
DECLARE @publication AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @subscriber AS sysname;
SET @publication = N'AdvWorksProductTran'; 
SET @subscriptionDB = N'AdventureWorks2008R2Replica'; 
SET @subscriber = $(SubServer);

-- At the Publisher, register the subscription, using the defaults.
USE [AdventureWorks2008R2]
EXEC sp_addsubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @destination_db = @subscriptionDB, 
    @subscription_type = N'pull', 
    @update_mode = N'failover';
GO