SQL Troubleshooting Queries (2)
Found this MSSQL query on StackOverflow for troubleshooting locks or not being able to access a database being taken offline.. posting for self-reference:
SELECT
DB\_NAME(l.resource\_database\_id) DB,
l.resource\_type,
l.request\_mode,
l.request\_status,
l.request\_session\_id,
r.command,
r.status,
r.blocking\_session\_id,
r.wait\_type,
r.wait\_time,
r.wait\_resource,
request\_sql\_text = st.text,
s.program\_name,
most\_recent\_sql\_text = stc.text
FROM sys.dm\_tran\_locks l
LEFT JOIN sys.dm\_exec\_requests r
ON l.request\_session\_id = r.session\_id
LEFT JOIN sys.dm\_exec\_sessions s
ON l.request\_session\_id = s.session\_id
LEFT JOIN sys.dm\_exec\_connections c
ON s.session\_id = c.session\_id
OUTER APPLY sys.dm\_exec\_sql\_text(r.sql\_handle) st
OUTER APPLY sys.dm\_exec\_sql\_text(c.most\_recent\_sql\_handle) stc
--WHERE l.resource\_database\_id = DB\_ID('[Database Name]')
ORDER BY request\_session\_id;
Some additional helpful links: Lock Modes Wait Types; Additional wait Type Info Locks System Table Info
Also, connection by session:
SELECT \* FROM SYS.DM\_EXEC\_CONNECTIONS WHERE SESSION\_ID = [SESSION ID]
Enjoy Reading This Article?
Here are some more articles you might like to read next: