sp_adddistributiondb (Transact-SQL)

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

建立新的散發資料庫並安裝散發者架構。 散發資料庫會儲存復寫中使用的程序、架構和元數據。 這個預存程式會在資料庫的散發者端 master 執行,以建立散發資料庫,並安裝啟用復寫散發所需的必要數據表和預存程式。

Transact-SQL 語法慣例

語法

sp_adddistributiondb
    [ @database = ] N'database'
    [ , [ @data_folder = ] N'data_folder' ]
    [ , [ @data_file = ] N'data_file' ]
    [ , [ @data_file_size = ] data_file_size ]
    [ , [ @log_folder = ] N'log_folder' ]
    [ , [ @log_file = ] N'log_file' ]
    [ , [ @log_file_size = ] log_file_size ]
    [ , [ @min_distretention = ] min_distretention ]
    [ , [ @max_distretention = ] max_distretention ]
    [ , [ @history_retention = ] history_retention ]
    [ , [ @security_mode = ] security_mode ]
    [ , [ @login = ] N'login' ]
    [ , [ @password = ] N'password' ]
    [ , [ @createmode = ] createmode ]
    [ , [ @from_scripting = ] from_scripting ]
    [ , [ @deletebatchsize_xact = ] deletebatchsize_xact ]
    [ , [ @deletebatchsize_cmd = ] deletebatchsize_cmd ]
[ ; ]

引數

[ @database = ] N'database'

要建立之散發資料庫的名稱。 @database為 sysname,沒有預設值。 如果指定的資料庫已經存在且尚未標示為散發資料庫,則會安裝啟用散發所需的物件,並將資料庫標示為散發資料庫。 如果指定的資料庫已經啟用為散發資料庫,則會傳回錯誤。

[ @data_folder = ] N'data_folder'

用來儲存散發資料庫數據檔的目錄名稱。 @data_folder為 nvarchar(255),預設值為 NULL。 如果 NULL為,則會使用該 SQL Server 實體的數據目錄,例如 C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Data

[ @data_file = ] N'data_file'

資料庫檔案的名稱。 @data_file為 nvarchar(255),預設值為 NULL。 如果 NULL為 ,則預存程式會使用資料庫名稱來建構檔名。

[ @data_file_size = ] data_file_size

初始數據檔案大小以 MB 為單位。 @data_file_size為 int,預設值5為 ,預設值為 5 MB。

[ @log_folder = ] N'log_folder'

資料庫記錄檔的目錄名稱。 @log_folder為 nvarchar(255),預設值為 NULL。 如果 NULL為,則會使用該 SQL Server 實體的資料目錄(例如 , C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Data

[ @log_file = ] N'log_file'

記錄檔的名稱。 @log_file為 nvarchar(255),預設值為 NULL。 如果 NULL為 ,則預存程式會使用資料庫名稱來建構檔名。

[ @log_file_size = ] log_file_size

初始記錄檔大小以 MB 為單位。 @log_file_size為 int,預設值0為 ,它會使用 資料庫引擎 允許的最小記錄檔大小來建立檔案。

[ @min_distretention = ] min_distretention

交易從散發資料庫刪除之前,以小時為單位的最小保留期限。 @min_distretention為 int,預設值為 0

[ @max_distretention = ] max_distretention

刪除交易前的保留期間上限,以小時為單位。 @max_distretention為 int,預設值為 72。 尚未收到複寫命令且早於發佈保留期間上限的訂用帳戶會標示為非使用中,且必須重新初始化。 針對每個非使用中的訂用帳戶發出錯誤號碼 21011。 值 0 表示複寫的交易不會儲存在散發資料庫中。

[ @history_retention = ] history_retention

保留歷程記錄的時數。 @history_retention為 int,預設值48為 ,這表示兩天。

[ @security_mode = ] security_mode

連接到散發者時要使用的安全性模式。 @security_mode為 int,預設值為 1

  • 0 指定 SQL Server 驗證
  • 1指定 Windows 驗證

[ @login = ] N'login'

連接到散發者以建立散發資料庫時所使用的登入名稱。 @login為 sysname,預設值為 NULL如果@security_mode設為 0,則需要@login

[ @password = ] N'password'

連接到散發者時所使用的密碼。 @password為 sysname,預設值為 NULL如果 @security_mode 設定為 0,則需要@password

[ @createmode = ] createmode

@createmode為 int,而且可以是下列其中一個值。

Description
0 僅供參考之用。 不支援。 我們無法保證未來的相容性。
1 (預設值) CREATE DATABASE 或使用現有的資料庫, instdist.sql 然後套用檔案以在散發資料庫中建立復寫物件。
2 僅供參考之用。 不支援。 我們無法保證未來的相容性。

[ @from_scripting = ] from_scripting

僅供參考之用。 不支援。 我們無法保證未來的相容性。

[ @deletebatchsize_xact = ] deletebatchsize_xact

指定要在清除資料表中過期交易 MSRepl_Transactions 期間使用的批次大小。 @deletebatchsize_xact為 int,預設值為 5000

適用於: SQL Server 2012 (11.x) Service Pack 4、SQL Server 2016 (13.x) Service Pack 2、SQL Server 2017 (14.x) 和更新版本。

[ @deletebatchsize_cmd = ] deletebatchsize_cmd

指定要在清除資料表中過期命令 MSRepl_Commands 期間使用的批次大小。 @deletebatchsize_cmd為 int,預設值為 2000

適用於: SQL Server 2012 (11.x) Service Pack 4、SQL Server 2016 (13.x) Service Pack 2、SQL Server 2017 (14.x) 和更新版本。

傳回碼值

0 (成功)或 1 (失敗)。

備註

sp_adddistributiondb 用於所有類型的複寫。 不過,這個預存程式只會在散發者端執行。

執行 之前sp_adddistributiondb,您必須先執行 sp_adddistributor 來設定散發者。

執行之前執行 sp_adddistributorsp_adddistributiondb

範例

此腳本會使用 SQLCMD 腳本變數,而且必須在 SQLCMD 模式中執行。 變數的格式 $(MyVariable)為 。 如需如何在命令行和 SQL Server Management Studio 中使用腳本變數的詳細資訊,請參閱 執行複寫腳本

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

權限

只有系統管理員固定伺服器角色的成員才能執行 sp_adddistributiondb