还原数据库出现“未获得排他訪问”解决方法(杀死数据库连接的存储过程sqlserver)
在master数据库下创建存储步骤例如以下:
createproc killspid
(@dbnamevarchar(20))
as
begin
declare@sqlnvarchar(500)
declare@spidint
set@sql='declare getspid cursor for select spid from sysprocesses where dbid in (select dbid from sysdatabases where name=' +@dbname+' )'
exec(@sql)
open getspid
fetch next from getspid into@spid
while @@fetch_status<>-1
begin
exec('kill '+@spid)
fetch next from getspid into@spid
end
close getspid
deallocate getspid
end
--使用方法
as
begin
declare@sqlnvarchar(500)
declare@spidint
set@sql='declare getspid cursor for select spid from sysprocesses where dbid in (select dbid from sysdatabases where name=' +@dbname+' )'
exec(@sql)
open getspid
fetch next from getspid into@spid
while @@fetch_status<>-1
begin
exec('kill '+@spid)
fetch next from getspid into@spid
end
close getspid
deallocate getspid
end
--使用方法
use master
exec killspid '''数据库名'''
单引號嵌套
用三个单引號的作用,最外层的单引號表示vchar。第二个单引號是转义。最里面的单引號是字符串中的单引號。declare @dbname nvarchar(500)
set @dbname='''zcc''' --得到的变量为'zcc'。
set @dbname='zcc' --得到的变量是zcc
posted on 2017-07-14 15:15 cynchanpin 阅读(174) 评论(0) 编辑 收藏 举报