sp_addpublication_snapshot (Transact-SQL)

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

安全性注意事項安全性注意事項

當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_login 和 job_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'
    這是發行集的名稱。publication 是 sysname,沒有預設值。

  • [ @frequency_type=] frequency_type
    這是快照集代理程式的執行頻率。frequency_type 是 int,它可以是下列值之一。

    描述

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

    9 = 工作日 |

    10 = 週末

    64

    frequency_interval 未使用。

    128

    frequency_interval 未使用。

  • [ @frequency_subday=] frequency_subday
    這是 freq_subday_interval 的單位。frequency_subday 是 int,它可以是下列值之一。

    描述

    1

    一次

    2

    4 (預設值)

    分鐘

    8

    小時

  • [ @frequency_subday_interval=] frequency_subday_interval
    這是 frequency_subday 的間隔。frequency_subday_interval 是 int,預設值是 5,表示間隔 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 = ] 'snapshot_agent_name'
    這是在使用現有作業時,現有快照集代理程式作業的名稱。snapshot_agent_name 是 nvarchar(100),預設值是 NULL。這個參數供內部使用,當建立新的發行集時,不應指定。如果指定 snapshot_agent_name 的話,job_login 和 job_password 必須是 NULL。

  • [ @publisher_security_mode= ] publisher_security_mode
    這是連接到發行者時,代理程式所用的安全性模式。publisher_security_mode 是 smallint,預設值是 1。0 指定 SQL Server 驗證,1 指定 Windows 驗證。非 SQL Server 發行者必須指定 0 值。盡可能使用 Windows 驗證。

  • [ @publisher_login= ] 'publisher_login'
    這是連接到發行者時所用的登入。publisher_login 是 sysname,預設值是 NULL。當 publisher_security_mode 是 0 時,必須指定 publisher_login。如果 publisher_login 是 NULL,publisher_security_mode 是 1,當連接到發行者時,就會使用 job_login 所指定的 Windows 帳戶。

  • [ @publisher_password= ] 'publisher_password'
    這是連接到發行者時所用的密碼。publisher_password 是 sysname,預設值是 NULL。

    安全性注意事項安全性注意事項

    請勿將驗證資訊儲存在指令碼檔案中。若要改善安全性,我們建議您在執行階段提供登入名稱和密碼。

  • [ @job_login= ] 'job_login'
    這是執行代理程式之 Windows 帳戶的登入。job_login 是 nvarchar(257),預設值是 NULL。通往散發者的代理程式連接一律使用這個 Windows 帳戶。您必須在建立新的快照集代理程式作業時,提供這個參數。

    [!附註]

    如果是非 SQL Server 發行者,這必須是 sp_adddistpublisher (Transact-SQL) 中所指定的相同登入。

  • [ @job_password= ] 'job_password'
    這是執行代理程式之 Windows 帳戶的密碼。job_password 是 sysname,沒有預設值。您必須在建立新的快照集代理程式作業時,提供這個參數。

    安全性注意事項安全性注意事項

    請勿將驗證資訊儲存在指令碼檔案中。若要改善安全性,我們建議您在執行階段提供登入名稱和密碼。

  • [ @publisher= ] 'publisher'
    指定非 SQL Server 發行者。publisher 是 sysname,預設值是 NULL。

    [!附註]

    當在 SQL Server 發行者端建立快照集代理程式時,不應使用 publisher。

傳回碼值

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'AdventureWorks2008R2'; 
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

權限

只有系統管理員 (sysadmin) 固定伺服器角色和 db_owner 固定資料庫角色的成員,才能夠執行 sp_addpublication_snapshot