If you haven’t used sp_who2 before then it is a great utility to help in diagnosing a problem with your database application.
What is sp_who2 in SQL Server and what is it used for ?
It’s a stored procedure which is installed with SQL Server which when run, outputs a row for each “SPID”. SPID stands for Server Process ID and one is created every time an application needs to open a connection to SQL Server.
Each row contains a number of useful columns including information on things like the status of the SPID, what its resource usage is in terms of CPU/IO and what login is currently executing the command.
What permissions are needed for sp_who2 ?
A login can run sp_who2 and obtain information about its own connection. For a full list of SPID’s, either the login has to have sysadmin permission or VIEW SERVER STATE permission.
Using sp_who2 to help identify blocking queries
Lets say for example that the phone rings and everyone in the department using the sales system is complaining that their copy of the application is freezing when trying to access the customer sales order data.
So for this blocking demo, I have created a simple table in my database called “Orders” and I am going to start a transaction and leave it open without committing or rolling it back.
BEGIN TRAN INSERT INTO Orders(AccountID, DatePlaced, Amount, Currency) VALUES(1, GETDATE(), 100, '$');
Now I will try and read the orders table via another query
SELECT * FROM Orders;
and again via another query…
SELECT * FROM Orders;
Now I will run exec sp_who2 to check what is happening to these connections and it tells me that I have two blocked SPID’s (55 and 56) and that they are being blocked by SPID 54
The DBA now has to decide how best to proceed with resolving this problem based on the cause and effect of taking action.
They will try and work out what the lead blocker is doing and one way they might do this would be to use “DBCC INPUTBUFFER(SPID)”.
In this event, the SPID is the number of the connection to be analysed and the results returned tell the DBA what the command/query was executing as that SPID.
Instead of using sp_who2, another way would be to look at the dm_exec_requests DMV. I’m going to look at these in a future post.
In my demo it is easy to resolve this as I can kill the connection, force a commit or force a rollback and it will be instantly resolved.
It might not be as clear cut as this in another scenario. Lets say that a process has been running for some time with a large update and this causes the blocking problem described in the demo.
When the DBA views the results of sp_who2 and notices that the process has been running for many hours, they know that by killing the process, a rollback has to finish before the resource will become available again for other queries to be able to complete.
The rollback could also take many hours in this instance and so such decisions cannot be taken lightly. The solution will vary depending on the scenario.
With this tool, you can check a specific connection. It takes an optional parameter SPID, for example:
exec sp_who2 54
How can you filter and order the results of sp_who2 ?
Something I get asked is whether there is a way to check for active connections by running a filter or specifying an order by – something like exec sp_who2 ‘active’. Sadly this doesn’t work but there is a simple way and that is to capture the results to a temporary table or temporary variable and then query that.
This piece of code is creating a table in memory and inserting the results into it. You can then filter it or order it how you want.
DECLARE @sp_who_output TABLE( SPID INT, Status VARCHAR(MAX), LOGIN VARCHAR(MAX), HostName VARCHAR(MAX), BlkBy VARCHAR(MAX), DBName VARCHAR(MAX), Command VARCHAR(MAX), CPUTime INT, DiskIO INT, LastBatch VARCHAR(MAX), ProgramName VARCHAR(MAX), SPID_1 INT, REQUESTID INT ) INSERT INTO @sp_who_output EXEC sp_who2 SELECT * FROM @sp_who_output WHERE BlkBy IS NOT NULL;
You could take this a step further and create a stored procedure of your own passing in the columns you wanted to filter on and the order by options. Then commands such as “sp_who2 blkby”, “sp_who2 active” etc start to become possible.
What is the difference between sp_who and sp_who2 ?
The main difference is that sp_who displays fewer columns that sp_who2.
You will find that using either that the same info is found in sp_who2 as in sp_who. However with sp_who2, there are more columns available but not so many where you are overwhelmed. It is simply better.
I personally never use sp_who.
You may also notice that in searching for official Microsoft documentation about sp_who2, that you can’t find any. That is because sp_who2 is undocumented and but sp_who is documented.
sp_who2 should be part of every DBA’s troubleshooting toolbox.
It provides a great overview of what the connections are doing on the SQL Server and can quickly help the DBA find reasons for increases in application timeouts, high disk IO or high CPU pressure.