sys.sp_adddynamicsnapshot_job (Transact-SQL)

Van toepassing op:SQL ServerAzure SQL Managed Instance

Maakt een agentjob aan die een gefilterde datasnapshot genereert voor een publicatie met geparametriseerde rijfilters. Deze opgeslagen procedure wordt uitgevoerd bij Publisher in de publicatiedatabase. Deze opgeslagen procedure wordt door een beheerder gebruikt om handmatig gefilterde datasnapshot-taken voor abonnees te maken.

Note

Om een gefilterde datasnapshot-taak te maken, moet er al een standaard snapshot-taak voor de publicatie bestaan.

Voor meer informatie, zie Create a Snapshot for a Merge Publication with Parameterized Filters.

Transact-SQL syntaxis-conventies

Syntax

sys.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 ]
[ ; ]

Arguments

[ @publication = ] N'publicatie'

De naam van de publicatie waaraan de gefilterde datasnapshot-taak wordt toegevoegd. @publication is sysname, zonder standaard.

[ @suser_sname = ] N'suser_sname'

De waarde die wordt gebruikt bij het maken van een gefilterde datasnapshot voor een abonnement, wordt gefilterd door de waarde van de SUSER_SNAME functie bij de abonnee. @suser_sname is een systeemnaam, met als standaard .NULL @suser_sname zou moeten zijn NULL als deze functie niet wordt gebruikt om de publicatie dynamisch te filteren.

[ @host_name = ] N'host_name'

De waarde die wordt gebruikt bij het maken van een gefilterde datasnapshot voor een abonnement, die wordt gefilterd door de waarde van de HOST_NAME functie bij de abonnee. @host_name is een systeemnaam, met als standaard NULL. @host_name zou moeten zijn NULL als deze functie niet wordt gebruikt om de publicatie dynamisch te filteren.

[ @dynamic_snapshot_jobname = ] N'dynamic_snapshot_jobname' OUTPUT

De naam van de gefilterde datasnapshot-taak die is aangemaakt. @dynamic_snapshot_jobname is een OUTPUT parameter van het type sysname. Indien gespecificeerd, moet @dynamic_snapshot_jobname zich betrekken voor een unieke functie bij de distributeur. Indien niet gespecificeerd, wordt een taaknaam automatisch gegenereerd in de resultaatset, waarbij de naam als volgt wordt aangemaakt:

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

Note

Bij het genereren van de naam van de dynamische snapshot-job kun je de naam van de standaard snapshot-job afkorten.

[ @dynamic_snapshot_jobid = ] 'dynamic_snapshot_jobid' OUTPUT

Een identifier voor de gefilterde datasnapshot-taak die is aangemaakt. @dynamic_snapshot_jobid is een OUTPUT parameter van type uniqueidentifier, met een standaard van NULL.

[ @frequency_type = ] frequency_type

Specificeert de frequentie waarmee de gefilterde datasnapshot-taak wordt gepland. @frequency_type is int, en kan een van deze waarden zijn.

Value Description
1 Eenmaal
2 (standaard) Op aanvraag
4 Dagelijks
8 Wekelijks
16 Maandelijks
32 Maandelijkse verwant
64 Autostart
128 Terugkerend

[ @frequency_interval = ] frequency_interval

De periode, gemeten in dagen, waarin de gefilterde datasnapshot-taak wordt uitgevoerd. @frequency_interval is int, en hangt af van de waarde van @frequency_type.

Waarde van @frequency_type Effect op @frequency_interval
1 (standaard) @frequency_interval wordt niet gebruikt.
4 Elke @frequency_interval dagen.
8 @frequency_interval is een of meer van de volgende (gecombineerd met een | (Bitwise OK) Logische operator):

1 = Zondag
2 = Maandag
4 = Dinsdag
8 = Woensdag
16 = Donderdag
32 = Vrijdag
64 = Zaterdag
16 Op de @frequency_interval dag van de maand.
32 @frequency_interval is een van de volgende opties:

1 = Zondag
2 = Maandag
3 = Dinsdag
4 = Woensdag
5 = Donderdag
6 = Vrijdag
7 = Zaterdag
8 = Dag
9 = Doordeweekse dag
10 = Weekenddag
64 @frequency_interval wordt niet gebruikt.
128 @frequency_interval wordt niet gebruikt.

[ @frequency_subday = ] frequency_subday

Specificeert de eenheden voor @frequency_subday_interval. @frequency_subday is int, en kan een van deze waarden zijn.

Value Description
1 (standaard) Eenmaal
2 Second
4 Minuut
8 Uur

[ @frequency_subday_interval = ] frequency_subday_interval

Het aantal @frequency_subday periodes dat tussen elke uitvoering van de taak verloopt. @frequency_subday_interval is int, met als standaard .1

[ @frequency_relative_interval = ] frequency_relative_interval

Het voorkomen van de gefilterde datasnapshot-taak in elke maand. Deze parameter wordt gebruikt wanneer @frequency_type is ingesteld op 32 (maandelijkse relatieve). @frequency_relative_interval is int, en kan een van deze waarden zijn.

Value Description
1 (standaard) First
2 Second
4 Third
8 Fourth
16 Laatste

[ @frequency_recurrence_factor = ] frequency_recurrence_factor

De door @frequency_type gebruikte recurrence factor @frequency_recurrence_factor is int, met als standaard .1

[ @active_start_date = ] active_start_date

De datum waarop de gefilterde datasnapshot-taak voor het eerst wordt gepland, geformatteerd als yyyyMMdd. @active_start_date is int, met een standaard van 0.

[ @active_end_date = ] active_end_date

De datum waarop de gefilterde datasnapshot-taak stopt met worden gepland, geformatteerd als yyyyMMdd. @active_end_date is int, met als standaard .0

[ @active_start_time_of_day = ] active_start_time_of_day

Het tijdstip waarop de gefilterde datasnapshot-taak voor het eerst wordt gepland, geformatteerd als HHmmss. @active_start_time_of_day is int, met als standaard .0

[ @active_end_time_of_day = ] active_end_time_of_day

Het tijdstip waarop de gefilterde datasnapshot-taak stopt met worden gepland, geformatteerd als HHmmss. @active_end_time_of_day is int, met als standaard .0

Resultaatset

Kolomnaam Gegevenstype Description
id int Identificeert de gefilterde datasnapshot-taak in de MSdynamicsnapshotjobs-systeemtabel .
dynamic_snapshot_jobname sysname Naam van de gefilterde datasnapshot-taak.
dynamic_snapshot_jobid uniqueidentifier Identificeert uniek de taak van de SQL Server Agent bij de distributeur.

Codewaarden retourneren

0 (geslaagd) of 1 (mislukt).

Remarks

sp_adddynamicsnapshot_job wordt gebruikt bij merge-replicatie voor publicaties die een geparametriseerd filter gebruiken.

Examples

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

Permissions

Alleen leden van de sysadmin-vaste serverrol of de db_owner vaste databaserol kunnen uitvoeren sp_adddynamicsnapshot_job.