﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Ken Simmons  / Return Query Text Along With sp_who2 Using SQL 2000 / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 18 Jun 2013 17:26:35 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>Ken,Thank you Ken for supplying us with your briliant script compilation for the SQL Activity Process Monitoring. Works like a charm...   ;)CheersMartin</description><pubDate>Thu, 07 Aug 2008 07:15:52 GMT</pubDate><dc:creator>Martin-450785</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>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.</description><pubDate>Tue, 22 Jul 2008 04:50:15 GMT</pubDate><dc:creator>KenSimmons</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>Better solution, without cursors or temp tablesselect 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_addressfrom            sys.dm_exec_sessions sleft join       sys.dm_os_waiting_tasks w       on s.session_id = w.session_idleft join       sys.dm_exec_requests r  on s.session_id = r.session_idleft join       sys.dm_exec_connections c  on s.session_id = c.session_idouter apply sys.dm_exec_sql_text(plan_handle) twhere           s.status != 'sleeping'                                                  -- // no inactive processesand                     s.session_id != @@spid</description><pubDate>Tue, 22 Jul 2008 03:08:13 GMT</pubDate><dc:creator>Lewis Dowson</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>Your approach for combining the output from a stored procedure and a DBCC Command is very helpful. Thanks for your article.</description><pubDate>Fri, 18 Jul 2008 12:08:17 GMT</pubDate><dc:creator>David Bird</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>[quote][b]philcart (7/18/2008)[/b][hr]How about using fn_get_sql instead of dbcc inputbuffer? That way the sql statement won't be truncated. ;)[/quote]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.[code]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_who2OPEN SpidCursorFETCH NEXT FROM SpidCursorINTO @spidWHILE @@FETCH_STATUS = 0BEGIN     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 @spidENDClose SpidCursorDeallocate SpidCursorSelect B.Statement, A.* from #sp_who2 A Left JOIN    #SqlStatement B ON A.spid = B.spidDrop Table #TempDrop Table #SqlStatementDrop Table #sp_who2[/code]</description><pubDate>Fri, 18 Jul 2008 08:50:09 GMT</pubDate><dc:creator>KenSimmons</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>You might do a search in Google for sp_who_3 (http://vyaskn.tripod.com/sp_who3.htm). Very robust with lots of optional parameters and already built to show the input buffer and a lot more than sp_who_2.</description><pubDate>Fri, 18 Jul 2008 07:14:05 GMT</pubDate><dc:creator>dbishop</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>So would it be possible to rewrite this so as to avoid the cursor?</description><pubDate>Fri, 18 Jul 2008 06:53:59 GMT</pubDate><dc:creator>Charles Kincaid</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>The only one problem, in multi-user environment and with quick transactions the time between taking is with sp_who and looping through those ids is enough that process is ended and another process assigned with this id. Then you get wrong inputbuffer output. We have it all the time with 2,000 users and 50-60 transactions per second.  Sometime 10-15% of id returns wrong inputbuffer or disappeared while loop is running</description><pubDate>Fri, 18 Jul 2008 06:27:53 GMT</pubDate><dc:creator>LP-181697</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>Excellent.  This will be very useful.</description><pubDate>Fri, 18 Jul 2008 05:52:05 GMT</pubDate><dc:creator>Ken Davis</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>Extremely useful - much appreciated!</description><pubDate>Fri, 18 Jul 2008 03:20:48 GMT</pubDate><dc:creator>rodonoghue</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>It works fine for SQL 2005 if you add an extra column to the create statement of #sp_who2CREATE 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 	 [b], RequestId INT [/b] -- Extra column for SQL2005	)</description><pubDate>Fri, 18 Jul 2008 02:46:49 GMT</pubDate><dc:creator>Jogos</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>Great and very useful article. :)</description><pubDate>Fri, 18 Jul 2008 01:39:13 GMT</pubDate><dc:creator>Anipaul</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>How about using fn_get_sql instead of dbcc inputbuffer? That way the sql statement won't be truncated. ;)</description><pubDate>Fri, 18 Jul 2008 00:35:49 GMT</pubDate><dc:creator>philcart</dc:creator></item><item><title>RE: Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>The code from the article does not copy/paste correctly, so I am posting it here.  I have noticed that if you paste it into Word or Open Office it will display correctly.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 (x varchar(100), y int, s varchar(1000), id intidentity (1,1))INSERT #sp_who2 EXEC sp_who2 Declare @spid varchar(10)Declare @Statement varchar(8000)declare @sql varchar(1000)DECLARE SpidCursor Cursor    FOR Select spid from #sp_who2OPEN SpidCursorFETCH NEXT FROM SpidCursorINTO @spidWHILE @@FETCH_STATUS = 0BEGIN   SET @sql = 'dbcc inputbuffer (' + @spid + ')'   insert #temp   exec (@sql)   Insert Into #SqlStatement    Select @spid, s  From #Temp where id = (Select max(id) from #Temp)  FETCH NEXT FROM SpidCursor  INTO @spidENDClose SpidCursorDeallocate SpidCursorSelect B.Statement, A.* from #sp_who2 A Left JOIN    #SqlStatement B ON A.spid = B.spidDrop Table #TempDrop Table #SqlStatementDrop Table #sp_who2</description><pubDate>Thu, 17 Jul 2008 20:21:51 GMT</pubDate><dc:creator>KenSimmons</dc:creator></item><item><title>Return Query Text Along With sp_who2 Using SQL 2000</title><link>http://www.sqlservercentral.com/Forums/Topic536417-1306-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/articles/SQL+Server+2000/63542/"&gt;Return Query Text Along With sp_who2 Using SQL 2000&lt;/A&gt;[/B]</description><pubDate>Thu, 17 Jul 2008 20:07:01 GMT</pubDate><dc:creator>KenSimmons</dc:creator></item></channel></rss>