How to store longer SQL Agent Job Step output messages
By: Greg Robidoux | Updated: 2018-06-02 | Comments (7) | Related: > SQL Server Agent
When working with SQL Agent jobs sometimes it is difficult to determine why a job failed or even exactly what occurred. For each job step SQL Server provides a message, but it is not always that easy to determine exactly what was occurring during that step. The default message that is saved in the job history is 1024 characters. In most cases this may be enough, but if you have a long running process you may need to store more than 1024 characters. Is there any way to get additional information within the job history to help troubleshoot issues as well as just knowing what occurred?
In SQL Server 2005 and later you have the ability to log additional SQL Agent job output beyond the 1024 characters that is stored in the msdb.dbo.sysjobhistory table. By default this enhanced logging is not turned on it is something that you need to turn on for each job step.
Let's take for example we are running DBCC CHECKDB commands for several databases. This command provides a lot of output data unless you use the WITH NO_INFOMSGS option. If you are not using the NO_INFOMSGS option the command output fills up the 1024 characters quite quickly and you can only see the part of the output in the job history message.
Here is a screen shot of the job history for the step that did a DBCC CHECKDB. As you can see we only get a portion of the command output.
To allow additional data to be logged you need to turn on some settings for each job step in your job. To do this edit the job step and select the Advanced tab.
On this screen you need to enable both the "Log to table" and "Include step output in history".
After we make these changes and run this again if we look at the job history you will see the same short message.
In order to see the additional logged information you need to use this stored procedure sp_help_jobsteplog or you could query the msdb.dbo.sysjobstepslog table directly.
If we run this command in a query window (test2 is the job name):
EXEC dbo.sp_help_jobsteplog @job_name = N'test2'; GO
we will get this additional output: (note: the output was edited to remove most of the middle to keep this web page smaller)
sys.queue_messages_2041058307'. [SQLSTATE 01000] There are 0 rows in 0 pages for object "sys.queue_messages_2041058307". [SQLSTATE 01000] DBCC results for 'Table1'. [SQLSTATE 01000] There are 62 rows in 1 pages for object "Table1". [SQLSTATE 01000] DBCC results for 'sysarticles'. [SQLSTATE 01000] There are 2 rows in 1 pages for object "sysarticles". [SQLSTATE 01000] DBCC results for 'sysarticlecolumns'. [SQLSTATE 01000] There are 4 rows in 1 pages for object "sysarticlecolumns". [SQLSTATE 01000] CHECKDB found 0 allocation errors and 0 consistency errors in database 'Test1'. [SQLSTATE 01000] DBCC execution completed. If DBCC printed error messages, contact your system administrator. [SQLSTATE 01000]
As you can see we can now get the entire output message since the output in the sysjobstepslog is stored as a nvarchar(max) instead of an nvarchar(1024) like in sysjobhistory.
- Helpfully this tip is useful for you to get additional job step history, just a couple of clicks can provide a lot more detail
- Keep in mind that only one row is stored per job step. If you use the "Append output to existing entry in table" this will just append the data to the existing record.
- Take a look at these other useful SQL Agent tips
About the author
View all my tips
Article Last Updated: 2018-06-02