Solved

Get running SQL statements

Posted on 2006-07-24
10
585 Views
Last Modified: 2008-02-26
We can know all active spid by
      select spid from master..sysprocesses

How to know the sql statements running by all these active spid?
Thanks!
0
Comment
Question by:luyan
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:ibost
ID: 17168086
I think you'd use the SQL Profiler tool
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17168120
for each spid, you can run
dbcc inputbuffer (spid)

0
 

Author Comment

by:luyan
ID: 17168207
For one spid using
dbcc inputbuffer (spid)

How about to get all statements of all spid in system?
Thanks!
0
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.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17168246
I repeat my sentance:
for EACH spid, you can run

there is nothing "for each" built-in in sql server 2000.
now, you might be using sql server 2005?
0
 
LVL 27

Expert Comment

by:ptjcb
ID: 17168384
There is also fn_get_sql (if you are running 2000 and sp3 or greater).

From BOL:
DECLARE @Handle varbinary(64);
SELECT @Handle = sql_handle
FROM sys.dm_exec_requests
WHERE session_id = 52 and request_id = 0;
SELECT * FROM ::fn_get_sql(@Handle);
GO


You would create a cursor to walk through the spids and find the active and then the fn_get_sql or inputbuffer. This link has an example of how it is done: http://www.sommarskog.se/sqlutil/aba_lockinfo.html

0
 

Author Comment

by:luyan
ID: 17168522
ptjcb,
I run following script in SQL Analyzer and get error -- Invalid object name 'sys.dm_exec_requests'.
What is dm_exec_requests?  Thanks!

DECLARE @Handle varbinary(64);
SELECT @Handle = sql_handle
FROM sys.dm_exec_requests
WHERE session_id = 52 and request_id = 0;
SELECT * FROM ::fn_get_sql(@Handle);
GO

Server: Msg 208, Level 16, State 1, Line 2
Invalid object name 'sys.dm_exec_requests'.
0
 
LVL 27

Expert Comment

by:ptjcb
ID: 17169727
Oh...that's the 2005 version. This is the 2000 version (from http://support.microsoft.com/kb/325607)

DECLARE @Handle binary(20)
SELECT @Handle = sql_handle FROM sysprocesses WHERE spid = 52
SELECT * FROM ::fn_get_sql(@Handle)


sys.dm_exec_requests is a sql 2005 dynamic view.
0
 

Author Comment

by:luyan
ID: 17170007
I would like to get all current running statements not only spid=52. Thanks!
0
 
LVL 27

Accepted Solution

by:
ptjcb earned 500 total points
ID: 17170508
I know - that is why I added the link for aba_lockinfo http://www.sommarskog.se/sqlutil/aba_lockinfo
0
 

Author Comment

by:luyan
ID: 17194612
Good stuff! Thanks!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query and VBA 5 45
SQL server 2014 replication error 16 34
reccommendations for a free msft sql query manager? 4 27
SQL Group By Question 4 19
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

860 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