Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


New SP_WHO3


New SP_WHO3

Author
Message
ricardo_chicas
ricardo_chicas
SSC-Enthusiastic
SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)

Group: General Forum Members
Points: 198 Visits: 676
Hello all,
I just did this sp_who and I think some of you will find it useful, You can provide it with three parameter ( it is shielded against code injection too...)
1) spid
2) databasename
3) session status

USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_who3] Script Date: 06/14/2010 16:17:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sp_who3] (
@SessionID int = NULL ,
@DBID varchar(100) = NULL ,
@statusID varchar(100) = NULL
)
AS
BEGIN

DECLARE @sSql nvarchar(4000)
DECLARE @sWhereClause nvarchar(4000)
DECLARE @sFootClause nvarchar(4000)
DECLARE @ParmDefinition nvarchar(4000)
DECLARE @NewLine nvarchar(4000)
SET @NewLine = CHAR(13) + CHAR(10)
SET @sWhereClause = '' -- Initialise

SET @sSql = 'SELECT ' + @NewLine
+ ' SPID = e.spid, ' + @NewLine
+ ' Status = max(A.status), ' + @NewLine
+ ' waits = sum(e.waittime), ' + @NewLine
+ ' [Login] = max(A.login_name), ' + @NewLine
+ ' HostName = max(A.host_name), ' + @NewLine
+ ' BlkBy = max(c.blocking_session_id), ' + @NewLine
+ ' DBName = max(DB_Name(e.dbid)), ' + @NewLine
+ ' Command = max(e.cmd), ' + @NewLine
+ ' SQLStatement = max(isnull(substring(b.text,1,4000),'''')), ' + @NewLine
+ ' ObjectName = max(OBJECT_NAME(b.objectid)), ' + @NewLine
+ ' Programname = max(A.Program_name), ' + @NewLine
+ ' ElapsedMS = max(C.total_elapsed_time), ' + @NewLine
+ ' CPUTime = sum(isnull(e.cpu,0)), ' + @NewLine
+ ' IOReads = max(isnull(a.logical_reads + a.reads,0)), ' + @NewLine
+ ' IOWrites = max(isnull(a.writes,0)), ' + @NewLine
+ ' LastWaitType = max(e.lastwaittype), ' + @NewLine
+ ' LastBatch = max(e.last_batch), ' + @NewLine
+ ' DiskIO = sum(e.physical_io) ' + @NewLine
+ ' FROM sys.dm_exec_sessions (nolock) a ' + @NewLine
+ ' LEFT JOIN sys.dm_exec_requests (nolock) c ON c.session_id = a.session_id ' + @NewLine
+ ' LEFT JOIN sys.sysprocesses E (nolock) ON e.spid = a.session_id ' + @NewLine
+ ' OUTER APPLY sys.dm_exec_sql_text(e.sql_handle) b ' + @NewLine
+ ' LEFT JOIN sys.dm_exec_connections (nolock) d ON d.session_id = a.session_id ' + @NewLine
+ ' where a.session_id > 50 '+ @NewLine

if @SessionID IS NOT NULL
SET @sWhereClause= @sWhereClause+@NewLine+ ' AND a.session_id = @SessionID'

IF @DBID IS NOT NULL
SET @sWhereClause= @sWhereClause+@NewLine+ ' AND DB_Name(e.dbid) = @DBID'


IF @StatusID IS NOT NULL
SET @sWhereClause= @sWhereClause+@NewLine+ ' AND a.status = @StatusID'

SET @sFootClause = ' group by e.spid ORDER BY e.spid'

SET @ParmDefinition = '@SessionID int,' + @NewLine
+ ' @DBID varchar(100),' + @NewLine
+ ' @StatusID varchar(100)'

SET @sSql = @sSql + @sWhereClause+@newLine + @sFootClause


EXEC sp_executesql @sSql,@ParmDefinition,
@SessionID=@SessionID,
@DBID=@DBID,
@StatusID=@StatusID
END

Regards
gleb.zalt
gleb.zalt
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 227
doesn't work on case-sensitive servers
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5963 Visits: 8312
no need to reinvent the wheel. use sp_whoisactive, an absolutely amazing freebie from Adam Machanic. See sqlblog.com for code (which is documented) and 30 different blog posts detailing it's power.

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
ricardo_chicas
ricardo_chicas
SSC-Enthusiastic
SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)

Group: General Forum Members
Points: 198 Visits: 676
mmm sp_whoisactive is quite nice, but my last version of sp_who3 ( the one posted here is one year old) has some more useful options
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5963 Visits: 8312
Can you please share with us what those "more useful options" might be please?

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
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