sp_addmergepushsubscription_agent (Transact-SQL)

适用于:SQL ServerAzure SQL 托管实例

添加新的代理作业,用于制定合并发布推送订阅的同步计划。 此存储过程在发布服务器上对发布数据库执行。

重要

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

Transact-SQL 语法约定

注意

Microsoft Entra ID 以前称为 Azure Active Directory(Azure AD)。

语法

sp_addmergepushsubscription_agent
    [ @publication = ] N'publication'
    [ , [ @subscriber = ] N'subscriber' ]
    [ , [ @subscriber_db = ] N'subscriber_db' ]
    [ , [ @subscriber_security_mode = ] subscriber_security_mode ]
    [ , [ @subscriber_login = ] N'subscriber_login' ]
    [ , [ @subscriber_password = ] N'subscriber_password' ]
    [ , [ @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' ]
    [ , [ @job_name = ] N'job_name' ]
    [ , [ @frequency_type = ] frequency_type ]
    [ , [ @frequency_interval = ] frequency_interval ]
    [ , [ @frequency_relative_interval = ] frequency_relative_interval ]
    [ , [ @frequency_recurrence_factor = ] frequency_recurrence_factor ]
    [ , [ @frequency_subday = ] frequency_subday ]
    [ , [ @frequency_subday_interval = ] frequency_subday_interval ]
    [ , [ @active_start_time_of_day = ] active_start_time_of_day ]
    [ , [ @active_end_time_of_day = ] active_end_time_of_day ]
    [ , [ @active_start_date = ] active_start_date ]
    [ , [ @active_end_date = ] active_end_date ]
    [ , [ @enabled_for_syncmgr = ] N'enabled_for_syncmgr' ]
[ ; ]

参数

[ @publication = ] N'publication'

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

[ @subscriber = ] N'subscriber'

订阅服务器的名称。 @subscriber为 sysname,默认值为 NULL.

[ @subscriber_db = ] N'subscriber_db'

订阅数据库的名称。 @subscriber_db为 sysname,默认值为 NULL.

[ @subscriber_security_mode = ] subscriber_security_mode

同步时连接到订阅服务器时要使用的安全模式。 @subscriber_security_mode为 smallint,默认值为 1. 以下值定义安全模式:

  • 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 开始。

重要

请尽可能使用 Windows 身份验证。

[ @subscriber_login = ] N'subscriber_login'

同步时连接到订阅服务器的订阅服务器时要使用的订阅服务器登录名。 @subscriber_login为 sysname,默认值为 NULL. 如果@subscriber_security_mode设置为0@subscriber_login,则需要@subscriber_login

[ @subscriber_password = ] N'subscriber_password'

SQL Server 身份验证的订阅服务器密码。 @subscriber_password为 sysname,默认值为 NULL. 如果@subscriber_security_mode设置为 0,则需要@subscriber_password 。 如果使用订阅者密码,则会自动加密。

重要

如果可能,请在运行时提示用户输入安全凭据。 如果必须在脚本文件中存储凭据,则必须保护文件以防止未经授权的访问。

[ @publisher_security_mode = ] publisher_security_mode

同步时连接到发布服务器时要使用的安全模式。 @publisher_security_mode为 smallint,默认值为 1. 以下值定义安全模式:

  • 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 开始。

重要

请尽可能使用 Windows 身份验证。

[ @publisher_login = ] N'publisher_login'

同步时连接到发布服务器时要使用的登录名。 @publisher_login为 sysname,默认值为 NULL.

[ @publisher_password = ] N'publisher_password'

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

重要

不要使用空密码。 请使用强密码。 如果可能,请在运行时提示用户输入安全凭据。 如果必须在脚本文件中存储凭据,则必须保护文件以防止未经授权的访问。

[ @job_login = ] N'job_login'

运行代理的 Windows 帐户的登录名。 @job_loginnvarchar(257),默认值为 NULL. 此 Windows 帐户始终用于到分发服务器的代理连接,以及在使用 Windows 集成身份验证时用于到订阅服务器和发布服务器的连接。

[ @job_password = ] N'job_password'

运行代理的 Windows 帐户的密码。 @job_password为 sysname,无默认值。

重要

如果可能,请在运行时提示用户输入安全凭据。 如果必须在脚本文件中存储凭据,则必须保护文件以防止未经授权的访问。

[ @job_name = ] N'job_name'

现有代理作业的名称。 @job_name为 sysname,默认值为 NULL. 仅当使用现有作业而不是(默认的)新创建的作业同步订阅时,才指定此参数。 如果你不是 sysadmin 固定服务器角色的成员,则必须在指定@job_name时指定job_loginjob_password

[ @frequency_type = ] frequency_type

一个值,该值指示合并代理何时运行。 @frequency_typeint,可以是以下值之一。

说明
1 一次
2 按需
4 每日
8 每周
16 每月
32 与“每月”选项相关
64 自动启动
128 定期
NULL(默认值)

注意

指定一个值,64使合并代理在连续模式下运行。 这对应于设置 -Continuous 代理的参数。 有关详细信息,请参阅 Replication Merge Agent

[ @frequency_interval = ] frequency_interval

合并代理运行的日子。 @frequency_interval为 int,可以是以下值之一。

说明
1 星期日
2 星期一
3 星期二
4 星期三
5 星期四
6 星期五
7 星期六
8
9 工作日
10 周末
NULL(默认值)

[ @frequency_relative_interval = ] frequency_relative_interval

合并代理的日期。 当@frequency_type设置为32(每月相对)时,将使用此参数。 @frequency_relative_intervalint,可以是以下值之一。

说明
1 第一个
2 第二个
4 第三个
8 第四
16 Last
NULL(默认值)

[ @frequency_recurrence_factor = ] frequency_recurrence_factor

@frequency_type使用的重复因子。 @frequency_recurrence_factor为 int,默认值为 0.

[ @frequency_subday = ] frequency_subday

在定义的时间段内重新安排的频率。 @frequency_subday为 int,可以是以下值之一。

说明
1 一次
2 Second
4 Minute
8 小时
NULL(默认值)

[ @frequency_subday_interval = ] frequency_subday_interval

@frequency_subday间隔。 @frequency_subday_interval为 int,默认值为 NULL.

[ @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.

[ @active_start_date = ] active_start_date

第一次计划合并代理的日期,格式为 yyyyMMdd@active_start_date为 int,默认值为 0.

[ @active_end_date = ] active_end_date

合并代理停止计划的日期,格式为 yyyyMMdd@active_end_date为 int,默认值为 99991231.

[ @enabled_for_syncmgr = ] N'enabled_for_syncmgr'

指定是否可以通过 Windows 同步管理器同步订阅。 @enabled_for_syncmgrnvarchar(5),默认值为 false.

  • 如果 false订阅未注册到 Synchronization Manager。
  • 如果 true订阅已注册到同步管理器,并且可以在不启动 SQL Server Management Studio 的情况下进行同步。

返回代码值

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

注解

sp_addmergepushsubscription_agent用于合并副本 (replica),并使用类似于sp_addpushsubscription_agent的功能。

示例

-- 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".

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @hostname AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorksReplica'; 
SET @hostname = N'adventure-works\david8'

-- Add a push subscription to a merge publication.
USE [AdventureWorks2022];
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'push',
  @hostname = @hostname;

--Add an agent job to synchronize the push subscription.
EXEC sp_addmergepushsubscription_agent 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @job_login = $(Login), 
  @job_password = $(Password);
GO

权限

只有 sysadmin 固定服务器角色的成员db_owner固定数据库角色的成员才能执行sp_addmergepushsubscription_agent