学院首页>网络编程>SQL Server>SQL SERVER2000备份和恢复存储过程

SQL SERVER2000备份和恢复存储过程

作者: 来源: 添加时间:2006-5-22 11:11:42
/*
 创建表变量,表结构与临时表基本一样
 就是多了两列,
 列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+’’’’)
 /*将临时表导入表变量中,并且计算出相应得路径*/
 insert into @tem(LogicalName,PhysicalName,File_path,Type,FileGroupName)  
  select LogicalName,PhysicalName,dbo.fn_GetFilePath(PhysicalName),Type,FileGroupName 
from #tem
 if @@rowcount>0 
 begin
  drop table #tem
 end
 select @loop_time=1
 select @max_ids=max(ids)  /*@tem表的ids列最大数*/
  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
  /*系统存储过程xp_cmdshell返回代码值:0(成功)或1(失败)*/
  IF (@proc_result<>0) 
select @loop_time=@loop_time+1  
  else
BREAK /*没有找到备份前数据文件原有存放路径,退出循环*/
 end
 select @master_path=’’
 if @loop_time>@max_ids 
  select @flag_file=1/*备份前数据文件原有存放路径存在*/
 else
 begin
  select @flag_file=0  /*备份前数据文件原有存放路径不存在*/
  select @master_path=dbo.fn_GetFilePath(filename)
from master..sysdatabases 
where name=’master’
 end
 select @sql_sub=’’
 /*type=’d’是数据文件,type=’l’是日志文件 */
 /*@flag_file=1时新的数据库文件还是存放在原来路径,否则存放路径和master数据库路径一样*/
 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
  +’_DATA’
  +convert(sysname,ids)  /*给文件编号*/
  +’.’
  +right(PhysicalName,3)  /*给文件加入后缀名,mdf or ndf*/
  +’’’,’  
 when ’l’ then @restore_db_name
  +’_LOG’
  +convert(sysname,ids)/*给文件编号*/
  +’.’
  +right(PhysicalName,3)  /*给文件加入后缀名,mdf or ndf*/
  +’’’,’  
 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 identity(int,1,1) ids, spid 
  into #temp
  from master..sysprocesses
  where dbid=db_id(@restore_db_name)
 if @@rowcount>0 --找到相应进程
 begin
  select @max_ids=max(ids) 
from #temp
  select @loop_time=1
  while @loop_time<=@max_ids
  begin
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’  /*参数@filename输入格式错误*/
end

GO

--run
--备份数据库test_database
declare @fl varchar(10)
execute pr_backup_db @fl out,’test_database’,’c:\test_database.bak’
select @fl
--恢复数据库,输入的参数错误
declare @fl varchar(20)
exec pr_restore_db @fl out,’sa’,’c:\’
select @fl

--恢复数据库,即创建数据库test_database的复本test_db
declare @fl varchar(20)
exec pr_restore_db @fl out,’test_db’,’c:\test_database.bak’
select @fl
第 2 页,共 2 页 [1] [2]
站内搜索