Solved

Find current query running in sql server 2005

Posted on 2011-03-08
4
1,370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 250 total points
ID: 35072248
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
ID: 35072315
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
ID: 35072332
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
ID: 35072358
Both very usefull.  Thanks a lot.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

759 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