Solved

Find current query running in sql server 2005

Posted on 2011-03-08
4
1,285 Views
Last Modified: 2012-05-11
HI,
I am running SQL profiler to troubleshoot a long running query.  I am trying to figure out a query that is taking a very long time.  There are a few applications running on this server.  I know I can use profiler to get the “SQL:BatchStarting” and the “SQL:BatchCompleted” to capture the TextData and find out the SQL query that is running and how long it took.  My question is how can I see the current query that is running in SQL Server without having to wait until it is completed.   Some of the queries are taking a very long time (may even abort at the end) and makes it difficult to troubleshoot and find out which one it was.   Is there a way to run a SQL Command to see the current running queries on the server?
Hope I was clear enough.
Thanks,
Miguel

FYI:  I can do this in Oracle running the following command.  Looking for the same in SQL Server.

 SELECT
      substr(a.username,1,10) UserName,a.sid, substr(a.osuser,1,10) osuser, p.spid OS_PID, a.AUDSID, substr(a.process,1,10) Session_ID,a.serial#,a.Last_Call_ET, a.command, substr(a.lockwait,1,5) lockwait ,substr(a.machine,1,17) machine ,substr(a.program,1,10) program,b.piece,b.sql_text
FROM
v$session a, v$sqltext_with_newlines b,v$process p
WHERE
a.status = 'ACTIVE' AND a.username <> 'SYS'
AND      a.sql_hash_value= b.hash_value
AND     a.paddr = p.addr
ORDER BY osuser,sid,piece;
0
Comment
Question by:agcsupport
4 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
Comment Utility
Try this query.

http://blog.sqlauthority.com/2009/01/07/sql-server-find-currently-running-query-t-sql/
SELECT sqltext.TEXT, 
       req.session_id, 
       req.status, 
       req.command, 
       req.cpu_time/(60*1000) cpu_time_in_minuetes, 
       req.total_elapsed_time/(60*1000) total_elapsed_time_in_minutes 
  FROM sys.dm_exec_requests req 
       CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext

Open in new window

0
 
LVL 2

Assisted Solution

by:EL_Barbado
EL_Barbado earned 250 total points
Comment Utility
Hi,
You can use this SQL statement (or a variation) to find the information that you need.

SELECT Object_name(objectid), SQLText.text, *
FROM sys.sysprocesses sp
CROSS APPLY sys.dm_exec_sql_text ( sp.sql_handle ) SQLText

This will show you the name of the stored procedure (if its a procedure) and the exact batch that is being executed along with other information about the process. You can include a WHERE clause to filter the data to you liking.

I hope this helps!
0
 
LVL 14

Expert Comment

by:Daniel_PL
Comment Utility
If you can create procedures on you server I's suggest you to use Adam Machanic's  Who Is Active stored procedure. It's like a sp_who2 on steroids.
Give it a try if you want to:
http://sqlblog.com/files/folders/release/default.aspx
0
 

Author Closing Comment

by:agcsupport
Comment Utility
Both very usefull.  Thanks a lot.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now