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'

發行集的名稱。 @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是下列其中一或多個專案(結合 |(位 OR)邏輯運算子):

1 = 星期日 |

2 = 星期一 |

4 = 星期二 |

8 = 星期三 |

16 = 星期四 |

32 = 星期五 |

64 = 星期六
16 當月@frequency_interval 日。
32 @frequency_interval是下列其中一個值:

1 = 星期日 |

2 = 星期一 |

3 = 星期二 |

4 = 星期三 |

5 = 星期四 |

6 = 星期五 |

7 = 星期六 |

8 = Day |

9 = Weekday |

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

[ @frequency_subday = ] frequency_subday

freq_subday_interval的單位。 @frequency_subday為 int,而且可以是下列其中一個值。

Description
1 一次
2 Second
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。

[ @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 標識符 先前稱為 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_login 為 NULL@publisher_security_mode1,則連接到發行者時會使用 @job_login 中指定的帳戶。

[ @publisher_password = ] N'publisher_password'

連接到發行者時所使用的密碼。 @publisher_password為 sysname,預設值為 NULL

重要

請勿將驗證資訊儲存在腳本檔案中。 為了協助改善安全性,建議您在運行時間提供登入名稱和密碼。

[ @job_login = ] N'job_login'

代理程序執行所在的帳戶登入。 在 Azure SQL 受控執行個體 上,使用 SQL Server 帳戶。 @job_login為 nvarchar(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 用於快照式複寫、事務複製和合併式複寫。

範例

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