sp_adddistributiondb (Transact-SQL)

适用于:SQL ServerAzure SQL 托管实例

创建新的分发数据库并安装分发服务器架构。 分发数据库存储过程、架构以及用于复制的元数据。 此存储过程在数据库的分发服务器上master执行,以便创建分发数据库,并安装启用副本 (replica)分发所需的表和存储过程。

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'

要创建的分发数据库的名称。 @databasesysname,无默认值。 如果指定的数据库已存在且尚未标记为分发数据库,则安装启用分发所需的对象,并将该数据库标记为分发数据库。 如果指定的数据库已经作为分发数据库启用,则返回错误。

[ @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. 如果使用 NULLSQL 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. 尚未收到副本 (replica)命令且早于最大分发保留期的订阅被标记为非活动,需要重新初始化。 为每个非活动订阅颁发错误号 21011。 0表示副本 (replica)事务不会存储在分发数据库中。

[ @history_retention = ] history_retention

保留历史记录的小时数。 @history_retention为 int,默认值48为 2 天。

[ @security_mode = ] security_mode

连接到分发服务器时要使用的安全模式。 @security_mode为 int,默认值为 1.

  • 0 指定 SQL Server 身份验证
  • 1指定Windows 身份验证

[ @login = ] N'login'

连接到分发服务器以创建分发数据库时使用的登录名。 @login为 sysname,默认值为 NULL. 如果@security_mode设置为0@login,则需要@login

[ @password = ] N'password'

连接到分发服务器时使用的密码。 @password为 sysname,默认值为 NULL. 如果@security_mode设置为 0,则需要@password

[ @createmode = ] createmode

@createmode为 int,可以是以下值之一。

说明
0 标识为仅供参考。 不支持。 不保证以后的兼容性。
1(默认值) CREATE DATABASE或使用现有数据库,然后应用instdist.sql该文件在分发数据库中创建副本 (replica)对象。
2 标识为仅供参考。 不支持。 不保证以后的兼容性。

[ @from_scripting = ] from_scripting

标识为仅供参考。 不支持。 不保证以后的兼容性。

[ @deletebatchsize_xact = ] deletebatchsize_xact

指定要在清理 MSRepl_Transactions 表中过期事务期间使用的批大小。 @deletebatchsize_xact为 int,默认值为 5000.

适用于: Service Pack 4、SQL Server 2016(13.x)和 Service Pack 2、SQL Server 2017(14.x)及更高版本 SQL Server 2012(14.x)。

[ @deletebatchsize_cmd = ] deletebatchsize_cmd

指定要在清理 MSRepl_Commands 表中过期命令期间使用的批大小。 @deletebatchsize_cmd为 int,默认值为 2000.

适用于: Service Pack 4、SQL Server 2016(13.x)和 Service Pack 2、SQL Server 2017(14.x)及更高版本 SQL Server 2012(14.x)。

返回代码值

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

注解

sp_adddistributiondb用于所有类型的副本 (replica)。 但是,此存储过程只在分发服务器上运行。

在执行之前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

权限

只有 sysadmin 固定服务器角色的成员才能执行sp_adddistributiondb