--查询分析器中,用下面的代码来处理/*
断开所有用户打开的连接
*/
use master
goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_killspid]
GOcreate proc p_killspid
@dbname sysname --要关闭进程的数据库名
as  
declare @s nvarchar(1000)
declare tb cursor for local
select s='kill '+cast(spid as varchar)
from master..sysprocesses 
where dbid=db_id(@dbname)open tb 
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
go--用法  
exec p_killspid  'hj'--恢复数据库.
RESTORE  DATABASE  hj  FROM  disk='D:\hj.dat'

解决方案 »

  1.   

    数据库在用的时候当然不能强制恢复了,要么置成单用户.企业管理器-->右建实例-->属性-->选项-->限制访问-->只读
      

  2.   

    p_killspid 上面是一个存储过程吗?我应该怎样使用呢?
      

  3.   

    在查询分析器中执行后出现以下错误:服务器: 消息 170,级别 15,状态 1,过程 p_killspid,行 6
    第 6 行: 'local' 附近有语法错误。
      

  4.   

    --写反了.
    --查询分析器中,用下面的代码来处理/*
    断开所有用户打开的连接
    */
    use master
    goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_killspid]
    GOcreate proc p_killspid
    @dbname sysname --要关闭进程的数据库名
    as  
    declare @s nvarchar(1000)
    declare tb cursor local for
    select s='kill '+cast(spid as varchar)
    from master..sysprocesses 
    where dbid=db_id(@dbname)open tb 
    fetch next from tb into @s
    while @@fetch_status=0
    begin
    exec(@s)
    fetch next from tb into @s
    end
    close tb
    deallocate tb
    go--用法  
    exec p_killspid  'hj'--恢复数据库.
    RESTORE  DATABASE  hj  FROM  disk='D:\hj.dat'