/*********************************
FINDING OPEN TRANSACTIONS AND KILLING THEM
**********************************/
/*
If you fail to commit a transaction, it'll lock the table. To see which transactions are open, you can use DBCC OPENTRAN (Database Console Commands).
OPENTRAN will show you the oldest open transaction. You can then use the KILL keyword to terminate the transaction (this'll also terminate the connection).
*/
DBCC OPENTRAN;
KILL 52; -- 52 is the server process ID for the transaction as listed on the 'SPID (server process ID):' line of DBCC OPENTRAN
/*********************************
HOW TO SEE WHAT QUERIES ARE BEING SENT TO THE DATABASE
**********************************/
/*
One method is to go to Tools and select SQL Server Profiler. You can then create a trace which will show all code hitting the database, including from other
users and applications.
A second method involves the following query which will show you queries based on session information (doesn't always work?).
*/
SELECT sdest.DatabaseName
,sdes.session_id
,sdes.[host_name]
,sdes.[program_name]
,sdes.client_interface_name
,sdes.login_name
,sdes.login_time
,sdes.nt_domain
,sdes.nt_user_name
,sdec.client_net_address
,sdec.local_net_address
,sdest.ObjName
,sdest.Query
FROM sys.dm_exec_sessions AS sdes
INNER JOIN sys.dm_exec_connections AS sdec ON sdec.session_id = sdes.session_id
CROSS APPLY (
SELECT db_name(dbid) AS DatabaseName
,object_id(objectid) AS ObjName
,ISNULL((
SELECT TEXT AS [processing-instruction(definition)]
FROM sys.dm_exec_sql_text(sdec.most_recent_sql_handle)
FOR XML PATH('')
,TYPE
), '') AS Query
FROM sys.dm_exec_sql_text(sdec.most_recent_sql_handle)
) sdest
where sdes.session_id <> @@SPID