在页面实现数据还原,在终止数据库进程时,报不能用kill来终结自己的进程

这是终止进程的存储过程
use master
go
---------------------------------------
--关闭指定数据库的全部访问进程
---------------------------------------------
create proc killspid
@dbname varchar(200) --要关闭进程的数据库名
as
declare @sql nvarchar(500)
declare @spid nvarchar(20)
declare #tb cursor for
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
open #tb
fetch next from #tb into @spid
while @@fetch_status=0
begin
exec('kill '+@spid)
fetch next from #tb into @spid
end
close #tb
deallocate #tb
go

 

看你的脚本的意思是:
在master数据库建存储过程:killspid,
如果你的目的也是这样,那么执行执行还原的sql语句应该是这样:
use masrer
go
exec killspid 'dbname'--dbname改成要还原的数据库名称
go
--在这里写RESTORE 语句还原你的数据库
go

如果你的意图不是上面的,而是在要还原的数据上面创建killspid存储过程,估计你将无完达到你的目的,因为你要执行killspid时,执行killspid的进程也在你要kill的进程列表里面,就出现了你所说的情况:不能用kill来终结自己的进程;

建议直接用sql语句而不是存储过程:
use master    --须使用use master
go

declare @dbname nvarchar(500)
set @dbname ='dbname'  --dbname改成要还原的数据库名称
declare @spid nvarchar(20)
declare #tb cursor for
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
open #tb
fetch next from #tb into @spid
while @@fetch_status=0
begin
exec('kill '+@spid)
fetch next from #tb into @spid
end
close #tb
deallocate #tb
go

posted @ 2011-01-03 22:47  scgw  阅读(1577)  评论(0编辑  收藏  举报