sp_adddistributiondb (Transact-SQL)

建立新的散發資料庫並安裝散發者結構描述。散發資料庫會儲存複寫所用的預存程序、結構描述和中繼資料。這個預存程序執行於 master 資料庫的散發者端,以便建立散發資料庫,它會安裝必要的資料表以及啟用複寫散發時所需要的預存程序。

主題連結圖示Transact-SQL 語法慣例

語法

sp_adddistributiondb [ @database= ] 'database' 
    [ , [ @data_folder= ] 'data_folder' ] 
    [ , [ @data_file= ] 'data_file' ] 
    [ , [ @data_file_size= ] data_file_size ] 
    [ , [ @log_folder= ] 'log_folder' ] 
    [ , [ @log_file= ] '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= ] 'login' ] 
    [ , [ @password= ] 'password' ] 
    [ , [ @createmode= ] createmode ]
    [ , [ @from_scripting = ] from_scripting ]

引數

  • [ @database=] 'database'
    這是要建立的散發資料庫名稱。databasesysname,沒有預設值。如果指定的資料庫已經存在,且尚未標示為散發資料庫,便會安裝啟用散發所需要的物件,且會將資料庫標示為散發資料庫。如果指定的資料庫已啟用為散發資料庫,就會傳回錯誤。
  • [ @data_folder=] 'data_folder'
    這是用來儲存散發資料庫資料檔的目錄名稱。data_foldernvarchar(255),預設值是 NULL如果是 NULL,就會使用這個 Microsoft SQL Server 執行個體的資料目錄,例如,C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL.1\Data
  • [ @data_file=] 'data_file'
    這是資料庫檔案的名稱。data_filenvarchar(255),預設值是 database。如果是 NULL,這個預存程序會利用資料庫名稱來建構檔案名稱。
  • [ @data_file_size=] data_file_size
    這是初始資料檔大小 (以 MB 為單位)。data_file_sizeint,預設值是 5MB。
  • [ @log_folder=] 'log_folder'
    這是資料庫記錄檔的目錄名稱。log_foldernvarchar(255),預設值是 NULL。如果是 NULL,就會使用這個 SQL Server 執行個體的資料目錄 (如 C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL.1\Data)。
  • [ @log_file=] 'log_file'
    這是記錄檔的名稱。log_filenvarchar(255),預設值是 NULL。如果是 NULL,這個預存程序會利用資料庫名稱來建構檔案名稱。
  • [ @log_file_size=] log_file_size
    這是初始記錄檔大小 (以 MB 為單位)。log_file_sizeint,預設值是 0 MB,表示利用 SQL Server 所允許的最小記錄檔大小來建立檔案大小。
  • [ @min_distretention=] min_distretention
    這是從散發資料庫中刪除交易之前的最小保留期限 (以小時為單位)。min_distretentionint,預設值是 0 小時。
  • [ @max_distretention=] max_distretention
    這是在刪除交易之前的最大保留期限 (以小時為單位)。max_distretentionint,預設值是 72 小時未收到超過最大散發保留期限之複寫命令的訂閱,會被標示為非使用中,必須重新初始化。針對每個非使用中的訂閱,系統會發出 RAISERROR 21011。0 值表示複寫的交易未儲存在散發資料庫中。
  • [ @history_retention=] history_retention
    這是保留歷程記錄的時數。history_retentionint,預設值是 48 小時。
  • [ @security_mode=] security_mode
    這是用來連接到散發者的安全性模式。security_modeint,預設值是 1。0 指定 SQL Server 驗證;1 指定 Windows 整合式驗證。
  • [ @login=] 'login'
    這是連接到散發者來建立散發資料庫時,所用的登入名稱。如果 security_mode 設為 0,則需要這個項目。loginsysname,預設值是 NULL。
  • [ @password=] 'password'
    這是連接到散發者時所用的密碼。如果 security_mode 設為 0,則需要這個項目。passwordsysname,預設值是 NULL。
  • [ @createmode=] createmode
    createmodeint,預設值是 0,它可以是下列值之一。

    描述

    0 (預設值)

    CREATE DATABASE,利用一份散發資料庫模型檔 (distmdl.mdf) 來附加散發資料庫

    1

    CREATE DATABASE,或使用現有的資料庫,再套用 instdist.sql 檔來建立散發資料庫中的複寫物件。

    2

    僅供內部使用。

  • [ @from_scripting= ] from_scripting
    僅供內部使用。

傳回碼值

0 (成功) 或 1 (失敗)

備註

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

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

在執行 sp_adddistributor 之前,先執行 sp_adddistributiondb

權限

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

範例

-- 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'AdventureWorks'; 

-- 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 AdventureWorks 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_changedistributiondb (Transact-SQL)
sp_dropdistributiondb (Transact-SQL)
sp_helpdistributiondb (Transact-SQL)
系統預存程序 (Transact-SQL)

其他資源

How to: Configure Publishing and Distribution (Replication Transact-SQL Programming)
設定散發

說明及資訊

取得 SQL Server 2005 協助