SQL Server: Filter output of sp_who2

后端 未结 13 2080
长情又很酷
长情又很酷 2020-12-02 03:49

Under SQL Server, is there an easy way to filter the output of sp_who2? Say I wanted to just show rows for a certain database, for example.

13条回答
  •  一整个雨季
    2020-12-02 04:19

    You could save the results into a temp table, but it would be even better to go directly to the source on master.dbo.sysprocesses.

    Here's a query that will return almost the exact same result as sp_who2:

    SELECT  spid,
            sp.[status],
            loginame [Login],
            hostname, 
            blocked BlkBy,
            sd.name DBName, 
            cmd Command,
            cpu CPUTime,
            physical_io DiskIO,
            last_batch LastBatch,
            [program_name] ProgramName   
    FROM master.dbo.sysprocesses sp 
    JOIN master.dbo.sysdatabases sd ON sp.dbid = sd.dbid
    ORDER BY spid 
    

    Now you can easily add any ORDER BY or WHERE clauses you like to get meaningful output.


    Alternatively, you might consider using Activity Monitor in SSMS (Ctrl + Alt + A) as well

提交回复
热议问题