Home > SQL Server Tips > Stored Procedures > Stored procedure to monitor long-running jobs in SQL Server 2000
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

STORED PROCEDURES

Stored procedure to monitor long-running jobs in SQL Server 2000


Michelle Gutzait
02.27.2008
Rating: -5.00- (out of 5)


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


The problem:

Monitoring long-running jobs in SQL Server 2000 is a complicated task. The problem is that the job execution history is written only after the first job step is completed. Therefore, if the first -- or only -- step of the job runs for a long time, you can't query any system table or function in order to capture its start time.

In this article, you'll learn about a T-SQL fix that retrieves information about long-running jobs and overcomes this problem.

Quick fix to identify long-running SQL Server jobs:

One option to easily solve this problem is to add a dummy first step to the job. The dummy step should do a simple and quick task, such as SELECT GETDATE(). Upon adding this step, the sysjobhistory table is monitored while the next steps of the job are executing. The run_date and run_time fields of this first step in the sysjobhistory table will show the date and time when the step completed and, thus, it will show the date and time when the next steps started.

Here is an example that shows the list of running jobs where the second step had started more than two minutes ago:

script showing the list of running jobs where the second step had started more than two minutes ago
(Click on code for script download.)

What about long-running SQL Server jobs in a complex environment?

What if you have hundreds of SQL jobs in dozens of servers? What if you are not entitled to add steps to jobs -- for example, when the jobs were built by an external vendor?

Adding another step to all the existing jobs may become extremely time-consuming or not allowed.

Let's talk about an alternate way to monitor long-running SQL jobs.

My goal here is to check the long-running jobs from a centralized location and gather information about them into a central table. Note: A long-running SQL Server 2000 job in my environment is one running for more than 4 to 4.5 hours.

In SQL Server 2000 Enterprise Manager, the job status is shown in the list of jobs:

View of job status in SQL Server 2000 Enterprise Manager
(Click on image for enlarged view.)

If you run SQL Profiler to monitor what is running when the Job list pane is opened, here's what you'll get:

exec msdb..sp_help_job

Since I'd like to store the results of the jobs in a central table, I cannot use the sp_help_job stored procedure.

If you run the command, INSERT… EXEC msdb..sp_help_job, in order to save the output in a table, it will fail because the stored procedure sp_help_job calls inner stored procedures.

Here's the solution:

Step 1:
I already have a central database named DBADB that was created for a similar reason -- gathering information from remote servers for management purposes.

The DBADB database contains a table with the list of all the "active" Linked Servers. I also created a Link Server for each of the servers on the list.

The table of servers (tbl_ServerList) contains only one field named ServerName.

So, in order to gather information from all the Linked Servers, I need to loop over the list of servers in the table and execute whatever's necessary.

For the long-running jobs information, I create a central table:

script creating a central table
(Click on code for script download.)

Step 2:
Looking carefully at the source code of the sp_help_job stored procedure, it appears that the following extended stored procedure runs and queries job information directly from the SQL Server Agent:

master.dbo.xp_sqlagent_enum_jobs

Since I don't want to rely on having MSDTC (Microsoft Distributed Transaction Coordinator), and since I will use Linked Servers to query the information from the remote servers,
More on Performance Tuning SQL Server:
  • Tracking query execution with SQL Server 2005 Profiler

  • Targeting SQL Profiler to determine performance problems

  • Query Analyzer: 10 tricks for simple querying
  • I will have to create a stored procedure on every REMOTE server participating in this process. The creation of this SP should be done once on each remote server and prior to the startup of the central process described in the following step. This stored procedure will run the master.dbo.xp_sqlagent_enum_jobs SP and insert the results into a temporary table. That way, my central SP can read the data from the temporary table using Linked Server.

    I have a DBADB database on each server, so I can create this stored procedure there. You could create it in the master database, although it's not recommended. The stored procedure should look like this:

    stored procedure created in the DBADB database
    (Click on code for script download.)

    Step 3:
    The central stored procedure should look like this:

    central stored procedure
    (Click on code for script download.)

    Step 4:
    Execute or schedule the execution of the SP as needed. For example, I scheduled it to run every 30 minutes because I need to be notified immediately when a job is running for more than 4 to 4.5 hours. The next step in my job looks for long-running jobs and send me an email alert with the list, if there are such jobs.

    The query to select long-running SQL jobs should look similar to the following:

    query to select long-running SQL jobs
    (Click on code for script download.)

    Monitoring your SQL Server environment to identify long-running jobs is a best practice for DBAs. It gives you an idea about performance degradation on the different jobs and also alerts you about jobs that might affect performance of other activities in your SQL Server environment. If you thought this was an impossible mission with SQL Server 2000, just follow the steps in this article, copy the stored procedures and you're ready to go.

    ABOUT THE AUTHOR:   
    Michelle Gutzait works as a principal database consultant for Victrix, a Montreal-based IT consulting firm specializing in information security, infrastructure, communication networks and applicative solutions and collaboration. Gutzait has been involved in IT for 20 years as a developer, business analyst and database consultant. For the past 10 years, she has worked exclusively with SQL Server, including SQL infrastructure and database design, performance tuning, security, high availability, disaster recovery, VLDBs, replication, T-SQL coding, DTS/SSIS packages, administrative and infrastructure tools development and reporting services.
    Copyright 2008 TechTarget


    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
    SQL Server performance and tuning
    Check SQL Server database and log file size with this stored procedure
    SQL Server PerfMon counters for access methods and buffer manager
    Find size of SQL Server tables and other objects with stored procedure
    Monitor SQL Server disk I/O with PerfMon counters
    SQL Server tempdb best practices increase performance
    SQL Server PerfMon counters for Windows operating system (OS)
    How to maintain SQL Server indexes for query optimization
    Performance tuning for SQL Server 2005 and Exchange running on SBS
    Troubleshoot SQL Server 2005 temporary table performance problems
    Maintain large SQL Server database and resolve website 'Timeout Error'

    SQL Server stored procedures
    Check SQL Server database and log file size with this stored procedure
    SQL Server source code analysis and management adds database security
    SQL and SQL Server Tutorial and Reference Guide
    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
    Use SQL Profiler to find long running stored procedures and commands
    Using BULK INSERT to insert rows from SQL Server dataset to table
    SQL Server query to import database names

    SQL/Transact SQL (T-SQL)
    Create DDL table in SQL Server 2005 to audit DDL trigger activity
    SQL Server source code analysis and management adds database security
    SQL and SQL Server Tutorial and Reference Guide
    Retrieve XML data values with XQuery in SQL Server 2005
    How to construct and use SQL OUTER JOINs optimally
    How to use the LEFT vs. RIGHT OUTER JOIN in SQL
    Using the FULL OUTER JOIN in SQL
    SQL OUTER JOIN sample uses
    SQL OUTER JOIN sample statements for queries
    Five sqlcmd features to automate SQL Server database tasks
    SQL/Transact SQL (T-SQL) Research

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    contiguity  (SearchSQLServer.com)
    contiguous  (SearchSQLServer.com)
    drilldown  (SearchSQLServer.com)
    hashing  (SearchSQLServer.com)
    hybrid online analytical processing  (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.

    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsWebcastsWhite 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  |  Reprints  |  Site Map




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