存储过程:set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
goALTER proc [dbo].[resetDatabase](@dbname varchar(20),@path varchar(100)) 
as
begin
declare @sql nvarchar(500) 
declare @spid int--SPID   值是当用户进行连接时指派给该连接的一个唯一的整数 
set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('' '+@dbname+' '')' 
--sysprocesses   包含有关   SQL   Server   进程的信息。 
exec(@sql) 
open getspid 
fetch next from getspid into @spid 
while @@fetch_status <> -1--如果FETCH   语句没有执行失败或此行不在结果集中。 
begin 
exec('kill '+@spid)--终止正常连接 
fetch next from getspid into @spid 
end
close getspid 
deallocate getspid 
 
restore database @dbname from disk = @path WITH REPLACE /*WITH REPLACE 覆盖日志*/
end 在查询分析器中能执行。但在程序调用时就会出错。
错误:
因为数据库正在使用,所以无法获得对数据库的独占访问权。 RESTORE DATABASE 正在异常终止连接字符串:Data Source=.\SQL2005;Initial Catalog=master;User ID=sa;Password=sqlpass该怎么实现这在线还原功能啊?