sp_addpublication_snapshot (Transact-SQL)

更新: 2006 年 12 月 12 日

建立指定發行集的快照集代理程式。這個預存程序執行於發行集資料庫的發行者端。

ms174958.security(zh-tw,SQL.90).gif安全性注意事項:
當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_loginjob_password) 都會以純文字的方式傳給散發者。您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。如需詳細資訊,請參閱<加密 SQL Server 的連接>。

主題連結圖示Transact-SQL 語法慣例

語法

sp_addpublication_snapshot [ @publication= ] '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 = ] 'snapshot_agent_name' ]
    [ , [ @publisher_security_mode = ] publisher_security_mode ]
    [ , [ @publisher_login = ] 'publisher_login' ]
    [ , [ @publisher_password = ] 'publisher_password' ] 
    [ , [ @job_login = ] 'job_login' ]
    [ , [ @job_password = ] 'job_password' ]
    [ , [ @publisher = ] 'publisher' ]

引數

  • [ @publication=] 'publication'
    這是發行集的名稱。publicationsysname,沒有預設值。
  • [ @frequency_type=] frequency_type
    這是快照集代理程式的執行頻率。frequency_typeint,它可以是下列值之一。

    描述

    1

    一次。

    4 (預設值)

    每天。

    8

    每週。

    16

    每月。

    32

    每月,相對於頻率間隔。

    64

    當 SQL Server Agent 啟動時。

    128

    在電腦閒置之時執行

  • [ @frequency_interval=] frequency_interval
    這是 frequency_type 設定的頻率所套用的值。frequency_intervalint,而且可以是下列其中一個值。

    frequency_type 的值 frequency_interval 的作用

    1

    未使用 frequency_interval

    4 (預設值)

    每隔 frequency_interval 天,預設值是每日。

    8

    frequency_interval 是一或多個下列項目 (用 | (位元 OR) (Transact-SQL) 邏輯運算子組合起來):

    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_subdayint,它可以是下列值之一。

    描述

    1

    一次

    2

    4 (預設值)

    分鐘

    8

    小時

  • [ @frequency_subday_interval=] frequency_subday_interval
    這是 frequency_subday 的間隔。frequency_subday_intervalint,預設值是 5,表示間隔 5 分鐘。
  • [ @frequency_relative_interval=] frequency_relative_interval
    這是快照集代理程式的執行日期。frequency_relative_intervalint,預設值是 1。
  • [ @frequency_recurrence_factor=] frequency_recurrence_factor
    這是 frequency_type 所用的循環因數。frequency_recurrence_factorint,預設值是 0。
  • [ @active_start_date=] active_start_date
    這是第一次排程快照集代理程式的日期,格式為 YYYYMMDD。active_start_dateint,預設值是 0。
  • [ @active_end_date=] active_end_date
    這是排程停止快照集代理程式的日期,格式為 YYYYMMDD。active_end_dateint,預設值是 99991231,表示 9999 年 12 月 31 日。
  • [ @active_start_time_of_day=] active_start_time_of_day
    這是第一次排程快照集代理程式的當日時間,格式為 HHMMSS。active_start_time_of_dayint,預設值是 0。
  • [ @active_end_time_of_day=] active_end_time_of_day
    這是排程停止快照集代理程式的當日時間,格式為 HHMMSS。active_end_time_of_dayint,預設值是 235959,表示 24 小時制的下午 11:59:59。
  • [ @snapshot_job_name = ] 'snapshot_agent_name'
    這是在使用現有作業時,現有快照集代理程式作業的名稱。snapshot_agent_namenvarchar(100),預設值是 NULL。這個參數供內部使用,當建立新的發行集時,不應指定。如果指定 snapshot_agent_name 的話,job_loginjob_password 必須是 NULL。
  • [ @publisher_security_mode= ] publisher_security_mode
    這是連接到發行者時,代理程式所用的安全性模式。publisher_security_modesmallint,預設值是 1。0 指定 SQL Server 驗證,1 指定 Windows 驗證。非 SQL Server 發行者必須指定 0 值。
  • [ @publisher_login= ] 'publisher_login'
    這是連接到發行者時所用的登入。publisher_loginsysname,預設值是 NULL。當 publisher_security_mode0 時,必須指定 publisher_login。如果 publisher_login 是 NULL,publisher_security_mode1,當連接到發行者時,就會使用 job_login 所指定的 Windows 帳戶。
  • [ @publisher_password= ] 'publisher_password'
    這是連接到發行者時所用的密碼。publisher_passwordsysname,預設值是 NULL。

    ms174958.security(zh-tw,SQL.90).gif安全性注意事項:
    請勿將驗證資訊儲存在指令碼檔案中。您應該在執行階段提供登入名稱和密碼。
  • [ @job_login= ] 'job_login'
    這是執行代理程式之 Windows 帳戶的登入。job_loginnvarchar(257),預設值是 NULL。通往散發者的代理程式連接一律使用這個 Windows 帳戶。您必須在建立新的快照集代理程式作業時,提供這個參數。
  • [ @job_password= ] 'job_password'
    這是執行代理程式之 Windows 帳戶的密碼。job_passwordsysname,沒有預設值。您必須在建立新的快照集代理程式作業時,提供這個參數。

    ms174958.security(zh-tw,SQL.90).gif安全性注意事項:
    請勿將驗證資訊儲存在指令碼檔案中。您應該在執行階段提供登入名稱和密碼。
  • [ @publisher= ] 'publisher'
    指定非 SQL Server 發行者。publishersysname,預設值是 NULL。

    ms174958.note(zh-tw,SQL.90).gif附註:
    當在 SQL Server 發行者端建立快照集代理程式時,不應使用 publisher

傳回碼值

0 (成功) 或 1 (失敗)

備註

sp_addpublication_snapshot 用於快照式複寫、交易式複寫和合併式複寫中。

權限

只有系統管理員 (sysadmin) 固定伺服器角色和 db_owner 固定資料庫角色的成員,才能夠執行 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

請參閱

參考

sp_addpublication (Transact-SQL)
sp_changepublication_snapshot (Transact-SQL)
sp_startpublication_snapshot (Transact-SQL)
複寫預存程序 (Transact-SQL)

其他資源

How to: Create a Publication (Replication Transact-SQL Programming)
建立和套用快照集

說明及資訊

取得 SQL Server 2005 協助

變更歷程記錄

版本 歷程記錄

2006 年 12 月 12 日

更新的內容:
  • 更新有關 @frequency_type@frequency_interval 參數的資訊。