1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 |
if exists( select
* from master.sys.sysprocesses where
spid in ( select blocked from
master.sys.sysprocesses)) begin ---确定进程被锁住的其他资源 select spid 进程,STATUS 状态, 登录帐号= SUBSTRING (SUSER_SNAME(sid),1,30) ,用户机器名称= SUBSTRING (hostname,1,12) ,是否被锁住= convert ( char (3),blocked) ,数据库名称= SUBSTRING (db_name(dbid),1,20),cmd 命令,waittype as
等待类型 ,last_batch 最后批处理时间,open_tran 未提交事务的数量 from master.sys.sysprocesses --列出锁住别人(在别的进程中blocked字段中出现的值)但自己未被锁住(blocked=0) Where spid in ( select blocked from
master.sys.sysprocesses) and
blocked=0 end else begin select
‘没有被锁住的进程‘ end select
t1.resource_type [资源锁定类型],DB_NAME(resource_database_id) as
数据库名 ,t1.resource_associated_entity_id 锁定对象,t1.request_mode as
等待者请求的锁定模式 ,t1.request_session_id 等待者SID ,t2.wait_duration_ms 等待时间 ,( select
TEXT from sys.dm_exec_requests r cross
apply sys.dm_exec_sql_text(r.sql_handle) where
r.session_id=t1.request_session_id) as
等待者要执行的SQL ,( select
SUBSTRING (qt.text,r.statement_start_offset/2+1, ( case
when r.statement_end_offset=-1 then
DATALENGTH(qt.text) else
r.statement_end_offset end
-r.statement_start_offset)/2+1 ) from sys.dm_exec_requests r cross
apply sys.dm_exec_sql_text(r.sql_handle)qt where r.session_id=t1.request_session_id) 等待者正要执行的语句 ,t2.blocking_session_id [锁定者SID] ,( select
TEXT from sys.sysprocesses p cross
apply sys.dm_exec_sql_text(p.sql_handle) where p.spid=t2.blocking_session_id ) 锁定者执行语句 from sys.dm_tran_locks t1,sys.dm_os_waiting_tasks t2 where t1.lock_owner_address=t2.resource_address |
原文地址:http://www.cnblogs.com/junko/p/3726230.html