適用於: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 是 sysname,沒有預設值。
[ @frequency_type = ] frequency_type
執行 快照集代理程式的頻率。 @frequency_type 為 int,且可以是以下其中一個值。
| 值 | Description |
|---|---|
1 |
一次 |
4 (預設) |
每日 |
8 |
每週 |
16 |
每月 |
32 |
每月,相對於頻率間隔 |
64 |
當 SQL Server Agent 啟動時 |
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,可以是這些數值之一。
| 值 | Description |
|---|---|
1 |
一次 |
2 |
第二個 |
4 (預設) |
Minute |
8 |
Hour |
[ @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 P.M。
[ @snapshot_job_name = ] N'snapshot_job_name'
如果使用現有的作業,則為現有 快照集代理程式 作業名稱。
@snapshot_job_name 為 nvarchar(100),預設值為 NULL。 此參數供內部使用,在建立新發行集時不應指定。 如果 指定@snapshot_agent_name ,則 @job_login 和 @job_password 必須是 NULL。
[ @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 開始。 -
5指定使用 Microsoft Entra 管理身份驗證,使用發行商的預設管理身份。 此值僅適用於交易複寫,當 Publisher 與 Distributor 皆為 Azure SQL 受控執行個體 資源時。
[ @publisher_login = ] N'publisher_login'
連接到發行者時所使用的登入。
@publisher_login 為 sysname,預設為 NULL。
當@publisher_security_mode為 0時,必須指定@publisher_login。 如果 @publisher_login 為 且 NULL 為 1,則連接到發行者時會使用 @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 為 sysname,預設為 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
權限
只有系統管理員固定伺服器角色或db_owner固定資料庫角色的成員才能執行 sp_addpublication_snapshot。