Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query helps.

Author  Topic 

NguyenL71
Posting Yak Master

228 Posts

Posted - 2008-08-18 : 13:30:06
How can I pass in the database name and return the status of running job from last night. I try to query out the status of backup job and input para is dbname. What other table I need to JOINs with?. Any helps is greatly appreciate.


DECLARE @DatabaseName VARCHAR(65)
SET @DatabaseName = 'Devdb'

SELECT CASE a.run_status
WHEN 0 THEN 'F'
WHEN 1 THEN 'S'
WHEN 2 THEN 'R'
WHEN 3 THEN 'C'
WHEN 4 THEN 'IP'
END, b.name
FROM msdb..sysjobhistory AS a
INNER JOIN msdb..sysjobs AS b
ON a.job_id = b.job_id
AND dbName = @DatabaseName ???
WHERE a.run_status = CONVERT(INT, CONVERT(CHAR(8), DATEADD(dd, -1, GETDATE()), 112)) -- return yesterday
go

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-18 : 13:45:56
http://www.sqlservercentral.com/articles/Administering/managingjobsusingtsql/945/
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-08-18 : 14:11:26
quote:
Originally posted by NguyenL71

How can I pass in the database name and return the status of running job from last night. I try to query out the status of backup job and input para is dbname. What other table I need to JOINs with?. Any helps is greatly appreciate.


DECLARE @DatabaseName VARCHAR(65)
SET @DatabaseName = 'Devdb'

SELECT CASE a.run_status
WHEN 0 THEN 'F'
WHEN 1 THEN 'S'
WHEN 2 THEN 'R'
WHEN 3 THEN 'C'
WHEN 4 THEN 'IP'
END, b.name
FROM msdb..sysjobhistory AS a
INNER JOIN msdb..sysjobs AS b
ON a.job_id = b.job_id
AND dbName = @DatabaseName ???
WHERE a.run_status = CONVERT(INT, CONVERT(CHAR(8), DATEADD(dd, -1, GETDATE()), 112)) -- return yesterday
go



Jobs are stored under server level. Are you trying to find all jobs related to that DB and status?
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-08-18 : 23:31:12
DB name is in msdb..sysjobsteps.
Go to Top of Page
   

- Advertisement -