SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Return Query Text Along With sp_who2 Using SQL 2000


Return Query Text Along With sp_who2 Using SQL 2000

Author
Message
KenSimmons
KenSimmons
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3832 Visits: 2614
philcart (7/18/2008)
How about using fn_get_sql instead of dbcc inputbuffer? That way the sql statement won't be truncated. Wink



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
David Bird
David Bird
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1856 Visits: 1300
Your approach for combining the output from a stored procedure and a DBCC Command is very helpful.

Thanks for your article.

David Bird
Lewis Dowson
Lewis Dowson
Old Hand
Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)

Group: General Forum Members
Points: 356 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
KenSimmons
KenSimmons
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3832 Visits: 2614
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
Martin-450785
Martin-450785
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 65
Ken,

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

Works like a charm... Wink

Cheers
Martin

SELECT * FROM users WHERE clue > 0 = 0 rows returned
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search