Custom SQL Agent job schedule to run on the 5th working day of each month

  • I have the need to send a suite of SSRS reports to a client on the 5th working day of the month. Is there a way of either:

    1) setting a subscription to run on this day for the reports

    2) set a sql agent job to run on this day

    I would ideally like to know both if possible. Is it possible to build custom schedules in sql agent? I have a table of dates and another of holiday dates which can be used to find the 5th working day of the month but I don't know how to apply it to a schedule.

  • You could do it with a SQL Agent Job pretty easly. Just setup a pre-populated list of the 5th business day each month. The run an IF statement like below.

    IF (select * from 5thdayofmonth) = getdate()

    BEGIN

    -- Run report logic here.

    END

    Don't know of a way to do this in SSRS Scheduling Manager.

    Fraggle

  • I have a need to pre-populate a list of the 5th business day each month. Any idea how to do that ? :hehe:

  • Agreed, you'll have to use a calendar table and a simple if statement to block / let the subscription run.

    Keep in mind that those jobs are rebuilt every time the subscription changes (this includes password update).

    So that solution can get annoying really fast if it's not properly scripted.

  • homebrew01 (8/2/2011)


    I have a need to pre-populate a list of the 5th business day each month. Any idea how to do that ? :hehe:

    WITH CTE ( dt, rn )

    As (

    SELECT

    dt

    , ROW_NUMBER() OVER ( ORDER BY dt ) AS rn

    FROM

    dbo.Calendar

    WHERE

    Y = YEAR(GETDATE())

    AND M = MONTH(GETDATE())

    AND IsBusDay = 1

    )

    SELECT

    'RUN SUBSCRIPTIONS'

    FROM

    CTE

    WHERE

    rn = 5

    AND dt = DATEADD(D , 0 , DATEDIFF(D , 0 , GETDATE()))

    IF @@ROWCOUNT = 1

    SELECT 'sp_startjob'

    ELSE

    SELECT 'NOTHING TO DO'

    Script to load my Calendar table.

    http://www.sqlservercentral.com/Forums/Attachment8839.aspx

  • Thank you for this script, it was one of the final pieces to my having to run a sub on the 3rd business day of every month.

    For anyone who is interested:

    Paul Turley has a blog that lists how to get the GUID associated with a individual sub in the SSRS Report Manager.

    https://sqlserverbiblog.wordpress.com/2010/03/11/manually-starting-a-report-subscription/#comment-1781

    Then you use the command:

    use msdb

    EXEC sp_start_job @job_name = 'XXXXXXX-8FA9-4DFD-8677-34D451A75C74'

    and the subscription fires at the time the script is completed, so it doesn't matter what the days that are in the sub are set for.

    Also, I used a Aux_Calendar table from:

    http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

    (no author listed, but he is one smart cookie)

    Basically with his number table and calendar with this CTE script and the GUID I can now send any report on any set of days that a manager can dream up.:-D

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply