sp_addpullsubscription_agent (Transact-SQL)

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

加入新的排程代理程式作業,用來將提取訂閱同步處理至交易式發行集。 這個預存程式會在訂閱資料庫的訂閱者端執行。

Transact-SQL 語法慣例

語法

sp_addpullsubscription_agent
    [ @publisher = ] N'publisher'
    [ , [ @publisher_db = ] N'publisher_db' ]
    , [ @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' ]
    [ , [ @distributor = ] N'distributor' ]
    [ , [ @distribution_db = ] N'distribution_db' ]
    [ , [ @distributor_security_mode = ] distributor_security_mode ]
    [ , [ @distributor_login = ] N'distributor_login' ]
    [ , [ @distributor_password = ] N'distributor_password' ]
    [ , [ @optional_command_line = ] N'optional_command_line' ]
    [ , [ @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 ]
    [ , [ @distribution_jobid = ] distribution_jobid OUTPUT ]
    [ , [ @encrypted_distributor_password = ] encrypted_distributor_password ]
    [ , [ @enabled_for_syncmgr = ] N'enabled_for_syncmgr' ]
    [ , [ @ftp_address = ] N'ftp_address' ]
    [ , [ @ftp_port = ] ftp_port ]
    [ , [ @ftp_login = ] N'ftp_login' ]
    [ , [ @ftp_password = ] N'ftp_password' ]
    [ , [ @alt_snapshot_folder = ] N'alt_snapshot_folder' ]
    [ , [ @working_directory = ] N'working_directory' ]
    [ , [ @use_ftp = ] N'use_ftp' ]
    [ , [ @publication_type = ] publication_type ]
    [ , [ @dts_package_name = ] N'dts_package_name' ]
    [ , [ @dts_package_password = ] N'dts_package_password' ]
    [ , [ @dts_package_location = ] N'dts_package_location' ]
    [ , [ @reserved = ] N'reserved' ]
    [ , [ @offloadagent = ] N'offloadagent' ]
    [ , [ @offloadserver = ] N'offloadserver' ]
    [ , [ @job_name = ] N'job_name' ]
    [ , [ @job_login = ] N'job_login' ]
    [ , [ @job_password = ] N'job_password' ]
[ ; ]

引數

[ @publisher = ] N'publisher'

發行者的名稱。 @publisher為 sysname,沒有預設值。

注意

伺服器名稱可以指定為 <Hostname>,<PortNumber>。 使用自定義埠在Linux或 Windows 上部署 SQL Server,並停用瀏覽器服務時,請指定連線的埠號碼。 遠端散發者的自定義埠號碼使用適用於 SQL Server 2019 (15.x) 和更新版本。

[ @publisher_db = ] N'publisher_db'

發行者資料庫的名稱。 @publisher_db為 sysname,預設值為 NULLOracle 發行者會忽略@publisher_db

[ @publication = ] N'publication'

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

[ @subscriber = ] N'subscriber'

如果訂閱者資料庫位於可用性群組中,則為訂閱者實例的名稱或 AG 接聽程式的名稱。 @subscriber為 sysname,預設值為 NULL

注意

此參數已被取代,而且會維護腳本的回溯相容性。

針對屬於 AG 的訂閱者執行sp_addpullsubscription_agent時,請將@subscriber設定為 AG 接聽程式名稱。 如果您在 CU 16 之前執行 SQL Server 2016 (13.x) 和舊版,或在 CU 16 之前執行 SQL Server 2017 (14.x),則預存程式會執行而不會傳回錯誤,但複寫 散發代理程式 上的 @subscriber 參數不會參考 AG 接聽程式名稱;參數是以執行命令的訂閱者伺服器名稱建立。 若要修正此問題,請使用 AG 接聽程式名稱值手動更新 散發代理程式 作業@subscriber參數。

[ @subscriber_db = ] N'subscriber_db'

訂閱資料庫的名稱。 @subscriber_db為 sysname,預設值為 NULL

注意

此參數已被取代,而且會維護腳本的回溯相容性。

[ @subscriber_security_mode = ] subscriber_security_mode

同步處理時連接到訂閱者時要使用的安全性模式。 @subscriber_security_mode 為 int,預設值為 NULL

  • 0 指定 SQL Server 驗證
  • 1指定 Windows 驗證

注意

此參數已被取代,而且會維護腳本的回溯相容性。 散發代理程式 一律會使用 Windows 驗證連線到本機訂閱者。 如果為此參數指定或 1 以外的NULL值,則會傳回警告訊息。

[ @subscriber_login = ] N'subscriber_login'

同步處理時連接到訂閱者時要使用的訂閱者登入。 @subscriber_login為 sysname,預設值為 NULL

注意

此參數已被取代,而且會維護腳本的回溯相容性。 如果為此參數指定值,則會傳回警告訊息,但會忽略該值。

[ @subscriber_password = ] N'subscriber_password'

訂閱者密碼。 如果subscriber_security_mode設定為 0,則需要subscriber_password@subscriber_password為 sysname,預設值為 NULL。 如果使用訂閱者密碼,則會自動加密。

注意

此參數已被取代,而且會維護腳本的回溯相容性。 如果為此參數指定值,則會傳回警告訊息,但會忽略該值。

[ @distributor = ] N'distributor'

散發者的名稱。 @distributor為 sysname,預設值為 @publisher指定的值。

[ @distribution_db = ] N'distribution_db'

散發資料庫的名稱。 @distribution_db為 sysname,預設值為 NULL

[ @distributor_security_mode = ] distributor_security_mode

注意

Microsoft Entra 標識符 先前稱為 Azure Active Directory (Azure AD)。

同步處理時連接到散發者時要使用的安全性模式。 @distributor_security_mode為 int,預設值為 1。 下列值會定義安全性模式:

  • 0 指定 SQL Server 驗證。
  • 1指定 Windows 驗證。
  • 2 指定從 SQL Server 2022 (16.x) CU 6 開始的 Microsoft Entra 密碼驗證。
  • 3 指定從 SQL Server 2022 (16.x) CU 6 開始的 Microsoft Entra 整合式驗證。
  • 4 指定從 SQL Server 2022 (16.x) CU 6 開始的 Microsoft Entra 令牌驗證。

重要

儘可能使用 Windows 驗證。

[ @distributor_login = ] N'distributor_login'

在同步處理時連接到散發者時要使用的散發者登入。 @distributor_login為 sysname,預設值為 NULL如果@distributor_security_mode設定為 0,則需要@distributor_login

[ @distributor_password = ] N'distributor_password'

散發者密碼。 如果distributor_security_mode設定為 0,則需要distributor_password@distributor_password為 sysname,預設值為 NULL

重要

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

[ @optional_command_line = ] N'optional_command_line'

提供給 散發代理程式 的選擇性命令提示字元。 例如,-DefinitionFile C:\Distdef.txt-CommitBatchSize 10@optional_command_line為 nvarchar(4000),預設值為空字串。

[ @frequency_type = ] frequency_type

排程 散發代理程式的頻率。 @frequency_type為 int,而且可以是下列其中一個值。

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

注意

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

[ @frequency_interval = ] frequency_interval

要套用至 @frequency_type設定頻率的值。 @frequency_interval為 int,預設值為 1

[ @frequency_relative_interval = ] frequency_relative_interval

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

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

[ @frequency_recurrence_factor = ] frequency_recurrence_factor

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

[ @frequency_subday = ] frequency_subday

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

Description
1 (預設值) 一次
2 Second
4 Minute
8 Hour

[ @frequency_subday_interval = ] frequency_subday_interval

@frequency_subday的間隔。 @frequency_subday_interval 為 int,預設值為 1

[ @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,預設值為 0

[ @active_start_date = ] active_start_date

第一次排程 散發代理程式 的日期,格式為 yyyyMMdd@active_start_date為 int,預設值為 0

[ @active_end_date = ] active_end_date

散發代理程式 停止排程的日期,格式為 yyyyMMdd@active_end_date為 int,預設值為 0

[ @distribution_jobid = ] distribution_jobid OUTPUT

此作業 散發代理程式 的標識碼。 @distribution_jobid是 binary(16)類型的 OUTPUT 參數,預設值為 NULL

[ @encrypted_distributor_password = ] encrypted_distributor_password

@encrypted_distributor_password為 bit,預設值為 0

注意

不再支援設定 @encrypted_distributor_password 。 嘗試將這個 參數設定為 1 會導致錯誤。

[ @enabled_for_syncmgr = ] N'enabled_for_syncmgr'

指定是否可以透過 Microsoft Synchronization Manager 同步處理訂閱。 @enabled_for_syncmgr為 nvarchar(5),預設值為 false

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

[ @ftp_address = ] N'ftp_address'

此參數已被取代,而且會維護腳本的回溯相容性。

[ @ftp_port = ] ftp_port

此參數已被取代,而且會維護腳本的回溯相容性。

[ @ftp_login = ] N'ftp_login'

此參數已被取代,而且會維護腳本的回溯相容性。

[ @ftp_password = ] N'ftp_password'

此參數已被取代,而且會維護腳本的回溯相容性。

[ @alt_snapshot_folder = ] N'alt_snapshot_folder'

指定快照集替代資料夾的位置。 @alt_snapshot_folder為 nvarchar(255),預設值為 NULL

[ @working_directory = ] N'working_directory'

用來儲存發行集數據和架構檔案的工作目錄名稱。 @working_directory為 nvarchar(255),預設值為 NULL。 名稱應以 UNC 格式指定。

[ @use_ftp = ] N'use_ftp'

指定使用 FTP 而不是一般通訊協議來擷取快照集。 @use_ftp為 nvarchar(5),預設值為 false

[ @publication_type = ] publication_type

指定發行集的復寫類型。 @publication_type為 tinyint,預設值為 0

  • 如果 0為 ,則 publication 是交易類型。
  • 如果 1為 ,則發行集是快照集類型。
  • 如果 2為 ,則 publication 是合併類型。

[ @dts_package_name = ] N'dts_package_name'

指定 DTS 封裝的名稱。 @dts_package_name為 sysname 預設值為 NULL。 例如,若要指定 的 DTSPub_Package封裝,參數會是 @dts_package_name = N'DTSPub_Package'

[ @dts_package_password = ] N'dts_package_password'

如果有的話,指定封裝上的密碼。 @dts_package_password是 sysname,預設值NULL為 ,這表示密碼不在套件上。

注意

如果 指定@dts_package_name ,您必須指定密碼。

[ @dts_package_location = ] N'dts_package_location'

指定封裝位置。 @dts_package_location為 nvarchar(12),預設值為 subscriber。 封裝的位置可以是 distributorsubscriber

[ @reserved = ] N'reserved'

僅供參考之用。 不支援。 我們無法保證未來的相容性。

[ @offloadagent = ] N'offloadagent'

此參數已被取代,而且會維護腳本的回溯相容性。 如果為此參數指定值,則會傳回警告訊息,但會忽略該值。 將@offloadagent設定為產生錯誤以外的false值。

[ @offloadserver = ] N'offloadserver'

此參數已被取代,而且會維護腳本的回溯相容性。 如果為此參數指定值,則會傳回警告訊息,但會忽略該值。 將@offloadserver設定為產生錯誤以外的false值。

[ @job_name = ] N'job_name'

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

[ @job_login = ] N'job_login'

代理程序執行所在的 Windows 帳戶登入。 @job_login為 nvarchar(257),沒有預設值。 此 Windows 帳戶一律用於與訂閱者的代理程式連線。

[ @job_password = ] N'job_password'

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

重要

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

傳回碼值

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

備註

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

-- Execute this batch at the Subscriber.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @publisher = $(PubServer);
SET @publicationDB = N'AdventureWorks2022';

-- At the subscription database, create a pull subscription 
-- to a transactional publication.
USE [AdventureWorks2022Replica]
EXEC sp_addpullsubscription 
  @publisher = @publisher, 
  @publication = @publication, 
  @publisher_db = @publicationDB;

-- Add an agent job to synchronize the pull subscription.
EXEC sp_addpullsubscription_agent 
  @publisher = @publisher, 
  @publisher_db = @publicationDB, 
  @publication = @publication, 
  @distributor = @publisher, 
  @job_login = $(Login), 
  @job_password = $(Password);
GO

權限

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