Saturday, September 3, 2011

Sql Server: Quering Locks



SELECT
    lock.request_session_id AS "Session Id"
    , task.blocking_session_id AS "Blocked By"
    , DB_NAME(lock.resource_database_id) AS "DB Name"
    , obj.[type] AS "Object Type"
    , SCHEMA_NAME(obj.schema_id) + '.' + obj.Name AS "Object Name"
    , lock.resource_type AS "Resource Type"
    , lock.resource_description AS "Resource Id"
    , lock.request_mode AS "Lock Type"
    , lock.request_status AS "Lock Status"
    , lock.request_reference_count AS "Ref Count"
    , stmt.text AS "Sql Statement"
    , ssntran.is_user_transaction as "User Transaction"
    , acttran.name as "Transaction Name"
    , '***' AS '***'
    , ssn.login_name AS "Login"
    , ssn.host_name AS "Host"
    , ssn.program_name AS "Program"
    , ssn.login_time AS "Login Time"
    , conn.net_transport AS "Transport"
    , conn.auth_scheme as "Authentication"
FROM
    sys.dm_tran_locks lock
    LEFT JOIN sys.dm_os_waiting_tasks task ON lock.lock_owner_address = task.resource_address
    JOIN sys.partitions prtn ON prtn.hobt_id = lock.resource_associated_entity_id
    JOIN sys.objects obj ON obj.object_id = prtn.object_id
    JOIN sys.dm_exec_sessions ssn ON ssn.session_id = lock.request_session_id
    JOIN sys.dm_tran_session_transactions ssntran ON ssn.session_id = ssntran.session_id
    JOIN sys.dm_tran_active_transactions acttran ON ssntran.transaction_id = acttran.transaction_id
    JOIN sys.dm_exec_connections conn ON conn.session_id = ssn.session_id
    CROSS APPLY sys.dm_exec_sql_text(conn.most_recent_sql_handle) AS stmt
WHERE resource_database_id = db_id()
ORDER BY lock.request_session_id

No comments:

Post a Comment