sp_adddynamicsnapshot_job (Transact-SQL)

適用於:SQL ServerAzure SQL 受控執行個體

建立代理程式作業,以針對具有參數化數據列篩選的發行集產生篩選的數據快照集。 這個預存程式會在發行集資料庫的發行者端執行。 系統管理員會使用此預存程式,手動建立訂閱者篩選的數據快照集作業。

注意

若要建立篩選的數據快照集作業,發行集的標準快照集作業必須已經存在。

如需詳細資訊,請參閱 使用參數化篩選建立合併式發行集的快照集

Transact-SQL 語法慣例

語法

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

要加入篩選數據快照集作業的發行集名稱。 @publication為 sysname,沒有預設值。

[ @suser_sname = ] N'suser_sname'

為訂閱建立篩選的數據快照集時所使用的值,該訂閱是由訂閱者端SUSER_SNAME函式的值所篩選。 @suser_sname為 sysname,預設值為 NULL如果函式 不是用來動態篩選發行集,則@suser_sname應該是 NULL

[ @host_name = ] N'host_name'

為訂閱建立篩選的數據快照集時所使用的值,這個訂閱是由訂閱者端HOST_NAME函式的值所篩選。 @host_name為 sysname,預設值為 NULL如果函式不是用來動態篩選發行集, 則host_name應該是 NULL

[ @dynamic_snapshot_jobname = ] N'dynamic_snapshot_jobname' OUTPUT

所建立篩選數據快照集作業的名稱。 @dynamic_snapshot_jobname是 sysname 類型的 OUTPUT 參數。 如果指定, @dynamic_snapshot_jobname 必須解析為散發者端的唯一作業。 如果未指定,系統會自動在結果集中產生作業名稱,其中會建立名稱,如下所示:

'dyn_' + <name of the standard snapshot job> + <GUID>

注意

產生動態快照集作業的名稱時,您可能會截斷標準快照集作業的名稱。

[ @dynamic_snapshot_jobid = ] 'dynamic_snapshot_jobid' OUTPUT

所建立篩選數據快照集作業的標識碼。 @dynamic_snapshot_jobid是 uniqueidentifier 類型的 OUTPUT 參數,預設值為 NULL

[ @frequency_type = ] frequency_type

指定排程篩選數據快照集作業的頻率。 @frequency_type為 int,而且可以是下列其中一個值。

Description
1 一次性
2 (預設值) [視需要]
4 每日
8 每週
16 每月
32 每月相對
64 自動啟動
128 重複執行

[ @frequency_interval = ] frequency_interval

執行篩選的數據快照集作業時,以天數為單位的期間。 @frequency_interval 為 int,取決於@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 = Weekday
10 = 週末日
64 @frequency_interval未使用。
128 @frequency_interval未使用。

[ @frequency_subday = ] frequency_subday

指定@frequency_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,預設值為 1

[ @frequency_relative_interval = ] frequency_relative_interval

每個月篩選過的數據快照集作業出現次數。 當@frequency_type設定為 32 (每月相對) 時,會使用此參數。 @frequency_relative_interval為 int,而且可以是下列其中一個值。

Description
1 (預設值) First
2 Second
4 Third
8
16 姓氏

[ @frequency_recurrence_factor = ] frequency_recurrence_factor

frequency_type所使用的週期因數。 @frequency_recurrence_factor為 int,預設值為 1

[ @active_start_date = ] active_start_date

第一次排程篩選資料快照集作業的日期,格式為 yyyyMMdd@active_start_date為 int,預設值為 0

[ @active_end_date = ] active_end_date

篩選的數據快照集作業停止排程的日期,格式為 yyyyMMdd@active_end_date為 int,預設值為 0

[ @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,預設值為 0

結果集

資料行名稱 資料類型 描述
id int 識別 MSdynamicsnapshotjobs 系統數據表中篩選的數據快照集作業。
dynamic_snapshot_jobname sysname 篩選的數據快照集作業名稱。
dynamic_snapshot_jobid uniqueidentifier 可唯一識別散發者端的 SQL Server Agent 作業。

傳回碼值

0 (成功)或 1 (失敗)。

備註

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'AdventureWorks2022';
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 [AdventureWorks2022];

-- Enable AdventureWorks2022 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 AdventureWorks2022.', 
  @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].[BusinessEntityID] = [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

權限

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