Home > Microsoft .Net Development Tips > Microsoft SQL Server > Scheduling jobs to run daily, with exceptions
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

MICROSOFT SQL SERVER

Scheduling jobs to run daily, with exceptions


Luigi Visintin
05.01.2002
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Here is a very simple way to schedule a job to run daily (or any other time interval) and not run on a specific day (a local holiday, etc).

The key here is to exploit the 'go to step' feature combined with the 'raiserror' stmt. The following SQL job will run its second step any day as scheduled, except on April 25th.

---
-- Script generated on 4/30/2002 3:09 PM
-- By: PCVISINTINAdministrator
-- Server: PCVISINTINPCVISINTINPROD

BEGIN TRANSACTION            
  DECLARE @JobID BINARY(16)  
  DECLARE @ReturnCode INT    
  SELECT @ReturnCode = 0     

IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'[Uncategorized (Local)]') < 1 
  EXECUTE msdb.dbo.sp_add_category 
          @name = N'[Uncategorized (Local)]'

  -- Delete the job with the same name (if it exists)
  SELECT @JobID = job_id     
  FROM   msdb.dbo.sysjobs    
  WHERE (name = N'Runs daily except single holiday on apr 25th')       
  IF (@JobID IS NOT NULL)    
  BEGIN  

  -- Check if the job is a multi-server job  
  IF (EXISTS (SELECT  * 
              FROM    msdb.dbo.sysjobservers 
              WHERE   (job_id = @JobID) AND (server_id <> 0))) 
  BEGIN 

    -- There is, so abort the script 
    RAISERROR (N'Unable to import job ''Runs daily except single holiday on apr 25th'' since there is already a multi-server job with this name.', 16, 1) 
    GOTO QuitWithRollback  
  END 
  ELSE 

    -- Delete the [local] job 
    EXECUTE msdb.dbo.sp_delete_job 
            @job_name = N'Runs daily except single holiday on apr 25th' 
    SELECT @JobID = NULL
  END 

BEGIN 

  -- Add the job
  EXECUTE @ReturnCode = msdb.dbo.sp_add_job 
          @job_id = @JobID OUTPUT , 
          @job_name = N'Runs daily except single holiday on apr 25th',
          @owner_login_name = N'PCVISINTINAdministrator', 
          @description = N'No description available.', 
          @category_name = N'[Uncategorized (Local)]', 
          @enabled = 1, 
          @notify_level_email = 0, 
          @notify_level_page = 0, 
          @notify_level_netsend = 0, 
          @notify_level_eventlog = 2, 
          @delete_level= 0
  IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback 

  -- Add the job steps
  EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep 
          @job_id = @JobID, 
          @step_id = 1, 
          @step_name = N'CheckHoliday', 
          @command = N'IF month(getdate())=4 and day(getdate())=25
 begin
    raiserror(''holiday'',16, 1)
    end
         ', @database_name = N'master', 
         @server = N'', 
         @database_user_name = N'', 
         @subsystem = N'TSQL', 
         @cmdexec_success_code = 0, 
         @flags = 0, 
         @retry_attempts = 0, 
         @retry_interval = 1, 
         @output_file_name = N'', 
         @on_success_step_id = 0, 
         @on_success_action = 3, 
         @on_fail_step_id = 3, 
         @on_fail_action = 4
  IF (@@ERROR <>  0 OR @ReturnCode <>  0) GOTO QuitWithRollback 

  EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep 
          @job_id = @JobID, 
          @step_id = 2, 
          @step_name = N'Execute always but apr 25th',  
          @command = N'select getdate(),''not holiday''', 
          @database_name = N'master', 
          @server = N'', 
          @database_user_name = N'', 
          @subsystem = N'TSQL', 
          @cmdexec_success_code = 0, 
          @flags = 0, 
          @retry_attempts = 0, 
          @retry_interval = 1, 
          @output_file_name = N'', 
          @on_success_step_id = 0, 
          @on_success_action = 3, 
          @on_fail_step_id = 0, 
          @on_fail_action = 3
  IF (@@ERROR <>  0 OR @ReturnCode <>  0) GOTO QuitWithRollback 

  EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep 
          @job_id = @JobID, 
          @step_id = 3, 
          @step_name = N'EndOfJob', 
          @command = N'select ''finished''', 
          @database_name = N'master', 
          @server = N'', 
          @database_user_name = N'', 
          @subsystem = N'TSQL', 
          @cmdexec_success_code = 0, 
          @flags = 0, 
          @retry_attempts = 0, 
          @retry_interval = 1, 
          @output_file_name = N'', 
          @on_success_step_id = 0, 
          @on_success_action = 1, 
          @on_fail_step_id = 0, 
          @on_fail_action = 2
  IF (@@ERROR <>  0 OR @ReturnCode <>  0) GOTO QuitWithRollback 
  EXECUTE @ReturnCode = msdb.dbo.sp_update_job 
          @job_id = @JobID, 
          @start_step_id = 1 

  IF (@@ERROR <>  0 OR @ReturnCode <>  0) GOTO QuitWithRollback 

  -- Add the Target Servers
  EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver 
          @job_id = @JobID, 
          @server_name = N'(local)' 
  IF (@@ERROR <>  0 OR @ReturnCode <>  0) GOTO QuitWithRollback 

END
COMMIT TRANSACTION          
GOTO   EndSave              
QuitWithRollback:
  IF (@@TRANCOUNT >  0) ROLLBACK TRANSACTION 
EndSave: 

For More Information

  • What do you think about this tip? E-mail the Editor at tdichiara@techtarget.com with your feedback.
  • The Best SQL Server Web Links: tips, tutorials, scripts, and more.
  • Have an SQL Server tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
  • Ask your technical SQL Server questions--or help out your peers by answering them--in our live discussion forums.
  • Check out our Ask the Experts feature: Our SQL, Database Design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SQL Server and .NET development
Microsoft releases new CTP of Oslo SDK
Perpetuum unveils database synchronizer for .NET 2.0
The CTE, the hierarchical query and SQL Server 2005
SQL Server 2005 recursive functions and the CTE
SQL Examiner Suite synchronizes data schema, sets
Book excerpt: ADO.NET and SQL Server 2005
DataDirect database drivers now support MySQL
Top .NET tips of 2007 (so far)
Addressing common SQL Server questions
Book Excerpt: The .NET Framework and SQL Server 2005

Microsoft SQL Server
Using the Visual Studio 2005 DataSet Designer to build a data access layer
The CTE, the hierarchical query and SQL Server 2005
SQL Server 2005 recursive functions and the CTE
Choose the right .NET data provider, optimize application performance
The fallacy of the data layer -- or, a new architectural model for software designs
Book excerpt: ADO.NET and SQL Server 2005
Addressing common SQL Server questions
Book Excerpt: The .NET Framework and SQL Server 2005
WinForms development using SQL Server 2005 and Visual Basic 2005
Configuring ASP.NET 2.0 apps to SQL Server 2005 databases

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts