再次修正了恢復(fù)過程,加入了殺死相應(yīng)進(jìn)程的功能,使恢復(fù)過程更完善
if exists(
select * from sysobjects
where name='pr_restore_db' and xtype='p'
)
begin
/*存在就刪除*/
drop proc pr_restore_db
end
go
create proc pr_restore_db /*恢復(fù)數(shù)據(jù)庫*/
@flag varchar(20) out, /*過程運行的狀態(tài)標(biāo)志,是輸入?yún)?shù)*/
@restore_db_name nvarchar(128), /*要恢復(fù)的數(shù)據(jù)名字*/
@filename nvarchar(260) /*備份文件存放的路徑+備份文件名字*/
as
declare @proc_result tinyint /*返回系統(tǒng)存儲過程xp_cmdshell運行結(jié)果*/
declare @loop_time smallint /*循環(huán)次數(shù)*/
declare @max_ids smallint /*@tem表的ids列最大數(shù)*/
declare @file_bak_path nvarchar(260) /*原數(shù)據(jù)庫存放路徑*/
declare @flag_file bit /*文件存放標(biāo)志*/
declare @master_path nvarchar(260) /*數(shù)據(jù)庫master文件路徑*/
declare @sql nvarchar(4000),@par nvarchar(1000)
declare @sql_sub nvarchar(4000)
declare @sql_cmd nvarchar(4000)
declare @sql_kill nvarchar(400)
/*
判斷參數(shù)@filename文件格式合法性,以防止用戶輸入類似d: 或者 c:\a\ 等非法文件名
參數(shù)@filename里面必須有'\'并且不以'\'結(jié)尾
*/
if right(@filename,1)<>'\' and charindex('\',@filename)<>0
begin
select @sql_cmd='dir '+@filename
EXEC @proc_result = master..xp_cmdshell @sql_cmd,no_output
IF (@proc_result<>0) /*系統(tǒng)存儲過程xp_cmdshell返回代碼值:0(成功)或1(失?。?/
begin
select @flag='not exist' /*備份文件不存在*/
return /*退出過程*/
end
/*創(chuàng)建臨時表,保存由備份集內(nèi)包含的數(shù)據(jù)庫和日志文件列表組成的結(jié)果集*/
create table #tem(
LogicalName nvarchar(128), /*文件的邏輯名稱*/
PhysicalName nvarchar(260) , /*文件的物理名稱或操作系統(tǒng)名稱*/
Type char(1), /*數(shù)據(jù)文件 (D) 或日志文件 (L)*/
FileGroupName nvarchar(128), /*包含文件的文件組名稱*/
[Size] numeric(20,0), /*當(dāng)前大小(以字節(jié)為單位)*/
[MaxSize] numeric(20,0) /*允許的最大大?。ㄒ宰止?jié)為單位)*/
)
/*
創(chuàng)建表變量,表結(jié)構(gòu)與臨時表基本一樣
就是多了兩列,
列ids(自增編號列),
列file_path,存放文件的路徑
*/
declare @tem table(
ids smallint identity, /*自增編號列*/
LogicalName nvarchar(128),
PhysicalName nvarchar(260),
File_path nvarchar(260),
Type char(1),
FileGroupName nvarchar(128)
)
insert into #tem
execute('restore filelistonly from disk='''+@filename+'''')
/*將臨時表導(dǎo)入表變量中,并且計算出相應(yīng)得路徑*/
insert into @tem(LogicalName,PhysicalName,File_path,Type,FileGroupName)
select LogicalName,PhysicalName,dbo.fn_GetFilePath(PhysicalName),Type,FileGroupName
from #tem
select * from #tem
select * from @tem
if @@rowcount>0
begin
drop table #tem
end
select @loop_time=1
select @max_ids=max(ids) /*@tem表的ids列最大數(shù)*/
from @tem
while @loop_time<=@max_ids
begin
select @file_bak_path=file_path
from @tem where ids=@loop_time
select @sql_cmd='dir '+@file_bak_path
EXEC @proc_result = master..xp_cmdshell @sql_cmd,no_output
/*系統(tǒng)存儲過程xp_cmdshell返回代碼值:0(成功)或1(失?。?/
IF (@proc_result<>0)
select @loop_time=@loop_time+1
else
BREAK /*沒有找到備份前數(shù)據(jù)文件原有存放路徑,退出循環(huán)*/
end
select @master_path=''
if @loop_time>@max_ids
select @flag_file=1 /*備份前數(shù)據(jù)文件原有存放路徑存在*/
else
begin
select @flag_file=0 /*備份前數(shù)據(jù)文件原有存放路徑不存在*/
select @master_path=dbo.fn_GetFilePath(filename)
from master..sysdatabases where name='master'
end
select @flag_file as '@flag_file'
select @sql_sub=''
/*type='d'是數(shù)據(jù)文件,type='l'是日志文件 */
/*@flag_file=1時新的數(shù)據(jù)庫文件還是存放在原來路徑,否則存放路徑和master數(shù)據(jù)庫路徑一樣*/
select @sql_sub=@sql_sub+'move '''+LogicalName+''' to '''
+case type
when 'd' then case @flag_file
when 1 then File_path
else @master_path
end
when 'l' then case @flag_file
when 1 then File_path
else @master_path
end
end
+case type
when 'd' then @restore_db_name+'_'+LogicalName+'_data.mdf'','
when 'l' then @restore_db_name+'_'+LogicalName+'_log.ldf'','
end
from @tem
select @sql='RESTORE DATABASE @db_name FROM DISK=@filename with '
select @sql=@sql+@sql_sub+'replace'
select @par='@db_name nvarchar(128),@filename nvarchar(260)'
select @sql as 'sql'
/*關(guān)閉相關(guān)進(jìn)程,把相應(yīng)進(jìn)程狀況導(dǎo)入臨時表中*/
--select * from master..sysprocesses where dbid=db_id(@restore_db_name)
select identity(int,1,1) ids, spid
into #temp
from master..sysprocesses
where dbid=db_id(@restore_db_name)
select * from #temp
if @@rowcount>0 --找到相應(yīng)進(jìn)程
begin
select @max_ids=max(ids)
from #temp
select @loop_time=1
while @loop_time<=@max_ids
begin
select @sql_kill=''
select @sql_kill='kill '+convert(nvarchar(20),spid)
from #temp
where ids=@loop_time
execute sp_executesql @sql_kill
select @loop_time=@loop_time+1
end
end
drop table #temp
execute sp_executesql @sql,@par,@db_name=@restore_db_name,@filename=@filename
select @flag='ok' /*操作成功*/
end
else
begin
SELECT @flag='file type error' /*參數(shù)@filename輸入格式錯誤*/
end
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。