Posts

Showing posts from October, 2022

Skip SQL job execution on last day OR specific day of the month.

A few days before I got the requirement to schedule a job in a way that it should not run on the last day of the month. and as we know, in SQL job schedule we do not have any option to skip the job execution on a specific day.  But, we could able to achieve this using T-SQL. Below is the SQL which i have used to fulfill the requirement.  USE MSDB DECLARE @date as datetime; SET @date=(select convert(date,dateadd(s,-1,dateadd(mm,datediff(m,0,getdate())+1,0)))) -- Getting last day of the month IF(dateadd(dd,0,DATEDIFF(dd,0,GETDATE()))=@date)  -- Comparing last day of the month with current date. begin exec sp_stop_job @job_name='<Job_Name>'   -- If condition satisfied then, job execution will stop end else begin exec sp_update_jobstep @job_name='<Job_Name>',@step_id=2  -- If condition is not satisfied then, job execution will proceed further. end We can add the above query as the first step in SQL job or a separate job can be created which would ca...

Perform Automatic Switchover in Log Shipping

Image
 DR drill is one of the major activities of every DBA’s life. And if log shipping is in the picture, then we have to follow a long checklist. Many things we have to check like sync status, disable and top LS jobs on primary and secondary, get tail log backup, copy and restore the same, etc.  In such drills, achieving RTO and RPO is very important. Most of us might have come across the situation due to a small miss switch over or switch back got delayed. What if we could automate switchover and switch back activity in log shipping. If this could happen then we can easily achieve the RPO and RTO also human error can be avoided. I have tried to automate this process. I’m not too good in script writing. But I tried my best to complete this. I have written the scripts which perform the switchover without manual intervention. We just need to provide the database name for which switchover needs to be performed and then simply execute the script. There are two scripts written....