A better sp_who2 using dmvs (sp_who3)

Source: Internet
Author: User

The following code generates the same information found in sp_who2, along with some additional troubleshooting information. it also contains the SQL statement being run, so instead of having to execute a separate DBCC inputbuffer, the statement being executed is shown in the results.

Unlike sp_who2, sp_who3 only shows sessions that have a current executing request.

What is also shown is the reads and writes for the current command, along with the number of reads and writes for the entire spid. it also shows the protocol being used (TCP, namedpipes, or shared memory ).

Create Procedure Sp_who3
(
@ SessionidInt =Null
)
As
Begin
Select
Spid = ER. session_id
, Status = SES. Status
, [Login] = SES. login_name
, Host = SES. host_name
, Blkby = ER. blocking_session_id
, Dbname = db_name (ER. database_id)
, Commandtype = ER. Command
, Sqlstatement = ST. Text
, Objectname = object_name (St. objectid)
, Elapsedms = ER. total_elapsed_time
, Cputime = ER. cpu_time
, Ioreads = ER. logical_reads + Er. reads
, Iowrites = ER. Writes
, Lastwaittype = ER. last_wait_type
, Starttime = ER. start_time
, Protocol = con.net _ transport
, Connectionwrites = con. num_writes
, Connectionreads = con. num_reads
, Clientaddress = con. client_net_address
, Authentication = con. auth_scheme
From SYS. dm_exec_requests er
Outer Apply SYS. dm_exec_ SQL _text (ER. SQL _handle) ST
Left Join SYS. dm_exec_sessions ses
On SES. session_id = ER. session_id
Left Join SYS. dm_exec_connections con
On Con. session_id = SES. session_id
Where Er. session_id> 50
And @ SessionidIs Null Or Er. session_id = @ sessionid
Order By
Er. blocking_session_idDesc
, Er. session_id
  
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.