sql server 查询某个表一直显示"正在执行中..."的问题

问题描述:只是单纯的执行了"select count(*) from 某表";数据表中只有一两条数据,能查询其他表,唯独这个表不能进行任何操作;

经百度搜索实验,发现应该是某个进程阻塞了,使用了一下代码进行的查询(这个比较详细):

 1 declare @spid int,@bl int  
 2 DECLARE s_cur CURSOR FOR   select  0 ,blocked   from (select * from sys.sysprocesses where  blocked>0 ) a  
 3 where not exists(select * from (select * from sys.sysprocesses where  blocked>0 ) b   where a.blocked=spid)   
 4 union 
 5 select spid,blocked from sys.sysprocesses where  blocked>0   OPEN s_cur   FETCH NEXT FROM s_cur INTO @spid,@bl   WHILE @@FETCH_STATUS = 0  
 6 begin   if @spid =0             
 7 select ' 引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + ' 进程号, 其执行的SQL 语法如下'   else              
 8 select ' 进程号SPID :'+ CAST(@spid AS VARCHAR(10))+ '' + ' 进程号SPID :'+ CAST(@bl AS VARCHAR(10)) +' 阻塞, 其当前进程执行的SQL 语法如下' 
 9 DBCC INPUTBUFFER (@bl )   FETCH NEXT FROM s_cur INTO @spid,@bl   end   CLOSE s_cur  
10 DEALLOCATE s_cur 

还有其他的语句也都可以证明有进程被阻塞了,只不过我是初学者,看不懂:

1 use master
2 go
3 --检索死锁进程
4 select spid, blocked, loginame, last_batch, status, cmd, hostname, program_name
5 from sysprocesses
6 where spid in
7 ( select blocked from sysprocesses where blocked <> 0 ) or (blocked <>0)

或者

1 select * from sys.sysprocesses where blocked<>0

解决办法:只需要杀死阻塞进程就可以了:

1 select spid, blocked, loginame, last_batch, status, cmd, hostname, program_name
2 from sysprocesses
3 where spid in
4 ( select blocked from sysprocesses where blocked <> 0 ) or (blocked <>0)
5 
6 kill spid(进程号)

 

posted @ 2018-03-27 17:47  单纯的桃子  阅读(13171)  评论(1编辑  收藏  举报