SQL Server: Different Ways to find SPID


There are many ways to know the Server Process id or now also known as Session ID (SPID). The most common methods are as follows:

1. Use global variable @@SPID

SELECT @@SPID as spid
sql-global-ssid

2. Use Properties window

While working in the Query Analyzer, press F4. A Property Window opens and you can see SPID in the last row

SQL Server SPID

3. Use System procedure sp_who

EXEC sp_who

sql-spwho

The above will list out all the processes with spid. The last spid is the one related
to your session.

4. Use sysprocesses view

select spid from sys.sysprocesses

This query will list out all the processes with spid. The last spid is the one related
to your session.

3 comments:

  1. I got this for your 4th method

    SELECT @@SPID
    Gets 65

    Then the

    select spid from sys.sysprocesses

    The last was 78 :)

    ReplyDelete
  2. Looks like it is the second last row that has the SSID. I am not sure though.

    Madhivanan, any inputs?

    ReplyDelete
  3. I think it may not gaurantee to give the id in the order we expect. sysprocesses will have the spid for the current session

    ReplyDelete