restore database 数据库名
from disk='x:\path\备份文件名'
with replace  --强制替换已经存在的数据库,条件是要替换的数据库没有被使用.
为了断开对要替换的数据库的连接,你可以用下面的存储过程来关闭用户连接./*
关闭用户打开的进程处理
*/
create  proc  killspid  (@dbname  varchar(20))  
as  
begin  
declare  @sql  nvarchar(500)  
declare  @spid  int  
set  @sql='declare  getspid  cursor  for    
select  spid  from  sysprocesses  where  dbid=db_id('''+@dbname+''')'  
exec  (@sql)  
open  getspid  
fetch  next  from  getspid  into  @spid  
while  @@fetch_status  <  >-1  
begin  
exec('kill  '+rtrim(@spid))  
fetch  next  from  getspid  into  @spid  
end  
close  getspid  
deallocate  getspid  
end  --用法  
use  master  
exec  killspid  '数据库名'

解决方案 »

  1.   

    create  proc  killspid  (@dbname  varchar(20))  
    as  
    begin  
    declare  @sql  nvarchar(500)  
    declare  @spid  int  
    set  @sql='declare  getspid  cursor  for    
    select  spid  from  sysprocesses  where  dbid=db_id('''+@dbname+''')'  
    exec  (@sql)  
    open  getspid  
    fetch  next  from  getspid  into  @spid  
    while  @@fetch_status  <  >-1  
    begin  
    exec('kill  '+rtrim(@spid))  
    fetch  next  from  getspid  into  @spid  
    end  
    close  getspid  
    deallocate  getspid  
    end  --用法  
    use  master  
    exec  killspid  '数据库名'
    这种方法是不是有点限制啊
    如果给予的用户权限不允许操作master数据库怎么办?