• subscribe
February 15, 2005 12:00 AM

Meet sp_who1

A new member of the sp_who family offers rich filters and block detection
SQL Server Pro
InstantDoc ID #45034
Downloads
45034.zip

Panning for Gold
As I mentioned earlier, I included in sp_who1 a feature that I call multiple filter panning, which lets you apply filters in any combination you choose to narrow down the range of returned results you're interested in and exclude the results you don't want. Let's look at how this feature works. Imagine that user John Woo reports that his trading report is stalled; he'd noticed that the report had been running sluggishly since he left the office yesterday. Other traders aren't having any trouble running the report, so you run sp_who1 @blocked = 1, but find no existing blocks. You decide to take a look at the trading database, and you try to limit your search to information related to John Woo's report by combining the following parameters:

sp_who1 @program = 'Trading Report',
   @loginame = 'Boston\Jwoo', @hostname = 'Johnwoo', 
   @dbname = 'trading', @lastbatch = 'Oct 20 2004 8:30:00:000AM'

Your query returns one SPID that meets all your search conditions, as Figure 2 shows. You notice that John last ran the trading report at 8:40 am. The process in question tried to perform an INSERT operation on a table in the trading database in an open transaction. You can see that the waittime is enormous (more than 2 hours) and the lastwaittype shows NETWORKIO. These details suggest a problem in the network connection between John's PC and the SQL Server. It turns out that the network administrator did some rewiring work last night in the server room, and when you ask him to help troubleshoot John's faulty PC connection, the administrator finds a malfunctioning hub port. Once he switches John's cable to another free port, the problem goes away. This is just one scenario in which you can use the power of multiple-filter panning in sp_who1.

Make Yourself Unique
DBAs often need exclusive access to a database. For example, imagine you're trying to restore a database called target_db. You keep getting the error Exclusive access could not be obtained because the database is in use. With sp_who1, you can easily see who is accessing target_db. Run the following statement:

sp_who1 @dbname = 'target_db'

Then, you can use the KILL command to clean up the SPIDs that the statement returns and safely perform the restore of target_db.

Just a Click Away
Nowadays, database professionals are over tasked, and flexible and efficient tools help improve their productivity. Sp_who1 has several unique features that can save a DBA's time and effort. To make the stored procedure quick and easy to use when you need it, I recommend you save in a file a list of sp_who1 statements containing the parameters or combined parameters you use most. Leave this file open in a Query Analyzer session while you work, and you'll be just one click away from investigating abnormalities in your SQL Server databases.



ARTICLE TOOLS

Comments
  • HOWARD
    6 years ago
    Jun 09, 2006

    I downloaded this some time ago & found it to be a very useful tool. I created a file with various statments using sp_who1 as suggested. This has become an important part of my toolbox of utilities.
    Thanks!

  • Tim
    7 years ago
    Apr 01, 2005

    sp_who1 @loginame = [domain\\username] won't work if the user is accessing via an NT group. Comment out the below and all is OK:
    if (@loginame is not null)
    begin
    if not exists (select 1 from master.dbo.syslogins with (nolock) where name = @loginame)
    begin
    raiserror(15007, -1, -1, @loginame)
    return (1)
    end
    end

  • Craig
    7 years ago
    Mar 02, 2005

    Very Nice!

    If you like sp_who1, you should try sp_who_3:

    http://www.sqlservercentral.com/scripts/contributions/1232.asp

    It got like a perfect 5 stars and seems to be more efficient, more powerful, and has even more filtering and even include ad-hoc sorting. Check it out and post back comments.

    This type of procedure has been desperately needed. I think Microsoft should release something like this in a service pack or in SQL 2005. There are plenty of sp_who_x out there on the web for free download.

    Microsoft should just pick the best one and include it.

  • PATRICK
    7 years ago
    Mar 01, 2005

    Excellent article. Thanks for your brains and hard work.

  • flexdba
    7 years ago
    Feb 28, 2005

    Very useful. FYI: The web code download wraps two long comment lines leading to parse errors but these are easily solved by re-joining the long lines. Richard: I thank you very much for creating/sharing this code. Very useful!

You must log on before posting a comment.

Are you a new visitor? Register Here