Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Return Query Text Along With sp_who2 Using SQL 2000 Expand / Collapse
Author
Message
Posted Friday, July 18, 2008 8:50 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, May 9, 2013 8:07 AM
Points: 1,221, Visits: 2,614
philcart (7/18/2008)
How about using fn_get_sql instead of dbcc inputbuffer? That way the sql statement won't be truncated. ;)



Here is the fn_get_sql version, but it will strip out any statement that may have a password. Also, you have to change the "Maximum characters per column" in query analyzer to more than 256 to see the results.

CREATE TABLE #sp_who2 
(
SPID INT,
Status VARCHAR(1000) NULL,
Login SYSNAME NULL,
HostName SYSNAME NULL,
BlkBy SYSNAME NULL,
DBName SYSNAME NULL,
Command VARCHAR(1000) NULL,
CPUTime INT NULL,
DiskIO INT NULL,
LastBatch VARCHAR(1000) NULL,
ProgramName VARCHAR(1000) NULL,
SPID2 INT
)


Create Table #SqlStatement
(spid int,
statement varchar(8000))

create table #temp (dbid varchar(100), objectid varchar(100), number varchar(100), encrypted varchar(100), stmt varchar(8000), id int identity (1,1))


INSERT #sp_who2 EXEC sp_who2


Declare @spid varchar(10)
Declare @Statement varchar(8000)
declare @sql varchar(1000)
DECLARE @Handle binary(20)

DECLARE SpidCursor Cursor
FOR Select spid from #sp_who2

OPEN SpidCursor

FETCH NEXT FROM SpidCursor
INTO @spid

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @Handle = sql_handle FROM sysprocesses WHERE spid = @spid
insert #temp
SELECT * FROM ::fn_get_sql(@Handle)

Insert Into #SqlStatement
Select @spid, stmt From #Temp where id = (Select max(id) from #Temp)

FETCH NEXT FROM SpidCursor
INTO @spid
END

Close SpidCursor
Deallocate SpidCursor


Select B.Statement, A.* from #sp_who2 A Left JOIN
#SqlStatement B ON A.spid = B.spid

Drop Table #Temp
Drop Table #SqlStatement
Drop Table #sp_who2



Ken Simmons
http://twitter.com/KenSimmons
Post #536875
Posted Friday, July 18, 2008 12:08 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 12:17 PM
Points: 184, Visits: 1,012
Your approach for combining the output from a stored procedure and a DBCC Command is very helpful.

Thanks for your article.



David Bird

My PC Quick Reference Guide
Post #536990
Posted Tuesday, July 22, 2008 3:08 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, August 31, 2011 7:47 AM
Points: 30, Visits: 154
Better solution, without cursors or temp tables

select distinct

s.session_id as spid,

cast( convert(varchar, dateadd(second, datediff(ss, s.last_request_start_time, getdate()),'' ), 108) as varchar(8))[delta],

r.cpu_time / nullif(datediff(ss, s.last_request_start_time, getdate()),0) [cpu/sec],

(r.reads + r.writes) / nullif(datediff(ss, s.last_request_start_time, getdate()),0) [diskio/sec],

isnull(w.blocking_session_id, 0) as blockedby,

s.host_name,

s.login_name as login,

db_name(r.database_id) as dbname,

s.program_name,

s.client_interface_name,

s.status,

r.cpu_time as cpu,

r.granted_query_memory as memory,

r.reads,

r.writes,

r.logical_reads,

r.row_count,

cast(r.percent_complete as dec(4,1)) as pct_complete,

r.command,

t.text,

c.client_net_address

from sys.dm_exec_sessions s

left join sys.dm_os_waiting_tasks w on s.session_id = w.session_id

left join sys.dm_exec_requests r on s.session_id = r.session_id

left join sys.dm_exec_connections c on s.session_id = c.session_id

outer apply sys.dm_exec_sql_text(plan_handle) t

where s.status != 'sleeping' -- // no inactive processes

and s.session_id != @@spid
Post #538264
Posted Tuesday, July 22, 2008 4:50 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, May 9, 2013 8:07 AM
Points: 1,221, Visits: 2,614
Lewis,

The Dynamic Management Views are not available in SQL 2000, but I agree if you are using 2005 DMV's are the way to go.


Ken Simmons
http://twitter.com/KenSimmons
Post #538314
Posted Thursday, August 7, 2008 7:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 6, 2009 8:00 AM
Points: 2, Visits: 65
Ken,

Thank you Ken for supplying us with your briliant script compilation for the SQL Activity Process Monitoring.

Works like a charm... ;)

Cheers
Martin


SELECT * FROM users WHERE clue > 0 = 0 rows returned
Post #548266
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse