Replication System Stored Procedures Concepts
在SQL Server中,系統預存程式會提供對複寫拓撲中所有使用者可設定功能的程式設計存取。 雖然預存程式可以使用 SQL Server Management Studio 或 sqlcmd 命令列公用程式個別執行,但撰寫可以執行以執行邏輯複寫工作順序的 Transact-SQL 腳本檔案可能會很有説明。
指令碼複寫工作提供下列好處:
永久保留用以部署複寫拓撲的步驟副本。
使用單一指令碼來設定多個訂閱者。
讓新資料庫管理員快速上手,讓他們能夠評估、了解、變更或是疑難排解程式碼。
重要
指令碼有可能成為安全性弱點的根源;因為它們可以在使用者未查覺或介入的情況下叫用系統函數,且可能以純文字的方式包含安全性認證。 使用指令碼之前,請先檢閱它們是否有安全性問題。
建立複寫指令碼
從複寫的觀點來看,腳本是一系列一或多個 Transact-SQL 語句,其中每個語句都會執行複寫預存程式。 指令碼是文字檔案,通常具有 .sql 副檔名,可以使用 sqlcmd 公用程式來執行。 當執行指令碼檔案時,公用程式會執行儲存在檔案中的 SQL 陳述式。 同樣地,腳本可以儲存為SQL Server Management Studio專案中的查詢物件。
複寫指令碼可以用下列方式建立:
手動建立指令碼。
使用在複寫精靈中提供的指令碼產生功能或是
SQL Server Management Studio。 如需詳細資訊,請參閱 Scripting Replication。
使用 Replication Management Objects (RMO) 以程式設計的方式產生指令碼來建立 RMO 物件。
當您手動建立複寫指令碼時,請記住下列考量:
Transact-SQL 腳本有一或多個批次。 GO 命令代表批次的結尾。 如果 Transact-SQL 腳本沒有任何 GO 命令,則會以單一批次的形式執行。
當在單一批次中執行多個複寫預存程序時,在第一個程序之後,在批次中所有後續的程序,前面都必須加上 EXECUTE 關鍵字。
在批次中所有預存程序都必須在批次將執行之前編譯。 不過,只要批次已編譯且執行計劃已建立,就可能會發生執行階段錯誤。
當建立指令碼以設定複寫時,應該使用 Windows 驗證來避免在指令碼檔案中儲存安全性認證。 如果您必須將認證儲存在指令碼檔案中,則必須維護這個檔案的安全性,使他人無法在未獲授權的情況下擅自存取。
範例複寫指令碼
您可以執行下列指令碼,在伺服器上安裝發行和散發。
-- 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'AdventureWorks2012';
-- 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 AdventureWorks2012 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
在本機上可以將這個指令碼儲存為 instdistpub.sql
,以便在需要時執行或重新執行它。
先前的腳本包含sqlcmd腳本變數,這些變數用於 SQL Server《線上叢書》中的許多複寫程式碼範例。 指令碼變數是藉由使用 $(MyVariable)
語法所定義。 變數的值可以傳遞至命令列或SQL Server Management Studio中的腳本。 如需詳細資訊,請參閱本主題中的下一節「執行複寫指令碼」。
執行複寫指令碼
建立之後,複寫指令碼可以用下列其中一種方式來執行:
在 SQL Server Management Studio 中建立 SQL 查詢檔案
複寫 Transact-SQL 腳本檔案可以建立為SQL Server Management Studio專案中的 SQL 查詢檔案。 在寫入指令碼之後,可以為此查詢檔案建立資料庫的連接,而且可以執行指令碼。 如需如何使用 SQL Server Management Studio 建立 Transact-SQL 腳本的詳細資訊,請參閱查詢和文字編輯器 (SQL Server Management Studio) ) 。
若要使用包含腳本變數的腳本,SQL Server Management Studio必須在sqlcmd模式中執行。 在 sqlcmd 模式中,查詢編輯器會接受 sqlcmd 特有的其他語法,例如 :setvar
,它是用於變數值。 如需 sqlcmd 模式的詳細資訊,請參閱使用查詢編輯器編輯 SQLCMD 指令碼。 在下列指令碼中,:setvar
是用以提供 $(DistPubServer)
變數的值。
:setvar DistPubServer N'MyPublisherAndDistributor';
-- 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);
--
-- Additional code goes here
--
從命令列使用 sqlcmd 公用程式
下列範例示範如何在命令列使用 sqlcmd 公用程式來執行 instdistpub.sql
指令碼檔案:
sqlcmd.exe -E -S sqlserverinstance -i C:\instdistpub.sql -o C:\output.log -v DistPubServer="N'MyDistributorAndPublisher'"
在此範例中 -E
,參數表示連線到SQL Server時會使用 Windows 驗證。 在使用 Windows 驗證時,在指令碼檔案中不需要儲存使用者名稱與密碼。 腳本檔案的名稱和路徑是由 -i
參數所指定,而輸出檔的名稱是由 -o
參數所指定,SQL Server的輸出 (輸出會寫入此檔案,而不是使用此參數時) 主控台。 公用 sqlcmd
程式可讓您使用 -v
參數,在執行時間將腳本變數傳遞至 Transact-SQL 腳本。 在此範例中,sqlcmd
會在執行之前,以值 N'MyDistributorAndPublisher'
取代指令碼中 $(DistPubServer)
的每個執行個體。
注意
-X
參數會停用指令碼變數。
以批次檔自動化工作
透過使用批次檔,可以在相同的批次檔中,自動化複寫管理工作、複寫同步處理工作以及其他工作。 下列批次檔使用 sqlcmd 公用程式,卸除和重新建立訂閱資料庫並加入合併提取訂閱。 接著檔案會叫用合併代理程式以同步處理新訂閱:
REM ----------------------Script to synchronize merge subscription ----------------------
REM -- Creates subscription database and
REM -- synchronizes the subscription to MergeSalesPerson.
REM -- Current computer acts as both Publisher and Subscriber.
REM -------------------------------------------------------------------------------------
SET Publisher=%computername%
SET Subscriber=%computername%
SET PubDb=AdventureWorks
SET SubDb=AdventureWorksReplica
SET PubName=AdvWorksSalesOrdersMerge
REM -- Drop and recreate the subscription database at the Subscriber
sqlcmd /S%Subscriber% /E /Q"USE master IF EXISTS (SELECT * FROM sysdatabases WHERE name='%SubDb%' ) DROP DATABASE %SubDb%"
sqlcmd /S%Subscriber% /E /Q"USE master CREATE DATABASE %SubDb%"
REM -- Add a pull subscription at the Subscriber
sqlcmd /S%Subscriber% /E /Q"USE %SubDb% EXEC sp_addmergepullsubscription @publisher = %Publisher%, @publication = %PubName%, @publisher_db = %PubDb%"
sqlcmd /S%Subscriber% /E /Q"USE %SubDb% EXEC sp_addmergepullsubscription_agent @publisher = %Publisher%, @publisher_db = %PubDb%, @publication = %PubName%, @subscriber = %Subscriber%, @subscriber_db = %SubDb%, @distributor = %Publisher%"
REM -- This batch file starts the merge agent at the Subscriber to
REM -- synchronize a pull subscription to a merge publication.
REM -- The following must be supplied on one line.
"\Program Files\Microsoft SQL Server\120\COM\REPLMERG.EXE" -Publisher %Publisher% -Subscriber %Subscriber% -Distributor %Publisher% -PublisherDB %PubDb% -SubscriberDB %SubDb% -Publication %PubName% -PublisherSecurityMode 1 -OutputVerboseLevel 1 -Output -SubscriberSecurityMode 1 -SubscriptionType 1 -DistributorSecurityMode 1 -Validate 3
撰寫一般複寫工作的指令碼
下列是一些最常使用系統預存程序來編寫指令碼的複寫工作:
設定發行和散發
修改發行者和散發者屬性
停用發行與散發
建立發行集及定義發行項
刪除發行集及發行項
建立提取訂閱
修改提取訂閱
刪除提取訂閱
建立發送訂閱
修改提取訂閱
刪除發送訂閱
同步處理提取訂閱