适用于:SQL ServerAzure
SQL Managed Instance
为指定的发布创建快照代理。 此存储过程在发布服务器上对发布数据库执行。
重要
使用远程分发服务器配置发布服务器时,为所有参数(包括 @job_login 和 @job_password)提供的值将作为纯文本发送到分发服务器。 在执行此存储过程之前,应该对发布服务器及其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅将 SQL Server 数据库引擎配置为使用加密连接。
语法
sys.sp_addpublication_snapshot
[ @publication = ] N'publication'
[ , [ @frequency_type = ] frequency_type ]
[ , [ @frequency_interval = ] frequency_interval ]
[ , [ @frequency_subday = ] frequency_subday ]
[ , [ @frequency_subday_interval = ] frequency_subday_interval ]
[ , [ @frequency_relative_interval = ] frequency_relative_interval ]
[ , [ @frequency_recurrence_factor = ] frequency_recurrence_factor ]
[ , [ @active_start_date = ] active_start_date ]
[ , [ @active_end_date = ] active_end_date ]
[ , [ @active_start_time_of_day = ] active_start_time_of_day ]
[ , [ @active_end_time_of_day = ] active_end_time_of_day ]
[ , [ @snapshot_job_name = ] N'snapshot_job_name' ]
[ , [ @publisher_security_mode = ] publisher_security_mode ]
[ , [ @publisher_login = ] N'publisher_login' ]
[ , [ @publisher_password = ] N'publisher_password' ]
[ , [ @job_login = ] N'job_login' ]
[ , [ @job_password = ] N'job_password' ]
[ , [ @publisher = ] N'publisher' ]
[ , [ @distributor_security_mode = ] distributor_security_mode ]
[ , [ @distributor_login = ] N'distributor_login' ]
[ , [ @distributor_password = ] N'distributor_password' ]
[ , [ @job_security_mode = ] job_security_mode ]
[ ; ]
参数
[ @publication = ] N'publication'
发布的名称。 @publication 是 系统名,没有默认值。
[ @frequency_type = ] frequency_type
执行快照代理的频率。 @frequency_type 是 int,可以是以下值之一。
| 值 | 说明 |
|---|---|
1 |
一次 |
4(默认值) |
每日 |
8 |
每周 |
16 |
每月 |
32 |
每月,相对于频率间隔 |
64 |
SQL Server 代理启动时 |
128 |
计算机空闲时运行 |
[ @frequency_interval = ] frequency_interval
要应用于@frequency_type设置的频率的值。 @frequency_interval 是 int,可以是以下值之一。
| frequency_type 的值 | 对 frequency_interval 的影响 |
|---|---|
1 |
@frequency_interval 未被使用。 |
4(默认值) |
每 @frequency_interval 天,默认值为每日。 |
8 |
@frequency_interval 是以下一项或多项(与 |(按位或) 逻辑运算符):1 = 星期日 |2 = 星期一 |4 = 星期二 |8 = 星期三 |16 = 星期四 |32 = 星期五 |64 = 星期六 |
16 |
在 月@frequency_interval 日。 |
32 |
@frequency_interval 是以下值之一:1 = 星期日 |2 = 星期一 |3 = 星期二 |4 = 星期三 |5 = 星期四 |6 = 星期五 |7 = 星期六 |8 = 白天 |9 = 工作日 |10 = 周末日 |
64 |
@frequency_interval 未被使用。 |
128 |
@frequency_interval 未被使用。 |
[ @frequency_subday = ] frequency_subday
freq_subday_interval的单位。 @frequency_subday 是 int,可以是这些值之一。
| 值 | 说明 |
|---|---|
1 |
一次 |
2 |
Second |
4(默认值) |
Minute |
8 |
小时 |
[ @frequency_subday_interval = ] frequency_subday_interval
间隔时间 为@frequency_subday分钟。
@frequency_subday_interval 为 int,默认值为 5。
[ @frequency_relative_interval = ] frequency_relative_interval
快照代理运行的日期。
@frequency_relative_interval 为 int,默认值为 1。
[ @frequency_recurrence_factor = ] frequency_recurrence_factor
@frequency_type使用的重复因子。
@frequency_recurrence_factor 为 int,默认值为 0。
@active_start_date [ = ] active_start_date
第一次计划快照代理的日期,格式为 yyyyMMdd。
@active_start_date 为 int,默认值为 0。
@active_end_date [ = ] active_end_date
快照代理停止计划的日期,格式为 yyyyMMdd。
@active_end_date 为 int,默认值为 99991231,这意味着9999年12月31日。
[ @active_start_time_of_day = ] active_start_time_of_day
第一次计划快照代理的时间,格式为 HHmmss。
@active_start_time_of_day 为 int,默认值为 0。
[ @active_end_time_of_day = ] active_end_time_of_day
快照代理停止计划的时间,格式为 HHmmss。
@active_end_time_of_day 为 int,默认值为 235959,这意味着在24小时制下测量的11:59:59。
[ @snapshot_job_name = ] N'snapshot_job_name'
使用现有作业时,现有快照代理作业名称的名称。
@snapshot_job_name 是 nvarchar(100),默认值为 NULL。 此参数用于内部使用,在创建新发布时不应指定此参数。 如果指定了@snapshot_agent_name,则必须指定@job_login和@job_passwordNULL。
[ @publisher_security_mode = ] publisher_security_mode
注意
Microsoft Entra ID 以前称为 Azure Active Directory (Azure AD)。
连接到发布服务器时代理使用的安全模式。
@publisher_security_mode 为 int,默认值为 1。 必须为非 SQL Server 发布服务器指定值 0 。 请尽可能使用 Windows 身份验证。 以下值定义安全模式:
-
0指定 SQL Server 身份验证。 -
1指定 Windows 身份验证。 -
2指定 Microsoft Entra 密码认证,从 SQL Server 2022 (16.x) CU 6 开始。 -
3指定 Microsoft Entra 集成身份验证,从 SQL Server 2022 (16.x) CU 6 起。 -
4指定从 SQL Server 2022 (16.x) CU 6 起的 Microsoft Entra 令牌身份验证。 -
5指定使用 Microsoft Entra 托管身份进行托管身份验证,使用分发者的默认托管身份。 此值仅适用于事务复制,当 Publisher 和 Distributor 都是 Azure SQL 托管实例 资源时。
[ @publisher_login = ] N'publisher_login'
连接发布服务器时所使用的登录名。
@publisher_login 是 系统名,默认为 NULL。
当@publisher_security_mode为0时,必须指定@publisher_login。 如果@publisher_login且NULL@publisher_security_mode1,则在连接到发布服务器时使用@job_login中指定的帐户。
[ @publisher_password = ] N'publisher_password'
连接到发布服务器时使用的密码。
@publisher_password 是 系统名,默认为 NULL。
重要
不要将身份验证信息存储在脚本文件中。 为了提高安全性,建议您在运行时提供登录名和密码。
[ @job_login = ] N'job_login'
运行代理的帐户的登录名。 在Azure SQL 托管实例,使用 SQL Server 帐户。
@job_login 为 nvarchar(257),默认值为 NULL。 此帐户始终用于与分发服务器的代理连接。 创建新的快照代理作业时,必须提供此参数。
对于非 SQL Server 发布服务器,此登录名必须与sp_adddistpublisher中指定的登录名相同。
[ @job_password = ] N'job_password'
运行代理的 Windows 帐户的密码。 @job_password 是 sysname,没有默认值。 创建新的快照代理作业时,必须提供此参数。
重要
不要将身份验证信息存储在脚本文件中。 为了提高安全性,建议您在运行时提供登录名和密码。
[ @publisher = ] N'publisher'
指定非 SQL Server 发布服务器。
@publisher 是 系统名,默认为 NULL。
在SQL Server Publisher创建快照代理时不应使用 @publisher。
[ @distributor_security_mode = ] distributor_security_mode
标识为仅供参考。 不支持。 不保证以后的兼容性。
[ @distributor_login = ] N'distributor_login'
标识为仅供参考。 不支持。 不保证以后的兼容性。
[ @distributor_password = ] N'distributor_password'
标识为仅供参考。 不支持。 不保证以后的兼容性。
[ @job_security_mode = ] job_security_mode
标识为仅供参考。 不支持。 不保证以后的兼容性。
返回代码值
0 (成功)或 1 (失败)。
注解
sp_addpublication_snapshot 用于快照复制、事务复制和合并复制。
示例
-- To avoid storing the login and password in the script file, the values
-- are passed into SQLCMD as scripting variables. 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".
DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks';
SET @publication = N'AdvWorksProductTran';
-- Windows account used to run the Log Reader and Snapshot Agents.
SET @login = $(Login);
-- This should be passed at runtime.
SET @password = $(Password);
-- Enable transactional or snapshot replication on the publication database.
EXEC sp_replicationdboption
@dbname=@publicationDB,
@optname=N'publish',
@value = N'true';
-- Execute sp_addlogreader_agent to create the agent job.
EXEC sp_addlogreader_agent
@job_login = @login,
@job_password = @password,
-- Explicitly specify the use of Windows Integrated Authentication (default)
-- when connecting to the Publisher.
@publisher_security_mode = 1;
-- Create a new transactional publication with the required properties.
EXEC sp_addpublication
@publication = @publication,
@status = N'active',
@allow_push = N'true',
@allow_pull = N'true',
@independent_agent = N'true';
-- Create a new snapshot job for the publication, using a default schedule.
EXEC sp_addpublication_snapshot
@publication = @publication,
@job_login = @login,
@job_password = @password,
-- Explicitly specify the use of Windows Integrated Authentication (default)
-- when connecting to the Publisher.
@publisher_security_mode = 1;
GO
权限
只有 sysadmin 固定服务器角色的成员