Archive
SQL Server – Check SQL Agent Job History using T-SQL
To check SQL Server Agent Job History you can use the Log File Viewer from SQL Server Management Studio.
To Open Log File Viewer,
1. Expand Server Node > 2. Expand SQL Server Agent > 3. Expand Jobs > 4. Right click on the Job and 5. Select "View History" as shown in the screen shot below:
Alternatively, you can also use below T-SQL code to check Job History:
SELECT [JobName] = JOB.name,
[Step] = HIST.step_id,
[StepName] = HIST.step_name,
[Message] = HIST.message,
[Status] = CASE WHEN HIST.run_status = 0 THEN 'Failed'
WHEN HIST.run_status = 1 THEN 'Succeeded'
WHEN HIST.run_status = 2 THEN 'Retry'
WHEN HIST.run_status = 3 THEN 'Canceled'
END,
[RunDate] = HIST.run_date,
[RunTime] = HIST.run_time,
[Duration] = HIST.run_duration
FROM sysjobs JOB
INNER JOIN sysjobhistory HIST ON HIST.job_id = JOB.job_id
/* WHERE JOB.name = 'Job1' */
ORDER BY HIST.run_date, HIST.run_time
Output:
JobName Step StepName Message Status RunDate RunTime Duration
Job1 1 Step1 Executed a.. Succeeded 20120416 173935 10
Job1 0 (Job outcome) The job su.. Succeeded 20120416 173935 10
Job2 1 Step1 Executed a.. Succeeded 20120416 174037 10
Job2 0 (Job outcome) The job su.. Succeeded 20120416 174037 10
Check dbo.sysjobs and dbo.sysjobhistory on BOL for more information.
Hope This Helps!
Vishal
If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions -> Vishal@SqlAndMe.com
Follow me on Twitter -> @SqlAndMe
SQL Server – Reading ERRORLOG with xp_ReadErrorLog
To read error logs in SQL Server using T-SQL you can use extended stored procedure xp_ReadErrorLog to read SQL Server and SQL Server Agent error logs. xp_ReadErrorLog has seven parameters that can be used to filter error logs.
Syntax for xp_ReadErrorLog:
EXEC xp_ReadErrorLog <LogNumber>, <LogType>,
<SearchTerm1>, <SearchTerm2>,
<StartDate>, <EndDate>, <SortOrder>
The parameter values can be as follows:
| Parameter | Values |
| <LogNumber> | Log number 0, 1, 2 … For example 0 returns current log. 2 returns logs from ERRORLOG.2 |
| <LogType> | 1 – Reads SQL Server error logs, 2 – Reads SQL Server Agent error logs |
| <SearchTerm1> | Search Term for Text Column |
| <SearchTerm2> | Search Term for Text Column * When both search terms are specified, it only returns lines containing both terms |
| <StartDate> | Start reading logs from specified date |
| <EndDate> | Reads logs till this date |
| <SortOrder> | ASC – Ascending or DESC – Descending |
You can use the stored procedure as:
EXEC xp_ReadErrorLog
- Reads current SQL Server error log
Below are some more examples of xp_ReadErrorLog:
EXEC xp_ReadErrorLog 1
- Reads SQL Server error log from ERRORLOG.1 file
EXEC xp_ReadErrorLog 0, 1
- Reads current SQL Server error log
EXEC xp_ReadErrorLog 0, 2
- Reads current SQL Server Agent error log
EXEC xp_ReadErrorLog 0, 1, 'Failed'
- Reads current SQL Server error log with text 'Failed'
EXEC xp_ReadErrorLog 0, 1, 'Failed', 'Login'
- Reads current SQL Server error log with text ‘Failed’ AND 'Login'
EXEC xp_ReadErrorLog 0, 1, 'Failed', 'Login', '20121101', NULL
- Reads current SQL Server error log with text ‘Failed’ AND ‘Login’ from 01-Nov-2012
EXEC xp_ReadErrorLog 0, 1, 'Failed', 'Login', '20121101', '20121130'
- Reads current SQL Server error log with text ‘Failed’ AND ‘Login’ between 01-Nov-2012 and 30-Nov-2012
EXEC xp_ReadErrorLog 0, 1, NULL, NULL, '20121101', '20121130'
- Reads current SQL Server error between 01-Nov-2012 and 30-Nov-2012
EXEC xp_ReadErrorLog 0, 1, NULL, NULL, '20121101', '20121130', 'DESC'
- Reads current SQL Server error log between 01-Nov-2012 and 30-Nov-2012 and sorts in descending order
Hope This Helps!
Vishal
If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions -> Vishal@SqlAndMe.com
Follow me on Twitter -> @SqlAndMe

