Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Programmatically Execute Maintenance Plan

Is there any way to programmatically execute (start) a SQL Server maintenance plan? We have a windows service that runs nightly and updates the DB with lots of data, once that is finished we would like to trigger a maintenance plan in the DB to start running.

like image 374
Hungry Beast Avatar asked Jun 15 '11 15:06

Hungry Beast


2 Answers

You can start a job which is part of your maintenance plan by sp_start_job:

use msdb; 
go
exec dbo.sp_start_job N'job_name' ;
go
like image 67
a1ex07 Avatar answered Oct 04 '22 21:10

a1ex07


If you get the error "Could not find stored procedure 'dbo.sp_start_job'." try this one:

execute msdb.dbo.sp_maintplan_start @plan_id = N'549EDF1B-5712-472E-9722-DD81F622A3C2'

You get the guid from this query:

SELECT s.id AS [ID]    
FROM msdb.dbo.sysmaintplan_plans AS s    
WHERE s.name=N'MyMaintenancePlan'
like image 44
DShook Avatar answered Oct 04 '22 22:10

DShook