SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic TSQL to rerun failed jobs


Dynamic TSQL to rerun failed jobs

Author
Message
asdsfssd
asdsfssd
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 29
Need help in scripting - I'm trying to create a script to run on SQL Servers with the following goals in mind.
1) It'll look for failed jobs.
2) Re-run those failed jobs.

The script I have:

-- Declare some variables
DECLARE @count int
DECLARE @rancount int
DECLARE @job varchar(50)

-- Get failed jobs to a temp table rerunjb

select a.name from msdb..sysjobs a, msdb..sysjobhistory b
into #rerunjb
where a.job_id = b.job_id
and b.run_status = 0
and CONVERT(VARCHAR(8), b.run_date, 112) = CONVERT(VARCHAR(8),getdate(),112)
order by b.name

set @rancount = 0
set @count = (select count(*) from #rerunjb)
if @count > 0
while (@rancount < @count)
BEGIN
set @job = (select name from #rerunjb)
--comment out for now
--exec msdb..sp_start_job @job_name = '@job'
PRINT @job
set @rancount = @rancount+1
end


I'm getting 2 row(s) affected (which is right - 2 rows are inserted into #rerunjb);
but I only get Job1 (from the PRINT @job). For some reason it's not looping.
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41188 Visits: 19815
Do you get job1 printed twice? or only once?


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
asdsfssd
asdsfssd
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 29
I'm getting twice Job 1. And I think the issue is
set @job = (select TOP 1 name from #rerunjb)

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search