• ken.trock (4/25/2013)


    We have data retention jobs that hang sometimes and decided to do something similar; automatically kill them at a certain point.

    SELECT * INTO #JobInfo

    FROM OPENROWSET('sqloledb', 'server=(local);trusted_connection=yes'

    , 'set fmtonly off exec msdb.dbo.sp_help_job')

    --Don't let this accidently run between 7pm and 4am, legitimate times for data retention

    IF DATEPART(hh, GETDATE()) >= 19 OR DATEPART(hh, GETDATE()) <= 4

    RETURN

    DECLARE cRetentionJobs CURSOR FOR

    select name

    from #JobInfo

    where current_execution_status <> 4 --anything not idle

    AND name LIKE '%DataRetention%' AND [enabled] = 1

    We'll run thru this cursor and exec msdb..sp_stop_job for each.

    But we have the convenience knowing that there's only one schedule for each of these jobs.

    Thanks for the article.

    Ken

    It is a lot easier when there's only one scheduled time per job. Glad you liked it.

    --------------------------------------
    When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
    --------------------------------------
    It’s unpleasantly like being drunk.
    What’s so unpleasant about being drunk?
    You ask a glass of water. -- Douglas Adams