欢迎投稿

今日深度:

如何还原某一个目录下的所有数据库备份文件((3)

●还原多个数据库的存储过程代码(一个目录下)

当一个目录下,存放这很多个备份文件的时候,我们需要还原整个目录,或者部分数据库备份文件,这时我们另外一个存储过程能实现这样的操作。这里我写一个存储过程sp_RestoreDataBase2:

  1. if object_id('sp_restoredatabase2'Is Not Null 
  2.     Drop proc sp_restoredatabase2 
  3. Go 
  4. create proc sp_restoredatabase2 
  5.     @Path_bak nvarchar(1024), 
  6.     @Path_new nvarchar(1024)=null
  7.     @DataBaseList nvarchar(max)=null 
  8. As 
  9. /* 
  10.  
  11. @DataBaseList 数据库列表,可留空,或数据库之间使用回车、空格、逗号分隔都ok 
  12.  
  13. */ 
  14.  
  15.  
  16. Set Nocount On 
  17. Declare @subdirectory nvarchar(1024),@ErrorMsg nvarchar(1024),@flag smallint 
  18.  
  19. if isnull(@Path_new,'')=''         
  20.     exec master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultData', @Path_new Output     
  21.  
  22.  
  23. If Not Exists(Select 1 From master.sys.procedures Where name='sp_RestoreDataBase'
  24. Begin 
  25.     Raiserror 50001 N'找不到存储过程 sp_RestoreDataBase ' 
  26.     Goto ExitFLag 
  27. End 
  28.  
  29.  
  30. Declare @Dir Table(subdirectory nvarchar(1024),depth int,[file] int
  31. Declare @DBList table (DatabaseName nvarchar(128)) 
  32. Declare @DBListNull table (DatabaseName nvarchar(128)) 
  33.  
  34. /* 
  35. --SQL Server 2005 
  36. Declare @BakHeaderTMP Table(BackupName nvarchar(128),BackupDescription nvarchar(255),BackupType smallint,ExpirationDate datetime,Compressed tinyint,Position smallint,DeviceType tinyint,UserName nvarchar(128),ServerName nvarchar(128),DatabaseName nvarchar(128),DatabaseVersion int,DatabaseCreationDate datetime,BackupSize numeric(20,0),FirstLSN numeric(25,0),LastLSN numeric(25,0),CheckpointLSN numeric(25,0),DatabaseBackupLSN numeric(25,0),BackupStartDate datetime,BackupFinishDate datetime,SortOrder smallint,CodePage smallint,UnicodeLocaleId int,UnicodeComparisonStyle int,CompatibilityLevel tinyint,SoftwareVendorId int,SoftwareVersionMajor int,SoftwareVersionMinor int,SoftwareVersionBuild int,MachineName nvarchar(128),Flags int,BindingID uniqueidentifier,RecoveryForkID uniqueidentifier,Collation nvarchar(128),FamilyGUID uniqueidentifier,HasBulkLoggedData bit,IsSnapshot bit,IsReadOnly bit,IsSingleUser bit,HasBackupChecksums bit,IsDamaged bit,BeginsLogChain bit,HasIncompleteMetaData bit,IsForceOffline bit,IsCopyOnly bit,FirstRecoveryForkID uniqueidentifier,ForkPointLSN numeric(25,0) NULL,RecoveryModel nvarchar(60),DifferentialBaseLSN numeric(25,0) NULL,DifferentialBaseGUID uniqueidentifier,BackupTypeDescription nvarchar(60),BackupSetGUID uniqueidentifier NULL
  37. */ 
  38. --SQL Server 2008 
  39. Declare @BakHeaderTMP Table(BackupName nvarchar(128),BackupDescription nvarchar(255),BackupType smallint,ExpirationDate datetime,Compressed tinyint,Position smallint,DeviceType tinyint,UserName nvarchar(128),ServerName nvarchar(128),DatabaseName nvarchar(128),DatabaseVersion int,DatabaseCreationDate datetime,BackupSize numeric(20,0),FirstLSN numeric(25,0),LastLSN numeric(25,0),CheckpointLSN numeric(25,0),DatabaseBackupLSN numeric(25,0),BackupStartDate datetime,BackupFinishDate datetime,SortOrder smallint,CodePage smallint,UnicodeLocaleId int,UnicodeComparisonStyle int,CompatibilityLevel tinyint,SoftwareVendorId int,SoftwareVersionMajor int,SoftwareVersionMinor int,SoftwareVersionBuild int,MachineName nvarchar(128),Flags int,BindingID uniqueidentifier,RecoveryForkID uniqueidentifier,Collation nvarchar(128),FamilyGUID uniqueidentifier,HasBulkLoggedData bit,IsSnapshot bit,IsReadOnly bit,IsSingleUser bit,HasBackupChecksums bit,IsDamaged bit,BeginsLogChain bit,HasIncompleteMetaData bit,IsForceOffline bit,IsCopyOnly bit,FirstRecoveryForkID uniqueidentifier,ForkPointLSN numeric(25,0) NULL,RecoveryModel nvarchar(60),DifferentialBaseLSN numeric(25,0) NULL,DifferentialBaseGUID uniqueidentifier,BackupTypeDescription nvarchar(60),BackupSetGUID uniqueidentifier NULL,CompressedBackupSize numeric(20,0)) 
  40.  
  41.      
  42.  
  43.  
  44. While charindex(char(13)+Char(10),@DataBaseList)>0 
  45.     Set @DataBaseList=Replace(@DataBaseList,char(13)+Char(10),','
  46. While charindex(char(13),@DataBaseList)>0 
  47.     Set @DataBaseList=Replace(@DataBaseList,char(13),','
  48. While charindex(char(9),@DataBaseList)>0 
  49.     Set @DataBaseList=Replace(@DataBaseList,char(9),','
  50. While charindex(char(32),@DataBaseList)>0 
  51.     Set @DataBaseList=Replace(@DataBaseList,Char(32),','
  52.  
  53.          
  54. Set @DataBaseList='Select '''+Replace(@DataBaseList,',',''' Union All Select ''')+'''' 
  55. Insert Into @DBList  Exec(@DataBaseList) 
  56.  
  57. Delete @DBList Where DatabaseName ='' 
  58.  
  59. Insert Into @Dir Exec xp_dirtree @Path_bak,1,1 
  60.  
  61. If Not Exists(Select subdirectory From @Dir Where Charindex('.bak',subdirectory)>0) 
  62. Begin 
  63.     Set @ErrorMsg= N'无效的数据库路径: '+ rtrim(@Path_bak) 
  64.     Raiserror 50001 @ErrorMsg 
  65.     Goto ExitFlag 
  66. End 
  67.  
  68.  
  69.  
  70. Declare cur_x cursor For Select subdirectory From @Dir Where Charindex('.bak',subdirectory)>0 
  71. Open cur_x 
  72. Fetch Next From cur_x Into @subdirectory 
  73. While @@Fetch_status=0 
  74. Begin 
  75.      
  76.     Set @subdirectory=Case When Right(@Path_bak,1)='\' Then @Path_bak Else   @Path_bak+'\' End+@subdirectory 
  77.      
  78.     Delete From @BakHeaderTMP 
  79.  
  80.     Insert Into @BakHeaderTMP 
  81.         Exec sp_executesql N'Restore HeaderOnly From Disk=@DatabBaseBakPath',N'@DatabBaseBakPath nvarchar(260)',@subdirectory 
  82.          
  83.     IF @@ERROR <> 0             
  84.         Break 
  85.     Else if Exists(Select 1 From @BakHeaderTMP As a Where Exists(Select 1 From @DBList Where DatabaseName=a.DatabaseName) Or Not Exists(Select 1 From @DBList))         
  86.         Begin 
  87.             Exec @flag=sp_RestoreDataBase @subdirectory,@Path_new             
  88.             If @flag <>0 Break 
  89.         End 
  90.  
  91.     Insert Into @DBListNull(DatabaseName) 
  92.         Select DatabaseName From @BakHeaderTMP     
  93.                          
  94.     Fetch Next From cur_x Into @subdirectory 
  95. End 
  96. CLose cur_x 
  97. Deallocate cur_x 
  98.  
  99.  
  100.  
  101. If Exists(Select 1 From @DBList a Where Not Exists(Select 1 From @DBListNull Where DatabaseName=a.DatabaseName)) 
  102.     Select DatabaseName As [无效的数据库] From @DBList a Where Not Exists(Select 1 From @DBListNull Where DatabaseName=a.DatabaseName) 
  103.  
  104. print replicate('=',60) 
  105. Print N'@Path_new :  '+@Path_new 
  106.  
  107. ExitFLag: 
  108.  
  109. Go 

存储过程测试:

  1. use master 
  2. Go 
  3. Exec dbo.sp_RestoreDataBase2 'E:\DBBackup' 
  4. go 

小结

上面还原数据库的存储过程,它们给我们在工作中还原数据库的时候,带来许多便捷,如,不用我们一个个通过Microsoft SQL Server Management Studio(MSSMS)中的还原数据库向导去还原数据库,或也不用我们一个个执行”Restore Database”SQL语句去还原数据库。当然,在上面的代码中,我没有对每一个存储过程的每一个具体位置,进行解释。没有全部应用到”Restore Database”中”WITH”选项,我编写的主要目的是,存储过程参数尽可能的少,操作起来更方便,尽可能满足真实环境中的需要。如果你应用到以上的代码,可以根据自己所在的真实环境,进行修改补充。


www.htsjk.Com true http://www.htsjk.com/shujukugl/17753.html NewsArticle ●还原多个数据库的存储过程代码(一个目录下) 当一个目录下,存放这很多个备份文件的时候,我们需要还原整个目录,或者部分数据库备份文件,这时我...
评论暂时关闭