SQL Server: Filter output of sp_who2

后端 未结 13 2089
长情又很酷
长情又很酷 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:36

    Similar to KyleMit answer, its possible to select directly the tables used by SP_WHO2, although I think it's only need dbo.sysprocesses table.

    If someone open this SP, it can understand what it does. This is my best select to have a similar output as SP_WHO2

    select convert(char(5),sp.spid) as SPID
            ,  CASE lower(sp.status)
                     When 'sleeping' Then lower(sp.status)
                     Else  upper(sp.status)
                  END as Status
            , convert(sysname, rtrim(sp.loginame)) as LOGIN
            , CASE sp.hostname
                     When Null  Then '  .'
                     When ' ' Then '  .'
                     Else    rtrim(sp.hostname)
                  END as HostName
            , CASE isnull(convert(char(5),sp.blocked),'0')
                     When '0' Then '  .'
                     Else isnull(convert(char(5),sp.blocked),'0')
                  END as BlkBy
            , case when sp.dbid = 0 then null when sp.dbid <> 0 then db_name(sp.dbid) end as DBName
            , sp.cmd as Command
            , sp.cpu as CPUTime
            , sp.physical_io as DiskIO
            , sp.last_batch as LastBatch
            , sp.program_name as ProgramName 
            from master.dbo.sysprocesses sp (nolock)
      ;
    

    Over this select, you can select the fields you need and have the order you want.

提交回复
热议问题