Home > SQL Server Tips > Stored Procedures > Stored procedure: Examining SQL Agent job run history
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

STORED PROCEDURES

Stored procedure: Examining SQL Agent job run history


By Brian Walker
11.29.2005
Rating: -4.67- (out of 5)


Expert advice on database development
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


This tip continues the system stored procedure series with a routine to list run history for selected SQL Agent jobs.

One of the many things a SQL Server database administrator needs to keep track of is SQL Agent jobs -- defined tasks of a relatively low priority that execute in parallel with normal operations. SQL Agent jobs can be invoked directly, but they are often invoked indirectly by setting them up with a schedule for execution. The SQL Agent component of SQL Server automatically runs the jobs according to their schedules.

Each time a SQL Agent job runs a log entry is made. The accumulated log entries form the job's run history, which includes the dates and times of execution, as well as status messages that describe the results. It's not very convenient to examine the run history using Enterprise Manager and there's no capability to filter the log entries.

The SQL code in Listing 1 creates a system stored procedure named sp_ListJobRunHistory. The routine lists specified run history records for selected SQL Agent jobs.

The sp_ListJobRunHistory stored procedure accepts six parameters, but none of them are required.

The first parameter (@DBAdmin) is optional and it specifies the maximum number of history records to return for each job. A value of zero (0) is the default and it means up to 100 records will be returned for each job. The actual number of records returned for each job may be further limited by the available history and by the other parameters.

The next parameter (@DBUltra) is optional and it specifies whether to include history for job runs that finished successfully. A value of zero (0) includes history for job runs that finished with any status. A value of one (1) excludes history for job runs that finished successfully.

The next four parameters are optional and they work together to form a combination of selection criteria using job names. Please refer to my earlier tip for an explanation of how these parameters work. These parameters specify the SQL Agent jobs to be considered.

The sp_ListJobRunHistory stored procedure returns the most recent history records, up to the number indicated by @DBAdmin (or the number that actually exist). There is one row per job and the rows are filtered according to @DBUltra and the other parameters.

I recommend using the Customize option under the Tools menu of Query Analyzer to set up an appropriate call of this stored procedure so it can be executed with a simple keyboard combination. This


screen image demonstrates the suggestion.

This example lists up to five history records for each job, but only if they ended without success.

EXECUTE sp_ListJobRunHistory 5,1

This example lists the most recent history record for each job, regardless of its execution status.

EXECUTE sp_ListJobRunHistory 1,0

I hope you find this system stored procedure to be useful.


Click for the stored procedure: sp_ListJobRunHistory


About the author: Brian Walker is a senior database architect in an IS department that uses SQL Server 2000 and the .NET Framework. He has more than 25 years of experience in the IT industry with the last several years focused on databases and SQL Server. Walker is a software developer, database developer, database administrator and database consultant. He develops utility software as a hobby, including a large collection of SQL Server utilities.


More information from SearchSQLServer.com

  • Fast Guide: Undocumented stored procedures
  • Tips: View our complete collection of stored procedures
  • Topic: Get stored procedure tips and tricks in this topic section


  • Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


    RELATED CONTENT
    Stored Procedures
    Check SQL Server database and log file size with this stored procedure
    Configure SQL Server Service Broker for sending stored procedure data
    Find size of SQL Server tables and other objects with stored procedure
    Stored procedure to monitor long-running jobs in SQL Server 2000
    Make changes to SQL Server stored procedures with batch editing
    Stored procedure to find fragmented indexes in SQL Server
    Use table-valued parameters for SPs in SQL Server 2008
    Examples of SQL Server stored procedures and parameters
    Top 10 SQL Server development questions
    FAQ: SQL Server stored procedure how-tos

    SQL Server stored procedures
    SQL Server Service Broker Tutorial and Reference Guide
    SQL Server trigger vs. stored procedure to receive data notification
    How to use SQL Server 2008 hierarchyid data type
    SQL Server stored procedures tutorial: Write, tune and get examples
    Check SQL Server database and log file size with this stored procedure
    SQL Server source code analysis and management adds database security
    Configure SQL Server Service Broker for sending stored procedure data
    Find size of SQL Server tables and other objects with stored procedure
    Track changes to SQL Server 2000 and 2005 with one simple utility
    Troubleshoot SQL Server 2005 temporary table performance problems

    SQL/Transact SQL (T-SQL)
    The sqlcmd utility in SQL Server
    How to create a SQL inner join and outer join: Basics to get started
    Avoid cursors in SQL Server with these methods to loop over records
    Implementing security audit in SQL Server 2008
    New datetime data types in SQL Server 2008 offer flexibility
    Basic objects of T-SQL in SQL Server 2008
    Using T-SQL data types in SQL Server 2008
    SQL Server 2008 function types in T-SQL
    Additional T-SQL operations in SQL Server 2008
    Using DATEADD and DATEDIFF to calculate SQL Server datetime values
    SQL/Transact SQL (T-SQL) Research

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    library  (SearchSQLServer.com)
    trigger  (SearchSQLServer.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



    SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts