Replication System Stored Procedures Concepts

適用於:SQL ServerAzure SQL 受控執行個體

在 SQL Server 中,系統預存程式會提供對複寫拓撲中所有使用者可設定功能的程式設計存取。 雖然預存程式可以使用 SQL Server Management Studio 或 sqlcmd 命令列公用程式個別執行,但撰寫可執行以執行邏輯複寫工作的 Transact-SQL 腳本檔案可能很有説明。

腳本複寫工作提供下列優點:

  • 保留用來部署複寫拓撲之步驟的永久複本。

  • 使用單一腳本來設定多個訂閱者。

  • 讓新資料庫管理員能夠評估、瞭解、變更或疑難排解程式碼,以快速教育新的資料庫管理員。

    重要

    腳本可以是安全性弱點的來源;他們可以在沒有使用者知識或介入的情況下叫用系統函式,而且可能包含純文字的安全性認證。 使用腳本之前,請先檢閱安全性問題的腳本。

建立複寫腳本

從複寫的觀點來看,腳本是一系列一或多個 Transact-SQL 語句,其中每個語句都會執行複寫預存程式。 腳本是文字檔,通常是副檔名為 .sql 的文字檔,可以使用 sqlcmd 公用程式來執行。 執行腳本檔案時,公用程式會執行儲存在檔案中的 SQL 語句。 同樣地,腳本可以儲存為 SQL Server Management Studio 專案中的查詢物件。

您可以透過下列方式建立複寫腳本:

  • 手動建立腳本。

  • 使用複寫精靈中提供的腳本產生功能或

  • SQL Server Management Studio。 如需詳細資訊,請參閱 Scripting Replication

  • 使用複寫管理物件 (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'AdventureWorks2022';   
  
-- 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 AdventureWorks2022 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 腳本中的每個實例 $(DistPubServer) 取代為執行前的值 N'MyDistributorAndPublisher'

注意

參數 -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\130\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  
  

編寫一般複寫工作的腳本

以下是一些最常見的複寫工作可以使用系統預存程式編寫腳本:

  • 設定發佈和散發

  • 修改發行者和散發者屬性

  • 停用發佈和散發

  • 建立發行集和定義發行項

  • 刪除發行集和發行項

  • 建立提取訂閱

  • 修改提取訂閱

  • 刪除提取訂閱

  • 建立發送訂閱

  • 修改發送訂閱

  • 刪除發送訂閱

  • 同步處理提取訂閱

另請參閱

複寫程式設計概念
複寫預存程式 (Transact-SQL)
編寫複寫指令碼