본문 바로가기

SQL Server/SQL Server Tip & Tech

DMV를 사용하여 sp_who2 + DBCC Inputbuffer(spid) 작업을 한번에 하기

SELECT A.session_id, B.text

FROM sys.dm_exec_connections A

    CROSS APPLY sys.dm_exec_sql_text(most_recent_sql_handle) B



SELECT

       ISNULL(D.text, '') AS SQLStatement,

       A.Session_ID SPID,

       ISNULL(B.status,A.status) AS [Status],

       A.login_name AS [Login],

       ISNULL(A.host_name, '  .') AS HostName,

       ISNULL(CAST(C.BlkBy AS varchar(10)), '  .') AS BlkBy,

       DB_NAME(B.Database_ID) AS DBName,

       B.command,

       ISNULL(B.cpu_time, A.cpu_time) AS CPUTime,

       ISNULL((B.reads + B.writes),(A.reads + A.writes)) AS DiskIO,

       A.last_request_start_time AS LastBatch,

       ISNULL(A.program_name, '') AS ProgramName,

       ISNULL(A.client_interface_name, '') AS ClientInterfaceName

FROM sys.dm_exec_sessions A

       LEFT OUTER JOIN sys.dm_exec_requests B ON A.session_id = B.session_id

       LEFT OUTER JOIN (

             SELECT A.request_session_id SPID, B.blocking_session_id BlkBy

             FROM sys.dm_tran_locks A

                    INNER JOIN sys.dm_os_waiting_tasks B ON A.lock_owner_address = B.resource_address

       ) C ON A.Session_ID = C.SPID

       OUTER APPLY sys.dm_exec_sql_text(B.sql_handle) D