sp_addpublication_snapshot (Transact-SQL)

适用于:SQL ServerAzure SQL 托管实例

为指定的发布创建快照代理。 此存储过程在发布服务器上对发布数据库执行。

重要

使用远程分发服务器配置发布服务器时,为所有参数提供的值(包括 job_loginjob_password)都会以纯文本方式发送到该分发服务器。 在执行此存储过程之前,应该对发布服务器及其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅将 SQL Server 数据库引擎配置为使用加密连接

Transact-SQL 语法约定

语法

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' ]
[ ; ]

参数

[ @publication = ] N'publication'

发布的名称。 @publicationsysname,无默认值。

[ @frequency_type = ] frequency_type

执行快照代理的频率。 @frequency_typeint,可以是以下值之一。

说明
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 = Friday |

64 = 星期六
16 月@frequency_interval 日。
32 @frequency_interval是以下值之一:

1 = 星期日 |

2 = 星期一 |

3 = 星期二 |

4 = 星期三 |

5 = 星期四 |

6 = Friday |

7 = 星期六 |

8 = Day |

9 = 工作日 |

10 = 周末日期
64 @frequency_interval未使用。
128 @frequency_interval未使用。

[ @frequency_subday = ] frequency_subday

freq_subday_interval的单位@frequency_subdayint,可以是以下值之一。

说明
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为 11:59:59:59(以 24 小时制为单位)。

[ @snapshot_job_name = ] N'快照_job_name'

使用现有作业时,现有快照代理作业名称的名称。 @快照_job_namenvarchar(100),默认值为 NULL. 此参数用于内部使用,在创建新发布时不应指定此参数。 如果指定了 @快照_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 指定 Microsoft Entra 令牌身份验证,从 SQL Server 2022 (16.x) CU 6 开始。

[ @publisher_login = ] N'publisher_login'

连接发布服务器时所使用的登录名。 @publisher_login为 sysname,默认值为 NULL. 当@publisher_security_mode为0,必须指定@publisher_login。 如果@publisher_loginNULL@publisher_security_mode1,则在连接到发布服务器时使用@job_login中指定的帐户。

[ @publisher_password = ] N'publisher_password'

连接到发布服务器时使用的密码。 @publisher_password为 sysname,默认值为 NULL.

重要

不要将身份验证信息存储在脚本文件中。 为了提高安全性,建议您在运行时提供登录名和密码。

[ @job_login = ] N'job_login'

运行代理的帐户的登录名。 在Azure SQL 托管实例,使用 SQL Server 帐户。 @job_loginnvarchar(257),默认值为 NULL. 此帐户始终用于与分发服务器的代理连接。 创建新的快照代理作业时,必须提供此参数。

对于非 SQL Server 发布服务器,此登录名必须与 sp_adddistpublisher (Transact-SQL) 中指定的登录名相同。

[ @job_password = ] N'job_password'

运行代理的 Windows 帐户的密码。 @job_password为 sysname,无默认值。 创建新的快照代理作业时,必须提供此参数。

重要

不要将身份验证信息存储在脚本文件中。 为了提高安全性,建议您在运行时提供登录名和密码。

[ @publisher = ] N'publisher'

指定非 SQL Server 发布服务器。 @publisher为 sysname,默认值为 NULL.

在 SQL Server 发布服务器上创建快照代理时,不应使用@publisher。

[ @distributor_security_mode = ] distributor_security_mode

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

[ @distributor_login = ] N'distributor_login'

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

[ @distributor_password = ] N'distributor_password'

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

返回代码值

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

注解

sp_addpublication_snapshot用于快照 副本 (replica)、事务副本 (replica)和合并副本 (replica)。

示例

-- 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 固定服务器角色的成员db_owner固定数据库角色的成员才能执行sp_addpublication_snapshot