sql処理データベースロックの保存プロセスを共有する


鄒建2004.4
 
/*--
exec p_lockinfo1
--*/
alter proc p_lockinfo1
@kill_lock_spid bit=1, -- ,1 , 0
@show_spid_if_nolock bit=1 -- , ,1 ,0
as
declare @count int,@s nvarchar(max),@i int
select id=identity(int,1,1), ,
ID=spid, ID=kpid, ID=blocked, ID=dbid,
=db_name(dbid), ID=uid, =loginame, CPU =cpu,
=login_time, =open_tran, =status,
=hostname, =program_name, ID=hostprocess,
=nt_domain, =net_address
into #t from(
select =' ',
spid,kpid,a.blocked,dbid,uid,loginame,cpu,login_time,open_tran,
status,hostname,program_name,hostprocess,nt_domain,net_address,
s1=a.spid,s2=0
from master..sysprocesses a join (
select blocked from master..sysprocesses group by blocked
)b on a.spid=b.blocked where a.blocked=0
union all
select '|_ _>',
spid,kpid,blocked,dbid,uid,loginame,cpu,login_time,open_tran,
status,hostname,program_name,hostprocess,nt_domain,net_address,
s1=blocked,s2=1
from master..sysprocesses a where blocked<>0
)a order by s1,s2

select @count=@@rowcount,@i=1

if @count=0 and @show_spid_if_nolock=1
begin
insert #t
select =' ',
spid,kpid,blocked,dbid,db_name(dbid),uid,loginame,cpu,login_time,
open_tran,status,hostname,program_name,hostprocess,nt_domain,net_address
from master..sysprocesses
set @count=@@rowcount
end

if @count>0
begin
create table #t1(id int identity(1,1),a nvarchar(max),b Int,EventInfo nvarchar(max))
if @kill_lock_spid=1
begin
declare @spid varchar(max),@ varchar(max)
while @i<=@count
begin
select @spid= ID,@ = from #t where id=@i
insert #t1 exec('dbcc inputbuffer('+@spid+')')
if @ =' ' exec('kill '+@spid)
set @i=@i+1
end
end
else
while @i<=@count
begin
select @s='dbcc inputbuffer('+cast( ID as varchar(max))+')' from #t where id=@i
insert #t1 exec(@s)
set @i=@i+1
end
select a.*, SQL =b.EventInfo
from #t a join #t1 b on a.id=b.id
end
go