Scheduled stop of maintenance task I need something to automatically stop my index maintenance job on Saturday night. How do I do that? How can I check automatically if my index maintenance job is still running and if it's running, stop it every Saturday night before 12 am? 

Right now I do it manually using:

USE msdb
GO
EXEC dbo.sp_stop_job
N'The name of the job'


How can I automate stopping the job at a certain time ?
Programmatically, you can use DATEName(DW, GETDATE()) to see if it is Saturday along with time and then have your sp_stop_job run to stop the job.

You can put that code in IF .. check saturday and time then run stop job code else exit gracefully.

It would be much better if you use Ola's Index maintenance solution (or Minnionware - I have not used it though, since I am using the former).

EXECUTE dbo.IndexOptimize @Databases = 'ALL_DATABASES', 
        @FragmentationLow = NULL,
        @FragmentationMedium = 'INDEX_REORGANIZE',
        @FragmentationHigh =  'INDEX_REORGANIZE',
        @FragmentationLevel1 = 5,
        @FragmentationLevel2 = 30,
        @UpdateStatistics = 'ALL',
        @OnlyModifiedStatistics = 'Y',
        @TimeLimit = 5400, -- 90 minutes, specified in seconds
        @LogToTable = 'Y',
        @Indexes = 'ALL_INDEXES',
        @SortInTempdb = 'Y'