How to stop a job for the rest of the day after the relevant condition has been satisfied once?
I have a job that runs every 5 minutes to check an IF condition. If the condition is true, it will send an email, if not, it won’t do anything.
My problem is that once the IF condition is true, it will remain true for the whole day and, as the job is running after every 5 minutes, it will keep sending the email after every 5 minutes.
I need to stop the job for the whole day once the email has been sent once. Is there a way to do so?
sql-server t-sql sql-server-agent jobs
New contributor
add a comment |
I have a job that runs every 5 minutes to check an IF condition. If the condition is true, it will send an email, if not, it won’t do anything.
My problem is that once the IF condition is true, it will remain true for the whole day and, as the job is running after every 5 minutes, it will keep sending the email after every 5 minutes.
I need to stop the job for the whole day once the email has been sent once. Is there a way to do so?
sql-server t-sql sql-server-agent jobs
New contributor
add a comment |
I have a job that runs every 5 minutes to check an IF condition. If the condition is true, it will send an email, if not, it won’t do anything.
My problem is that once the IF condition is true, it will remain true for the whole day and, as the job is running after every 5 minutes, it will keep sending the email after every 5 minutes.
I need to stop the job for the whole day once the email has been sent once. Is there a way to do so?
sql-server t-sql sql-server-agent jobs
New contributor
I have a job that runs every 5 minutes to check an IF condition. If the condition is true, it will send an email, if not, it won’t do anything.
My problem is that once the IF condition is true, it will remain true for the whole day and, as the job is running after every 5 minutes, it will keep sending the email after every 5 minutes.
I need to stop the job for the whole day once the email has been sent once. Is there a way to do so?
sql-server t-sql sql-server-agent jobs
sql-server t-sql sql-server-agent jobs
New contributor
New contributor
edited 9 hours ago
Tony Hinkle
2,0781422
2,0781422
New contributor
asked 9 hours ago
Hitesh KhandelwalHitesh Khandelwal
162
162
New contributor
New contributor
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Look at the job schedule to get the ID of the schedule--I'll use schedule ID 51 in the following examples. You can disable the schedule on the job when the condition is first found to be true:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=0
GO
Then set up another job to run at the beginning of the day (or whenever the condition will be false again) to enable it:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=1
GO
One thing to note--a schedule can be used on multiple jobs, so you just need to ensure that schedule is not also being used for other jobs. So to check if schedule ID 51 is being used by multiple jobs:
USE msdb
GO
SELECT * FROM sysjobschedules WHERE schedule_id = 51
A newbie question:@schedule_id is the id of my job or it should remain 51 only?
– Hitesh Khandelwal
9 hours ago
Go to the schedules tab of the job and you'll see the IDs in the first column.
– Tony Hinkle
9 hours ago
Okay. Thank you so much.
– Hitesh Khandelwal
9 hours ago
add a comment |
Another option would be to create a small table somewhere that has a single datetime flag column. When your condition evaluates to TRUE then you update that value to the current datetime.
You will also need to add the condition to your IF statement that CAST(DatetimeFLAG AS DATE) must be < CAST(GETDATE() AS DATE). This basically stops it from evaluating to TRUE again on the same date.
This option will work if you don't want to mess with schedules.
New contributor
add a comment |
If your job executes a stored procedure, you can check the job history at the beginning of the SP and return if the job has been successfully executed today.
IF EXISTS(SELECT 1
FROM
msdb.dbo.sysjobs as job
JOIN
msdb.dbo.sysjobhistory his
ON job.job_id = his.job_id
WHERE
job.name = 'your job name'
AND CAST(CAST(his.run_date AS char(8)) as date) = CAST(GETDATE() as date)
AND his.run_status = 1)
BEGIN
RETURN;
END
add a comment |
I actually wrote a similar tip at mssqltips.com.
For your current case, the general idea would be like this:
you may add an additional step at the end of the job, once your primary job steps succeed, the job will run the final step (i.e. the newly added step)
If your job fails in your primary steps, your job will just exit and no need to run the final added step.
In you final job step, you will do one thing, i.e. updating your job schedule' start date to be next day.
use msdb
declare @active_date int, @sch_name varchar(128);
select @sch_name=s.name
from msdb.dbo.sysschedules s
inner join msdb.dbo.sysjobschedules js
on js.schedule_id = s.schedule_id
and js.job_id = $(ESCAPE_NONE(JOBID));
set @active_date = cast(replace(cast(cast(getdate()+1 as date) as varchar(10)), '-', '') as int);
exec sp_update_schedule @name=@sch_name, @active_start_date= @active_date;
Of course, I assume your job has one dedicated schedule only.
The approach is all self-dependent and you do not need any additional external jobs to manage your current job.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Hitesh Khandelwal is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229664%2fhow-to-stop-a-job-for-the-rest-of-the-day-after-the-relevant-condition-has-been%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Look at the job schedule to get the ID of the schedule--I'll use schedule ID 51 in the following examples. You can disable the schedule on the job when the condition is first found to be true:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=0
GO
Then set up another job to run at the beginning of the day (or whenever the condition will be false again) to enable it:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=1
GO
One thing to note--a schedule can be used on multiple jobs, so you just need to ensure that schedule is not also being used for other jobs. So to check if schedule ID 51 is being used by multiple jobs:
USE msdb
GO
SELECT * FROM sysjobschedules WHERE schedule_id = 51
A newbie question:@schedule_id is the id of my job or it should remain 51 only?
– Hitesh Khandelwal
9 hours ago
Go to the schedules tab of the job and you'll see the IDs in the first column.
– Tony Hinkle
9 hours ago
Okay. Thank you so much.
– Hitesh Khandelwal
9 hours ago
add a comment |
Look at the job schedule to get the ID of the schedule--I'll use schedule ID 51 in the following examples. You can disable the schedule on the job when the condition is first found to be true:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=0
GO
Then set up another job to run at the beginning of the day (or whenever the condition will be false again) to enable it:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=1
GO
One thing to note--a schedule can be used on multiple jobs, so you just need to ensure that schedule is not also being used for other jobs. So to check if schedule ID 51 is being used by multiple jobs:
USE msdb
GO
SELECT * FROM sysjobschedules WHERE schedule_id = 51
A newbie question:@schedule_id is the id of my job or it should remain 51 only?
– Hitesh Khandelwal
9 hours ago
Go to the schedules tab of the job and you'll see the IDs in the first column.
– Tony Hinkle
9 hours ago
Okay. Thank you so much.
– Hitesh Khandelwal
9 hours ago
add a comment |
Look at the job schedule to get the ID of the schedule--I'll use schedule ID 51 in the following examples. You can disable the schedule on the job when the condition is first found to be true:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=0
GO
Then set up another job to run at the beginning of the day (or whenever the condition will be false again) to enable it:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=1
GO
One thing to note--a schedule can be used on multiple jobs, so you just need to ensure that schedule is not also being used for other jobs. So to check if schedule ID 51 is being used by multiple jobs:
USE msdb
GO
SELECT * FROM sysjobschedules WHERE schedule_id = 51
Look at the job schedule to get the ID of the schedule--I'll use schedule ID 51 in the following examples. You can disable the schedule on the job when the condition is first found to be true:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=0
GO
Then set up another job to run at the beginning of the day (or whenever the condition will be false again) to enable it:
USE [msdb]
GO
EXEC msdb.dbo.sp_update_schedule @schedule_id=51,
@enabled=1
GO
One thing to note--a schedule can be used on multiple jobs, so you just need to ensure that schedule is not also being used for other jobs. So to check if schedule ID 51 is being used by multiple jobs:
USE msdb
GO
SELECT * FROM sysjobschedules WHERE schedule_id = 51
edited 9 hours ago
answered 9 hours ago
Tony HinkleTony Hinkle
2,0781422
2,0781422
A newbie question:@schedule_id is the id of my job or it should remain 51 only?
– Hitesh Khandelwal
9 hours ago
Go to the schedules tab of the job and you'll see the IDs in the first column.
– Tony Hinkle
9 hours ago
Okay. Thank you so much.
– Hitesh Khandelwal
9 hours ago
add a comment |
A newbie question:@schedule_id is the id of my job or it should remain 51 only?
– Hitesh Khandelwal
9 hours ago
Go to the schedules tab of the job and you'll see the IDs in the first column.
– Tony Hinkle
9 hours ago
Okay. Thank you so much.
– Hitesh Khandelwal
9 hours ago
A newbie question:@schedule_id is the id of my job or it should remain 51 only?
– Hitesh Khandelwal
9 hours ago
A newbie question:@schedule_id is the id of my job or it should remain 51 only?
– Hitesh Khandelwal
9 hours ago
Go to the schedules tab of the job and you'll see the IDs in the first column.
– Tony Hinkle
9 hours ago
Go to the schedules tab of the job and you'll see the IDs in the first column.
– Tony Hinkle
9 hours ago
Okay. Thank you so much.
– Hitesh Khandelwal
9 hours ago
Okay. Thank you so much.
– Hitesh Khandelwal
9 hours ago
add a comment |
Another option would be to create a small table somewhere that has a single datetime flag column. When your condition evaluates to TRUE then you update that value to the current datetime.
You will also need to add the condition to your IF statement that CAST(DatetimeFLAG AS DATE) must be < CAST(GETDATE() AS DATE). This basically stops it from evaluating to TRUE again on the same date.
This option will work if you don't want to mess with schedules.
New contributor
add a comment |
Another option would be to create a small table somewhere that has a single datetime flag column. When your condition evaluates to TRUE then you update that value to the current datetime.
You will also need to add the condition to your IF statement that CAST(DatetimeFLAG AS DATE) must be < CAST(GETDATE() AS DATE). This basically stops it from evaluating to TRUE again on the same date.
This option will work if you don't want to mess with schedules.
New contributor
add a comment |
Another option would be to create a small table somewhere that has a single datetime flag column. When your condition evaluates to TRUE then you update that value to the current datetime.
You will also need to add the condition to your IF statement that CAST(DatetimeFLAG AS DATE) must be < CAST(GETDATE() AS DATE). This basically stops it from evaluating to TRUE again on the same date.
This option will work if you don't want to mess with schedules.
New contributor
Another option would be to create a small table somewhere that has a single datetime flag column. When your condition evaluates to TRUE then you update that value to the current datetime.
You will also need to add the condition to your IF statement that CAST(DatetimeFLAG AS DATE) must be < CAST(GETDATE() AS DATE). This basically stops it from evaluating to TRUE again on the same date.
This option will work if you don't want to mess with schedules.
New contributor
New contributor
answered 8 hours ago
BCMBCM
111
111
New contributor
New contributor
add a comment |
add a comment |
If your job executes a stored procedure, you can check the job history at the beginning of the SP and return if the job has been successfully executed today.
IF EXISTS(SELECT 1
FROM
msdb.dbo.sysjobs as job
JOIN
msdb.dbo.sysjobhistory his
ON job.job_id = his.job_id
WHERE
job.name = 'your job name'
AND CAST(CAST(his.run_date AS char(8)) as date) = CAST(GETDATE() as date)
AND his.run_status = 1)
BEGIN
RETURN;
END
add a comment |
If your job executes a stored procedure, you can check the job history at the beginning of the SP and return if the job has been successfully executed today.
IF EXISTS(SELECT 1
FROM
msdb.dbo.sysjobs as job
JOIN
msdb.dbo.sysjobhistory his
ON job.job_id = his.job_id
WHERE
job.name = 'your job name'
AND CAST(CAST(his.run_date AS char(8)) as date) = CAST(GETDATE() as date)
AND his.run_status = 1)
BEGIN
RETURN;
END
add a comment |
If your job executes a stored procedure, you can check the job history at the beginning of the SP and return if the job has been successfully executed today.
IF EXISTS(SELECT 1
FROM
msdb.dbo.sysjobs as job
JOIN
msdb.dbo.sysjobhistory his
ON job.job_id = his.job_id
WHERE
job.name = 'your job name'
AND CAST(CAST(his.run_date AS char(8)) as date) = CAST(GETDATE() as date)
AND his.run_status = 1)
BEGIN
RETURN;
END
If your job executes a stored procedure, you can check the job history at the beginning of the SP and return if the job has been successfully executed today.
IF EXISTS(SELECT 1
FROM
msdb.dbo.sysjobs as job
JOIN
msdb.dbo.sysjobhistory his
ON job.job_id = his.job_id
WHERE
job.name = 'your job name'
AND CAST(CAST(his.run_date AS char(8)) as date) = CAST(GETDATE() as date)
AND his.run_status = 1)
BEGIN
RETURN;
END
edited 8 hours ago
Tony Hinkle
2,0781422
2,0781422
answered 8 hours ago
McNetsMcNets
15.8k42061
15.8k42061
add a comment |
add a comment |
I actually wrote a similar tip at mssqltips.com.
For your current case, the general idea would be like this:
you may add an additional step at the end of the job, once your primary job steps succeed, the job will run the final step (i.e. the newly added step)
If your job fails in your primary steps, your job will just exit and no need to run the final added step.
In you final job step, you will do one thing, i.e. updating your job schedule' start date to be next day.
use msdb
declare @active_date int, @sch_name varchar(128);
select @sch_name=s.name
from msdb.dbo.sysschedules s
inner join msdb.dbo.sysjobschedules js
on js.schedule_id = s.schedule_id
and js.job_id = $(ESCAPE_NONE(JOBID));
set @active_date = cast(replace(cast(cast(getdate()+1 as date) as varchar(10)), '-', '') as int);
exec sp_update_schedule @name=@sch_name, @active_start_date= @active_date;
Of course, I assume your job has one dedicated schedule only.
The approach is all self-dependent and you do not need any additional external jobs to manage your current job.
add a comment |
I actually wrote a similar tip at mssqltips.com.
For your current case, the general idea would be like this:
you may add an additional step at the end of the job, once your primary job steps succeed, the job will run the final step (i.e. the newly added step)
If your job fails in your primary steps, your job will just exit and no need to run the final added step.
In you final job step, you will do one thing, i.e. updating your job schedule' start date to be next day.
use msdb
declare @active_date int, @sch_name varchar(128);
select @sch_name=s.name
from msdb.dbo.sysschedules s
inner join msdb.dbo.sysjobschedules js
on js.schedule_id = s.schedule_id
and js.job_id = $(ESCAPE_NONE(JOBID));
set @active_date = cast(replace(cast(cast(getdate()+1 as date) as varchar(10)), '-', '') as int);
exec sp_update_schedule @name=@sch_name, @active_start_date= @active_date;
Of course, I assume your job has one dedicated schedule only.
The approach is all self-dependent and you do not need any additional external jobs to manage your current job.
add a comment |
I actually wrote a similar tip at mssqltips.com.
For your current case, the general idea would be like this:
you may add an additional step at the end of the job, once your primary job steps succeed, the job will run the final step (i.e. the newly added step)
If your job fails in your primary steps, your job will just exit and no need to run the final added step.
In you final job step, you will do one thing, i.e. updating your job schedule' start date to be next day.
use msdb
declare @active_date int, @sch_name varchar(128);
select @sch_name=s.name
from msdb.dbo.sysschedules s
inner join msdb.dbo.sysjobschedules js
on js.schedule_id = s.schedule_id
and js.job_id = $(ESCAPE_NONE(JOBID));
set @active_date = cast(replace(cast(cast(getdate()+1 as date) as varchar(10)), '-', '') as int);
exec sp_update_schedule @name=@sch_name, @active_start_date= @active_date;
Of course, I assume your job has one dedicated schedule only.
The approach is all self-dependent and you do not need any additional external jobs to manage your current job.
I actually wrote a similar tip at mssqltips.com.
For your current case, the general idea would be like this:
you may add an additional step at the end of the job, once your primary job steps succeed, the job will run the final step (i.e. the newly added step)
If your job fails in your primary steps, your job will just exit and no need to run the final added step.
In you final job step, you will do one thing, i.e. updating your job schedule' start date to be next day.
use msdb
declare @active_date int, @sch_name varchar(128);
select @sch_name=s.name
from msdb.dbo.sysschedules s
inner join msdb.dbo.sysjobschedules js
on js.schedule_id = s.schedule_id
and js.job_id = $(ESCAPE_NONE(JOBID));
set @active_date = cast(replace(cast(cast(getdate()+1 as date) as varchar(10)), '-', '') as int);
exec sp_update_schedule @name=@sch_name, @active_start_date= @active_date;
Of course, I assume your job has one dedicated schedule only.
The approach is all self-dependent and you do not need any additional external jobs to manage your current job.
answered 6 hours ago
jyaojyao
2,319420
2,319420
add a comment |
add a comment |
Hitesh Khandelwal is a new contributor. Be nice, and check out our Code of Conduct.
Hitesh Khandelwal is a new contributor. Be nice, and check out our Code of Conduct.
Hitesh Khandelwal is a new contributor. Be nice, and check out our Code of Conduct.
Hitesh Khandelwal is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229664%2fhow-to-stop-a-job-for-the-rest-of-the-day-after-the-relevant-condition-has-been%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown