sp_adddynamicsnapshot_job (Transact-SQL)

更新: 2005 年 12 月 5 日

建立一項代理程式作業,利用參數化的資料列篩選,產生發行集篩選資料快照集。這個預存程序執行於發行集資料庫的發行者端。管理員利用這個預存程序,手動建立訂閱者的已篩選資料快照集作業。

ms190327.note(zh-tw,SQL.90).gif附註:
若要建立已篩選資料快照集,發行集的標準快照集作業必須已經存在。

如需詳細資訊,請參閱<含參數化篩選之合併式發行集的快照集>。

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

語法

sp_adddynamicsnapshot_job [ @publication = ] 'publication' 
    [ , [ @suser_sname = ] 'suser_sname' ] 
    [ , [ @host_name = ] 'host_name' ] 
    [ , [ @dynamic_snapshot_jobname = ] 'dynamic_snapshot_jobname' OUTPUT ] 
    [ , [ @dynamic_snapshot_jobid = ] 'dynamic_snapshot_jobid' OUTPUT ] 
    [ , [ @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 ]

引數

  • [ @publication=] 'publication'
    這是加入篩選資料快照集作業的發行集名稱。publicationsysname,沒有預設值。
  • [ @suser_sname= ] 'suser_sname'
    這是為訂閱建立已篩選資料快照集所用的值;該訂閱是由訂閱者的 SUSER_SNAME 函數值所篩選。suser_snamesysname,沒有預設值。如果不是利用這個函數來動態篩選發行集,suser_sname 就應該是 NULL。
  • [ @host_name= ] 'host_name'
    這是為訂閱建立已篩選資料快照集所用的值;該訂閱是由訂閱者的 HOST_NAME 函數值所篩選。host_namesysname,沒有預設值。如果不是利用這個函數來動態篩選發行集,host_name 就應該是 NULL。
  • [ @dynamic_snapshot_jobname= ] 'dynamic_snapshot_jobname'
    這是建立的已篩選資料快照集作業的名稱。dynamic_snapshot_jobnamesysname,預設值是 NULL,這是一個選擇性的 OUTPUT 參數。如果指定的話,dynamic_snapshot_jobname 必須解析為在散發者端的唯一作業。如果未指定,就會自動產生作業名稱,而且會在結果集中傳回作業名稱,名稱的建立方式如下:

    'dyn_' + <name of the standard snapshot job> + <GUID>
    
    ms190327.note(zh-tw,SQL.90).gif附註:
    產生動態快照集作業的名稱時,您可以截斷標準快照集作業的名稱。
  • [ @dynamic_snapshot_jobid= ] 'dynamic_snapshot_jobid'
    這是建立的已篩選資料快照集作業的識別碼。dynamic_snapshot_jobiduniqueidentifier,預設值是 NULL,這是一個選擇性的 OUTPUT 參數。
  • [ @frequency_type=] frequency_type
    這是已篩選資料快照集作業的排程頻率。frequency_typeint,它可以是下列值之一。

    描述

    1

    一次

    2

    視需要

    4 (預設值)

    每日

    8

    每週

    16

    每月

    32

    每月相對

    64

    自動啟動

    128

    重複執行

  • [ @frequency_interval = ] frequency_interval
    這是執行已篩選資料快照集作業的週期 (以天為單位)。frequency_intervalint,預設值是 1;它會隨著 frequency_type 的值而不同。

    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
    指定 frequency_subday_interval 的單位。frequency_subdayint,它可以是下列值之一。

    描述

    1

    一次

    2

    第二個

    4 (預設值)

    分鐘

    8

    小時

  • [ @frequency_subday_interval=] frequency_subday_interval
    這是在各次執行作業之間發生的 frequency_subday 週期數。frequency_subday_intervalint,預設值是 5。
  • [ @frequency_relative_interval=] frequency_relative_interval
    這是每個月已篩選資料快照集作業的出現頻率。當 frequency_type 設定為 32 (每月相對) 時,則使用這個參數。frequency_relative_intervalint,它可以是下列值之一。

    描述

    1 (預設值)

    第一個

    2

    第二個

    4

    第三個

    8

    第四個

    16

    最後一個

  • [ @frequency_recurrence_factor=] frequency_recurrence_factor
    這是 frequency_type 所用的循環因數。frequency_recurrence_factorint,預設值是 0。
  • [ @active_start_date=] active_start_date
    這是第一次排程已篩選資料快照集作業的日期,格式為 YYYYMMDD。active_start_dateint,預設值是 NULL。
  • [ @active_end_date=] active_end_date
    這是排程停止已篩選資料快照集作業的日期,格式為 YYYYMMDD。active_end_dateint,預設值是 NULL。
  • [ @active_start_time_of_day=] active_start_time_of_day
    這是第一次排程已篩選資料快照集作業的當日時間,格式為 HHMMSS。active_start_time_of_dayint,預設值是 NULL。
  • [ @active_end_time_of_day=] active_end_time_of_day
    這是排程停止已篩選資料快照集作業的當日時間,格式為 HHMMSS。active_end_time_of_dayint,預設值是 NULL。

傳回碼值

0 (成功) 或 1 (失敗)

結果集

資料行名稱 資料類型 描述

id

int

識別 MSdynamicsnapshotjobs 系統資料表中已篩選資料快照集作業。

dynamic_snapshot_jobname

sysname

已篩選資料快照集作業的名稱。

dynamic_snapshot_jobid

uniqueidentifier

可唯一識別散發者端的 Microsoft SQL Server Agent 作業。

備註

sp_adddynamicsnapshot_job 用於使用參數化篩選之發行集的合併式複寫中。

權限

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

範例

-- To avoid storing the login and password in the script file, the value 
-- is passed into SQLCMD as a scripting variable. 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".

--Add a new merge publication.
DECLARE @publicationdb AS sysname;
DECLARE @publication AS sysname;
DECLARE @table1 AS sysname;
DECLARE @table2 AS sysname;
DECLARE @filter AS sysname;
DECLARE @schema_hr AS sysname;
DECLARE @schema_sales AS sysname;

SET @publicationdb = N'AdventureWorks';
SET @publication = N'AdvWorksSalesPersonMerge';
SET @table1 = N'Employee';
SET @table2 = N'SalesPerson';
SET @filter = N'SalesPerson_Employee';
SET @schema_hr = N'HumanResources';
SET @schema_sales = N'Sales';

USE [AdventureWorks];

-- Enable AdventureWorks for merge replication.
EXEC sp_replicationdboption
  @dbname = @publicationdb,
  @optname = N'merge publish',
  @value = N'true';  

-- Create new merge publication.  
EXEC sp_addmergepublication 
  @publication = @publication, 
  @description = N'Merge publication of AdventureWorks.', 
  @allow_subscriber_initiated_snapshot = N'false';

-- Create a new snapshot job for the publication, using the 
-- default schedule. Pass credentials at runtime using 
-- sqlcmd scripting variables.
EXEC sp_addpublication_snapshot 
  @publication = @publication, 
  @job_login = $(Login), 
  @job_password = $(password);

-- Add an article for the Employee table, 
-- which is horizontally partitioned using 
-- a parameterized row filter.
EXEC sp_addmergearticle 
  @publication = @publication, 
  @article = @table1, 
  @source_owner = @schema_hr, 
  @source_object = @table1, 
  @type = N'table', 
  @description = 'contains employee information', 
  @subset_filterclause = N'[LoginID] = HOST_NAME()';

-- Add an article for the SalesPerson table, 
-- which is partitioned based on a join filter.
EXEC sp_addmergearticle 
  @publication = @publication, 
  @article = @table2, 
  @source_owner = @schema_sales, 
  @source_object = @table2, 
  @type = N'table', 
  @description = 'contains customer information';

-- Add a join filter between the two articles.
EXEC sp_addmergefilter 
  @publication = @publication, 
  @article = @table1, 
  @filtername = @filter, 
  @join_articlename = @table2, 
  @join_filterclause = N'[Employee].[EmployeeID] = [SalesPerson].[SalesPersonID]', 
  @join_unique_key = 1, 
  @filter_type = 1;
GO

-- Start the snapshot agent job.
DECLARE @publication AS sysname;
SET @publication = N'AdvWorksSalesPersonMerge';

EXEC sp_startpublication_snapshot 
  @publication = @publication;
GO

PRINT '*** Waiting for the initial snapshot.';
GO

-- Create a temporary table to store the filtered data snapshot 
-- job information.
CREATE TABLE #temp (id int,
    job_name sysname,
    job_id uniqueidentifier,
    dynamic_filter_login sysname NULL,
    dynamic_filter_hostname sysname NULL,
    dynamic_snapshot_location nvarchar(255),
    frequency_type int, 
    frequency_interval int, 
    frequency_subday_type int,
    frequency_subday_interval int, 
    frequency_relative_interval int, 
    frequency_recurrence_factor int, 
    active_start_date int, 
    active_end_date int, 
    active_start_time int, 
    active_end_time int
)

