declare @dbname varchar(20) --数据库名称 declare @filename varchar(200) --数据库备份文件 路径 set @dbname='MyTest' set @filename='D:MyTest.bak' --关闭现有数据库连接 declare @i int declare cur cursor for select spid from sysprocesses where db_name(dbid)= @dbname open cur fetch next from cur into @i while @@fetch_status=0 begin exec('kill '+@i) fetch next from cur into @i end close cur deallocate cur --已关闭数据库连接 --还原数据库 RESTORE DATABASE @dbname FROM DISK=@filename WITH replace --还原数据库完毕