Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find current query running in sql server 2005

Posted on 2011-03-08
4
Medium Priority
?
1,401 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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 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