-- Create each snapshot for a partition 
-- The initial snapshot must already be generated.
DECLARE @publication AS sysname;
DECLARE @jobname AS sysname
DECLARE @hostname AS sysname
SET @publication = N'AdvWorksSalesPersonMerge';
SET @hostname = N'adventure-works\Fernando';

WHILE NOT EXISTS(SELECT * FROM sysmergepublications 
    WHERE [name] = @publication 
    AND snapshot_ready = 1)
BEGIN
    WAITFOR DELAY '00:00:05'
END

-- Create a data partition by overriding HOST_NAME().
EXEC sp_addmergepartition 
  @publication = @publication,
  @host_name = @hostname;

-- Create the filtered data snapshot job, and use the returned 
-- information to start the job.
EXEC sp_adddynamicsnapshot_job 
  @publication = @publication,
  @host_name = @hostname;

INSERT INTO #temp (id, job_name, job_id, dynamic_filter_login,
    dynamic_filter_hostname, dynamic_snapshot_location,
    frequency_type, frequency_interval, frequency_subday_type,
    frequency_subday_interval, frequency_relative_interval, 
    frequency_recurrence_factor, active_start_date, active_end_date, 
    active_start_time,active_end_time)
EXEC sp_helpdynamicsnapshot_job;

SELECT @jobname = (SELECT DISTINCT job_name FROM #temp WHERE dynamic_filter_hostname = @hostname);

EXEC msdb..sp_start_job @job_name = @jobname;
DROP TABLE #temp;
GO

請參閱

參考

sp_dropdynamicsnapshot_job (Transact-SQL)
sp_helpdynamicsnapshot_job (Transact-SQL)

其他資源

How to: Create a Snapshot for a Merge Publication with Parameterized Filters (Replication Transact-SQL Programming)
參數化資料列篩選

說明及資訊

取得 SQL Server 2005 協助

變更歷程記錄

版本 歷程記錄

2005 年 12 月 5 日

新增內容:
  • 新增結果集資訊。