sp_addmergepushsubscription_agent (Transact-SQL)

適用於:SQL ServerAzure SQL 受控執行個體

將用來排程發送訂閱同步處理至合併式發行集的新代理程序作業。 這個預存程式會在發行集資料庫的發行者端執行。

重要

當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_loginjob_password) 都會以純文字的方式傳給散發者。 您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。 如需詳細資訊,請參閱針對加密連線設定 SQL Server 資料庫引擎

Transact-SQL 語法慣例

注意

Microsoft Entra 標識符 先前稱為 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'

發行集的名稱。 @publication為 sysname,沒有預設值。

[ @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_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_login為 nvarchar(257),預設值為 NULL。 使用 Windows 整合式驗證時,此 Windows 帳戶一律用於代理程式連線到散發者,以及連接到訂閱者和發行者的連線。

[ @job_password = ] N'job_password'

代理程序執行所在的 Windows 帳戶密碼。 @job_password為 sysname,沒有預設值。

重要

可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須將認證儲存在指令碼檔案中,則必須維護這個檔案的安全性,使他人無法在未獲授權的情況下擅自存取。

[ @job_name = ] N'job_name'

現有代理程式作業的名稱。 @job_name為 sysname,預設值為 NULL。 只有在使用現有作業同步處理訂閱,而不是新建立的作業時,才會指定此參數(預設值)。 如果您不是系統管理員固定伺服器角色的成員,則必須在指定@job_name時指定job_loginjob_password

[ @frequency_type = ] frequency_type

值,指出 合併代理程式 何時執行。 @frequency_type為 int,而且可以是下列其中一個值。

Description
1 一次性
2 [視需要]
4 每日
8 每週
16 每月
32 每月相對
64 自動啟動
128 重複執行
NULL (預設值)

注意

指定的值64會導致 合併代理程式 以連續模式執行。 這會對應至設定 -Continuous 代理程序的參數。 如需詳細資訊,請參閱 Replication Merge Agent

[ @frequency_interval = ] frequency_interval

執行 合併代理程式 的天數。 @frequency_interval 為 int,而且可以是下列其中一個值。

Description
1 星期日
2 星期一
3 Tuesday
4 星期三
5 Thursday
6 星期五
7 星期六
8 Day
9 平日
10 週末日
NULL (預設值)

[ @frequency_relative_interval = ] frequency_relative_interval

合併代理程式 的日期。 當@frequency_type設定為 32 (每月相對) 時,會使用此參數。 @frequency_relative_interval為 int,而且可以是下列其中一個值。

Description
1 First
2 Second
4 Third
8 第四個
16 姓氏
NULL (預設值)

[ @frequency_recurrence_factor = ] frequency_recurrence_factor

@frequency_type所使用的週期因數。 @frequency_recurrence_factor為 int,預設值為 0

[ @frequency_subday = ] frequency_subday

在定義的期間重新排程的頻率。 @frequency_subday為 int,而且可以是下列其中一個值。

Description
1 一次
2 Second
4 Minute
8 Hour
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 Synchronization Manager 同步處理訂閱。 @enabled_for_syncmgr為 nvarchar(5),預設值為 false

  • 如果 false為 ,則訂閱未向 Synchronization Manager 註冊。
  • 如果 true為 ,則訂閱會向 Synchronization Manager 註冊,而且不需要啟動 SQL Server Management Studio 即可進行同步處理。

傳回碼值

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

備註

sp_addmergepushsubscription_agent 用於合併式複寫,並使用類似 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

權限

只有系統管理員固定伺服器角色或db_owner固定資料庫角色的成員才能執行 sp_addmergepushsubscription_agent