TSQL to findout blocking and locks on a SQL Server?
Have you ever performed huge operations such as deleting records of a table and processing inserts on that table at the same time?
This is a common task that every application will have to perform and you can avoid by fine tuning your queries (mostly deletes). As you are aware the inserts operations will have to wait to update the table where the table is locked until the delete transaction is commited. Similarly you can run a TSQL to find out what kind of blocking and locking is occuring on the server, here is the TSQL:
SELECT t1.request_session_id as spid, t1.resource_type as type, t1.resource_database_id as dbid,
(case resource_type WHEN 'OBJECT' THEN object_name(t1.resource_associated_entity_id) WHEN 'DATABASE' THEN ' '
ELSE
(SELECT object_name(object_id) FROM sys.partitions WHERE hobt_id=resource_associated_entity_id)
END) AS objname,
t1.resource_description as description, t1.request_mode as mode, t1.request_status as status, t2.blocking_session_id
FROM sys.dm_tran_locks AS t1 LEFT OUTER JOIN sys.dm_os_waiting_tasks AS t2
ON t1.lock_owner_address = t2.resource_address
**__________________________________**
SQL Server MVP, Sr. DBA & industry expert.
-
Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it. It is also a power and you will gain by sharing it.