Find Long-Open Transactions in SQL Server
Lists long-open transactions with their owners. Detects those blocking log truncation and holding locks.
SELECT s.session_id, s.login_name, s.host_name, s.status,
DB_NAME(t.database_id) AS db_name,
at.transaction_begin_time,
DATEDIFF(MINUTE, at.transaction_begin_time, GETDATE()) AS open_minutes,
st.text AS last_sql
FROM sys.dm_tran_session_transactions t
JOIN sys.dm_tran_active_transactions at ON at.transaction_id = t.transaction_id
JOIN sys.dm_exec_sessions s ON s.session_id = t.session_id
LEFT JOIN sys.dm_exec_connections c ON c.session_id = s.session_id
OUTER APPLY sys.dm_exec_sql_text(c.most_recent_sql_handle) st
WHERE t.session_id <> @@SPID
ORDER BY at.transaction_begin_time;Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.
- This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
- Prior to executing on production systems under high load, verify the target database version (Min. Version: 2012).
- Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.
Engineering Triad: Related Runbooks & Incident Response
Which production incidents and emergency drills execute this script as a diagnosis step?
SQL Server Blocking Chain: Find and Resolve the Head Blocker
Finds the head blocker, detects the open transaction and ends it safely. Covers RCSI and threshold settings to prevent recurrence.
SQL Server Transaction Log Full (Error 9002): Find the Cause and Fix Safely
Uses log_reuse_wait_desc to find why the log cannot be truncated and fixes it by cause: log backup, open transaction, replica or replication. Avoids the "switch to SIMPLE and shrink" mistake.
SQL Server TempDB Full: Find the Space Consumer and Recover
Distinguishes TempDB file, version store and physical disk exhaustion; provides a safe response order by consumer or growth limit.
Other SQLSERVER Diagnostic Scripts
View All (100)SQL Server Running Requests and Blocking Chain
Shows active requests, wait types, blocking sessions and the running statement together. The first diagnostic query to run during a slowdown.
Top CPU-Consuming Queries in SQL Server
Lists the top 20 queries by total and average CPU time from the plan cache. Pinpoints the source of CPU pressure at query level.
SQL Server Missing Index Recommendations (Missing Index DMVs)
Ranks the optimizer's missing index suggestions by estimated benefit. Merge them with existing indexes before applying.
SQL Server Index Fragmentation with Rebuild/Reorganize Advice
Measures fragmentation on large indexes and recommends rebuild or reorganize. Lets you plan maintenance windows without wasted work.