您好,欢迎来到思海网络,我们将竭诚为您提供优质的服务! 诚征网络推广 | 网站备案 | 帮助中心 | 软件下载 | 购买流程 | 付款方式 | 联系我们 [ 会员登录/注册 ]
促销推广
客服中心
业务咨询
有事点击这里…  531199185
有事点击这里…  61352289
点击这里给我发消息  81721488
有事点击这里…  376585780
有事点击这里…  872642803
有事点击这里…  459248018
有事点击这里…  61352288
有事点击这里…  380791050
技术支持
有事点击这里…  714236853
有事点击这里…  719304487
有事点击这里…  1208894568
有事点击这里…  61352289
在线客服
有事点击这里…  531199185
有事点击这里…  61352288
有事点击这里…  983054746
有事点击这里…  893984210
当前位置:首页 >> 技术文章 >> 文章浏览
技术文章

MS SQL数据库备份和恢复存储过程

添加时间:2013-3-30 17:38:13  添加: 思海网络 
数据库备份和恢复存储过程: 

if exists(
 select * from sysobjects
  where name='pr_backup_db' and xtype='p'
 )
begin
 drop proc pr_backup_db
end
go
/*备份数据库*/
create proc pr_backup_db
@flag varchar(10) out,
@backup_db_name varchar(128),
@filename varchar(1000)  --路径+文件名字
as
declare @sql nvarchar(4000),@par nvarchar(1000)
select @par='@filename varchar(1000)'
select @sql='BACKUP DATABASE '+@backup_db_name+' to disk=@filename with init'
execute sp_executesql @sql,@par,@filename
select @flag='ok'
go

 

if exists(
 select * from sysobjects
  where name='fn_GetFilePath' and xtype='fn'
 )
begin
 drop function fn_GetFilePath
end
go
/*创建函数,得到文件得路径*/
create function fn_GetFilePath(@filename nvarchar(260))
returns nvarchar(260)  
as
begin
 declare @file_path nvarchar(260)
 declare @filename_reverse nvarchar(260)
 select @filename_reverse=reverse(@filename)
 select @file_path=substring(@filename,1,len(@filename)+1-charindex('\',@filename_reverse))
 return @file_path
end

go


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    /*恢复数据库*/
@flag varchar(20) out,    /*过程运行的状态标志,是输入参数*/     
@restore_db_name nvarchar(128),  /*要恢复的数据名字*/
@filename nvarchar(260)         /*备份文件存放的路径+备份文件名字*/
as
declare @proc_result tinyint  /*返回系统存储过程xp_cmdshell运行结果*/
declare @loop_time smallint  /*循环次数*/
declare @max_ids smallint    /*@tem表的ids列最大数*/
declare @file_bak_path nvarchar(260)  /*原数据库存放路径*/
declare @flag_file bit   /*文件存放标志*/
declare @master_path nvarchar(260)  /*数据库master文件路径*/
declare @sql nvarchar(4000),@par nvarchar(1000)
declare @sql_sub nvarchar(4000)
declare @sql_cmd nvarchar(4000)
/*
判断参数@filename文件格式合法性,以防止用户输入类似d: 或者 c:\a\ 等非法文件名
参数@filename里面必须有'\'并且不以'\'结尾
*/
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)  /*系统存储过程xp_cmdshell返回代码值:0(成功)或1(失败)*/
 begin
  select @flag='not exist'   /*备份文件不存在*/
  return  /*退出过程*/
 end
 /*创建临时表,保存由备份集内包含的数据库和日志文件列表组成的结果集*/
 create table #tem(
     LogicalName nvarchar(128), /*文件的逻辑名称*/
     PhysicalName nvarchar(260) , /*文件的物理名称或操作系统名称*/
     Type char(1),  /*数据文件 (D) 或日志文件 (L)*/
     FileGroupName nvarchar(128), /*包含文件的文件组名称*/
     [Size] numeric(20,0),  /*当前大小(以字节为单位)*/
     [MaxSize] numeric(20,0)  /*允许的最大大小(以字节为单位)*/
   )
 /*
 创建表变量,表结构与临时表基本一样
 就是多了两列,
 列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+'_'+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)'
 print @sql
 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

 

 


--备份数据库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

关键字:数据库、存储过程、备份
分享到:

顶部 】 【 关闭
版权所有:佛山思海电脑网络有限公司 ©1998-2024 All Rights Reserved.
联系电话:(0757)22630313、22633833
中华人民共和国增值电信业务经营许可证: 粤B1.B2-20030321 备案号:粤B2-20030321-1
网站公安备案编号:44060602000007 交互式栏目专项备案编号:200303DD003  
察察 工商 网安 举报有奖  警警  手机打开网站