Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to find the snapshot job for my replication publisher

Is there a way, via script, to see what the name of the job is that will fire the snapshot for my replication?

This is for my Development environment. I am rebuilding my databases automatically via a visual studio database project. I have my replication setup automated, but I still have to fire the snapshot job manually in the morning.

I would like to be able to call:

sp_start_job 'JobThatWillTakeTheSnapShotForMyPublisher'

The problem is that the name of the job changes after every time I run my auto deploy (it adds a number to the end of the job name.)

If there is no way to find this out, is there a way to deterministically set the name of the job myself?

like image 669
Vaccano Avatar asked Dec 09 '25 14:12

Vaccano


2 Answers

You can find snapshot jobid in replication tables syspublications for transactional replication and sysmergepublications for merge replication, for example:

declare @jobId uniqueidentifier

use <MyPublicationDB>
set @jobId = (
    select snapshot_jobid from syspublications 
    where name = '<MyPublication>')

select @jobId
like image 149
agdk26 Avatar answered Dec 12 '25 05:12

agdk26


This is what I came up with:

declare @jobId uniqueidentifier
declare @jobName sysname

select  @jobId = jobs.job_id, @jobName = jobs.name
from    msdb.dbo.sysjobs jobs (nolock)
        join msdb.dbo.syscategories categories (nolock)
          on jobs.category_id = categories.category_id
where   categories.name = 'REPL-Snapshot'
        and jobs.name like '%MyPublisherNameHere%'

select @jobId, @jobName   

exec sp_start_job @job_id = @jobId  
like image 36
Vaccano Avatar answered Dec 12 '25 05:12

Vaccano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!