Changing the schedule of the Snapshot agent

I have transactional replication working pretty well.  I wrote this VB utility that allows the users to manage some of the publication articles.  Once again working pretty well.  There is one piece I can't seem to find any help on.  

After a new article is added via the utility, I want to change the schedule detail of the snapshot agent to run at 1am of the next day.  If at all possible I would like to do this using the system stored procedures and not a DMO or an ActiveX object.  What do ya think, is it possible?

Steve
LVL 4
Steve SiricaSenior Web Application EngineerAsked:
Who is Participating?
 
bsnyderCommented:
Here is part of the script I used in a cursor to stagger the time these jobs run.
You will probably want to get rid of the cursor, and add code to find just one database's replication snapshot job.

DECLARE @snap_name NVARCHAR(100),
        @failures  SMALLINT,
        @goodretry SMALLINT,
        @goodsched SMALLINT,
        @StartTime INT,
        @cr CURSOR
SET @failures  = 0
SET @goodretry = 0
SET @goodsched = 0
SET @StartTime = 200100   -- HHMMSS 8:01 pm

SET @cr = CURSOR FAST_FORWARD LOCAL FOR
     SELECT DISTINCT name
     FROM   msdb.dbo.sysjobs WHERE category_id = 15 ORDER BY name
OPEN @cr
FETCH NEXT FROM @cr INTO @snap_name
WHILE @@FETCH_STATUS = 0
  BEGIN
     DECLARE @ReturnCode INT
     SELECT  @ReturnCode = 0


     -- change job schedule
     EXECUTE @ReturnCode = msdb.dbo.sp_update_jobschedule @job_name = @snap_name,
                           @name = N'Replication Agent Schedule.',
                           @enabled = 1,
                           @freq_type = 4,
                           @active_start_time = @StartTime, -- was 70000 for 7:00 am,
                           @freq_interval = 1,
                           @freq_subday_type = 1,       -- was 8 for hours
                           @freq_subday_interval = NULL, -- was 12 Number of hours between runs
                           @freq_relative_interval = 1,
                           @freq_recurrence_factor = 0,
                           @active_end_date = 99991231,
                           @active_end_time = 220000
     IF (@@ERROR <> 0 OR @ReturnCode <> 0)
          SET @failures = @failures + 1
     ELSE
          SET @goodsched = @goodsched + 1
     SET @StartTime = @StartTime + 100 -- add 1 minutes
     FETCH NEXT FROM @cr INTO @snap_name
  END
CLOSE @cr
DEALLOCATE @cr

0
 
spcmnspffCommented:
The snap shot agent is manifested as scheduled job on the server, so look at sp_Update_JobSchedule in the BOL. This should give you all the resources necessary to change the schedule.  Actually I would use sp_Add_JobSchedule to add a second schedule to the job to run once at 1 AM on the next day and then enable it.  

You'll need to get the name or id of the job by looking through the jobs in Enterprise Manager once replication has been set up. And possibly the schedule ID - depending on how you want to implement this.  Sounds like fun, let me know how this turns out . . .


0
 
Steve SiricaSenior Web Application EngineerAuthor Commented:
bsnyder,

Thanks, with a little tweaking I got that code to do exactly what I was looking to do.  

Thanks again,
Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.