Today I wanted to drop a specific database, but their was one account still connected. I had no idea who was still using the database, so I needed a way to get the list of open database connections.
There are 2 commands that helped me out here:
- sp_who
- sp_who2
The sp_who internal procedure allows users to view current activity on the database. This command provides a view into several system tables (e.g., syslocks, sysprocesses, etc.). The sp_who command returns the following information:
- SpidāThe system process ID.
- statusāThe status of the process (e.g., RUNNABLE, SLEEPING).
- loginameāLogin name of the user.
- hostnameāMachine name of the user.
- blkāIf the process is getting blocked, this value is the SPID of the blocking process.
- dbnameāName of database the process is using.
- CmdāThe command currently being executed (e.g., SELECT, INSERT)
- CPUTimeāTotal CPU time the process has taken.
- DiskIOāTotal amount of disk reads for the process.
- LastBatchāLast time a client called a procedure or executed a query.
- ProgramNameāApplication that has initiated the connection (e.g., Visual Basic, MS SQL Query Analyzer)