sql-server的死锁及处理方法.doc

  1. 1、本文档共24页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多

【转】处理sqlserver的死锁

--第一篇

--检测死锁

--如果发生死锁了,我们怎么去检测具体发生死锁的是哪条SQL语句或存储过程?

--这时我们可以使用以下存储过程来检测,就可以查出引起死锁的进程和SQL语句。SQLServer自带的系统存储过程sp_who和sp_lock也可以用来查找阻塞和死锁,但没有这里介绍的方法好用。

?

usemaster

go

createproceduresp_who_lock

as

begin

declare@spidint,@blint,

?@intTransactionCountOnEntry?int,

???????@intRowcount???int,

???????@intCountProperties??int,

???????@intCounter???int

?createtable#tmp_lock_who(

?idintidentity(1,1),

?spidsmallint,

?blsmallint)

?

?IF@@ERROR0RETURN@@ERROR

?

?insertinto#tmp_lock_who(spid,bl)select?0,blocked

??from(select*fromsysprocesseswhere?blocked0)a

??wherenotexists(select*from(select*fromsysprocesseswhere?blocked0)b

??wherea.blocked=spid)

??unionselectspid,blockedfromsysprocesseswhere?blocked0

?IF@@ERROR0RETURN@@ERROR

?

--找到临时表的记录数

?select?@intCountProperties=Count(*),@intCounter=1

?from#tmp_lock_who

?

?IF@@ERROR0RETURN@@ERROR

?

?if@intCountProperties=0

?select现在没有阻塞和死锁信息asmessage

--循环开始

while@intCounter=@intCountProperties

begin

--取第一条记录

?select?@spid=spid,@bl=bl

?from#tmp_lock_whowhereId=@intCounter

?begin

?if@spid=0

???????????select引起数据库死锁的是:+CAST(@blASVARCHAR(10))+进程号,其执行的SQL语法如下

?else

???????????select进程号SPID:+CAST(@spidASVARCHAR(10))+被+进程号SPID:+CAST(@blASVARCHAR(10))+阻塞,其当前进程执行的SQL语法如下

?DBCCINPUTBUFFER(@bl)

?end

--循环指针下移

?set@intCounter=@intCounter+1

end

droptable#tmp_lock_who

return0

end

?

--杀死锁和进程

--如何去手动的杀死进程和锁?最简单的办法,重新启动服务。但是这里要介绍一个存储过程,通过显式的调用,可以杀死进程和锁。

usemaster

go

ifexists(select*fromdbo.sysobjectswhereid=object_id(N[dbo].[p_killspid])andOBJECTPROPERTY(id,NIsProcedure)=1)

dropprocedure[dbo].[p_killspid]

GO

createprocp_killspid

@dbnamevarchar(200)???--要关闭进程的数据库名

as?

???declare@sql?nvarchar(500)?

???declare@spidnvarchar(20)

???declare#tbcursorfor

???????selectspid=cast(spidasvarchar(20))frommaster..sysprocesseswheredbid=db_id(@dbname)

???open#tb

???fetchnextfr

文档评论(0)

A~下一站守候 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档