Below is an updated version of sp_who2 which I called sp_who3. There are separate versions for SQL 7, SQL 2000 and SQL 2005. There are actually two procedures created. sp_blocked and sp_who3. sp_blocked is used by sp_who3.
sp_who3 accepts three input parameters (while sp_who2 supports two parameters). Like sp_who2, sp_who3 supports passing a specific spid and the word active. It also supports the work blocked. Passing the work blocked will return a list of all blocking spids and all blocked spids. This will assist in tracing back a blocking tree to find the start of the blocking. Passing the active parameter will return the same data as sp_who2 with the active parameter.
When you run sp_who3 with a spid as the parameter 6 or 7 second sets will be returned. The first is the output from sp_who. The second is the output from sp_who2. The third is the input buffer (first 255 characters of the last command run). The Fourth will not always appear. When it does it will show you the FULL text of the T/SQL command that is being executed (it doesn't appear in SQL 2005 as Microsoft removed the function that returns this data, as soon as I find a work around I'll post an update). The Fifth is the information about the spid in the sysprocesses table. The sixth will be the information from the sysprocess table about all spids being blocked by this spid (if no processes are being blocked a message to this extent will be displayed). The seventh record set will be the lock information for the requested process.For SQL 2005 where is says sysprocesses it is actually pulling from sys.sysprocesses.
Each version grants execute rights to all users via the public role in the master database (last couple of lines of the script). If you do not want users to be able to execute this command remove the GRANT statement.
In SQL 2005 if users have the right to run sp_who3 because of default SQL Server 2005 security they will only be able to see information about their current process. In order for them to be able to view information about all the processes grant their login the VIEW SERVER STATE right using this command.
[code SQL 2005]
[color blue]GRANT[/color] [color blue]VIEW[/color] SERVER STATE [color blue]TO[/color] [[color blue]DOMAIN[/color]] [color blue]AS[/color] [sa]
[color blue]GO[/color]
[/code]
/*
Date Creator Action
2007.02.15 mrdenny Birth
2007.05.18 mrdenny Correct Full Query Text
2007.10.08 mrdenny Added Waiting Statement to Full Query RecordSet
*/
DECLARE @spid_i INT
DECLARE @spid_only bit
SET NOCOUNT ON
if @spid is null
BEGIN
exec sp_who2
END
ELSE
BEGIN
set @spid_only = 1
if lower(cast(@spid as varchar(10))) = 'active'
BEGIN
set @spid_only = 0
exec sp_who2 'active'
END
if lower(cast(@spid as varchar(10))) = 'blocked' or (isnumeric(@spid) = 1 and @spid < 0)
BEGIN
DECLARE @blocked TABLE
(spid int,
blocked int)
INSERT INTO @blocked
select spid, blocked
from sys.sysprocesses
where blocked <> 0
insert into @blocked
select spid, blocked
from sys.sysprocesses
where spid in (select blocked from @blocked)
set @spid_only = 0
select sys.sysprocesses.spid as 'SPID',
sys.sysprocesses.status,
sys.sysprocesses.loginame as 'Login',
sys.sysprocesses.hostname as 'HostName',
sys.sysprocesses.blocked as 'BlkBy',
sys.databases.name as 'DBName',
sys.sysprocesses.cmd as 'Command',
sys.sysprocesses.cpu as 'CPUTime',
sys.sysprocesses.physical_io as 'DiskIO',
sys.sysprocesses.last_batch as 'LastBatch',
sys.sysprocesses.program_name as 'ProgramName',
sys.sysprocesses.spid as 'SPID'
from sys.sysprocesses
left outer join sys.databases on sys.sysprocesses.dbid = sys.databases.database_id
where spid in (select spid from @blocked)
END
if @spid_only = 1
BEGIN
DECLARE @sql_handle varbinary(64)
DECLARE @stmt_start int
DECLARE @stmt_end int
set @spid_i = @spid
SELECT @sql_handle = sql_handle,
@stmt_start = stmt_start,
@stmt_end = stmt_end
from sys.sysprocesses
where spid = @spid_i
exec sp_who @spid_i
exec sp_who2 @spid_i
dbcc inputbuffer (@spid_i)
/*Start Get Output Buffer*/
select text as 'Full Query',
case when @stmt_start < 0 then
substring(text, @stmt_start/2, (@stmt_end/2)-(@stmt_start/2))
else
null
end as 'Current Command'
from sys.dm_exec_sql_text(@sql_handle)
/*End Get Output Buffer*/
select * from master.sys.sysprocesses where spid = @spid_i
exec sp_blocked @spid_i
exec sp_lock @spid_i
END
END[/code]
[img http://www.mrdenny.com/marker/sp_who3.jpg]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.