create  proc  killspid  (@dbname  varchar(20))  
as  
begin  
declare  @sql  nvarchar(500)  
declare  @spid  int  
set  @sql='declare  getspid  cursor  for    
select  spid  from  sysprocesses  where  dbid=db_id('''+@dbname+''')'  
exec  (@sql)  
open  getspid  
fetch  next  from  getspid  into  @spid  
while  @@fetch_status  <  >-1  
begin  
exec('kill  '+rtrim(@spid))  
fetch  next  from  getspid  into  @spid  
end  
close  getspid  
deallocate  getspid  
end  --用法  
use  master  
exec  killspid  '数据库名'

解决方案 »

  1.   

    select  spid  from  sysprocesses  
    中是所有的存关于运行在 Microsoft&reg; SQL Server&#8482; 上的进程的信息
    CrazyFor(太阳下山明朝依旧爬上来) 的方法回KILL没死锁的进程
      

  2.   

    select  blocked from  sysprocesses  where blocked >0
    表示引起LOCK的进程号
    KILL次进程号即可
      

  3.   

    修改如下
    create  proc  killspid  (@dbname  varchar(20))  
    as  
    begin  
    declare  @sql  nvarchar(500)  
    declare  @spid  int  
    set  @sql='declare  getspid  cursor  for    
    select  blocked  from  sysprocesses  where  dbid=db_id('''+@dbname+''') and blocked  >0'  
    exec  (@sql)  
    open  getspid  
    fetch  next  from  getspid  into  @spid  
    while  @@fetch_status  <  >-1  
    begin  
    exec('kill  '+rtrim(@spid))  
    fetch  next  from  getspid  into  @spid  
    end  
    close  getspid  
    deallocate  getspid  
    end  --用法  
    use  master  
    exec  killspid  '数据库名'
      

  4.   

    谢谢各位朋友。
    不过我想解决的问题是表级的死锁,而不是数据库一级的??
    直接kill会造成数据的丢失吗?