Procedure Helps Pinpoint Problematic Processes

Downloads
95187.zip

If your SQL Server 2005 machine is experiencing performance problems and you need to quickly see all the T-SQL code currently executing on it, you'll likely be interested in sp_display_curr_tsql_in_sessions. This stored procedure displays all the T-SQL statements currently being executed along with the IDs of the processes running those statements.

As Listing 4 shows, sp_display_curr_tsql_in_sessions loops through all the current sessions it finds in the sys.sysprocesses system view. For each session, the stored procedure fetches the sql_handle. It uses this handle with the fn_get_sql system function to obtain the T-SQL statement that the session is currently running. The fn_get_sql function is similar to the DBCC INPUTBUFFER statement in that both return the T-SQL statement that the specified session is currently executing. However, I prefer using fn_get_sql because it displays all the text in the T-SQL statement, whereas DBCC INPUTBUFFER returns only the first 255 characters in the T-SQL statement. The sp_display_curr_tsql_in_sessions stored procedure stores the session IDs and T-SQL statements in a temporary table, which it later removes.

To execute the sp_display_curr_tsql_in_sessions stored procedure, you use the following statement:

exec sp_display_curr_tsql_in_sessions

As you can see, it doesn't need any parameters.

I wrote sp_display_curr_tsql_in_sessions for use on SQL Server 2005. With a modification, you can get it to work on SQL Server 2000 Service Pack 3 (SP3) and later. (The stored procedure won't work on previous versions of SQL Server 2000 because the fn_get_sql function was introduced in SP3.) You'd need to use varchar(8000) instead of varchar(max) with the CONVERT function because varchar(max) is new to SQL Server 2005.

Discuss this Article 1

alvarr
on Apr 16, 2007
Wouldn't it be easier like this (at least in 2005) select Text from sys.sysprocesses Cross Apply sys.dm_exec_sql_text(sql_Handle) Roger

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
Vision road sign
May 6, 2013
blog

Cheaters Never Win, Even in TPC Benchmarks

In this portion of the series on database benchmarking, I want to tell you about one of my favorite aspects of the TPC benchmarks – CHEATING....More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.