?? 恢復指定目錄下的所有數據庫.sql
字號:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_RestoreDb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_RestoreDb]
GO
/*--恢復指定目錄下的所有數據庫
恢復的數據庫名為備份文件名(不含擴展名)
備份文件的擴展名固定為.bak
--鄒建 2003.10(引用請保留此信息)--*/
/*--調用示例
--恢復指定目錄下的所有數據庫
exec p_RestoreDb @bkpath='c:\'
--恢復指定目錄下的指定數據庫
exec p_RestoreDb @bkpath='c:\',@bkfile='客戶資料,xzkh_new'
--*/
create proc p_RestoreDb
@bkpath nvarchar(1000)='', --定義備份文件的存放目錄,默認為SQL的備份目錄
@bkfile nvarchar(4000)='', --定義要恢復的備份文件名,不含擴展名
@dbpath nvarchar(260)='', --恢復后的數據庫存放目錄,不指定則為SQL的默認數據目錄
@overexist bit=1, --是否覆蓋已經存在的數據庫,僅@retype為'DB'/'DBNOR'是有效
@killuser bit=1 --是否關閉用戶使用進程,僅@overexist=1時有效
as
declare @sql varchar(8000),@dbname sysname
if isnull(@bkpath,'')=''
begin
select @bkpath=rtrim(reverse(filename)) from master..sysfiles where name='master'
select @bkpath=substring(@bkpath,charindex('\',@bkpath)+1,4000)
,@bkpath=reverse(substring(@bkpath,charindex('\',@bkpath),4000))+'BACKUP\'
end
else if right(@bkpath,1)<>'\' set @bkpath=@bkpath+'\'
--得到恢復后的數據庫存放目錄
if isnull(@dbpath,'')=''
begin
select @dbpath=rtrim(reverse(filename)) from master..sysfiles where name='master'
select @dbpath=reverse(substring(@dbpath,charindex('\',@dbpath),4000))
end
else if right(@dbpath,1)<>'\' set @dbpath=@dbpath+'\'
--得到指定目錄下的所有備份文件
create table #t(fname varchar(260),depth int,isf bit)
insert into #t exec master..xp_dirtree @bkpath,1,1
if isnull(@bkfile,'')=''
declare tb cursor local for select fn=left(fname,patindex('%.bak',fname)-1) from #t
where isf=1 and fname like '%.bak' --取.bak文件
else
begin
set @bkfile=','+replace(@bkfile,',','.bak,')+'.bak,'
declare tb cursor local for select fn=left(fname,patindex('%.bak',fname)-1) from #t
where isf=1 and fname like '%.bak' and @bkfile like '%,'+fname+',%'
end
--恢復數據庫處理
open tb
fetch next from tb into @dbname
while @@fetch_status=0
begin
--生成數據庫恢復語句
set @sql='restore database ['+@dbname
+'] from disk='''+@bkpath+@dbname+'.bak'''
+' with RECOVERY'
+case when @overexist=1 then ',replace' else '' end
--添加移動邏輯文件的處理
--從備份文件中獲取邏輯文件名
declare @lfn nvarchar(128),@tp char(1),@i int
--創建臨時表,保存獲取的信息
create table #tb(ln nvarchar(128),pn nvarchar(260),tp char(1),fgn nvarchar(128),sz numeric(20,0),Msz numeric(20,0))
--從備份文件中獲取信息
insert into #tb exec('restore filelistonly from disk='''+@bkpath+@dbname+'.bak''')
declare #f cursor local for select ln,tp from #tb order by tp
open #f
fetch next from #f into @lfn,@tp
set @i=0
while @@fetch_status=0
begin
select @sql=@sql+',move '''+@lfn+''' to '''+@dbpath+@dbname+cast(@i as varchar)
+case @tp when 'D' then '.mdf''' else '.ldf''' end
,@i=@i+1
fetch next from #f into @lfn,@tp
end
close #f
deallocate #f
drop table #tb
--關閉用戶進程處理
if @overexist=1 and @killuser=1
begin
declare hCForEach cursor for
select s='kill '+cast(spid as varchar) from master..sysprocesses
where dbid=db_id(@dbname)
exec sp_msforeach_worker '?'
end
--恢復數據庫
exec(@sql)
fetch next from tb into @dbname
end
close tb
deallocate tb
go
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -