国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

SQL SERVER2000數據庫備份和恢復存儲過程(加強版本)

2024-08-31 00:48:21
字體:
來源:轉載
供稿:網友



sql server2000數據庫備份和恢復存儲過程(加強版本)

我自己寫的2個過程和一個函數,用于sql server2000數據庫備份和恢復
拿出來和大家交流一下,過程和函數的詳細說明在代碼中
謝謝


/*備份數據庫的過程*/
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(20) out,
@backup_db_name varchar(128),
@filename varchar(1000)  --路徑+文件名字
as
declare @sql nvarchar(4000),@par nvarchar(1000)
if not exists(
 select * from master..sysdatabases
  where [email protected]_db_name
  )
begin
 select @flag='db not exist'  /*數據庫不存在*/
 return
end
else
begin
 if right(@filename,1)<>'/' and charindex('/',@filename)<>0
 begin
  select @par='@filename varchar(1000)'
  select @sql='backup database '[email protected]_db_name+' to [email protected] with init'
  execute sp_executesql @sql,@par,@filename
  select @flag='ok' 
  return
 end
 else
 begin
  select @flag='file type error'  /*參數@filename輸入格式錯誤*/
  return
 end
end

go

說明:pr_backup_db過程是備份你的數據庫

 

 

/*創建函數,得到文件得路徑*/
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(100)
declare @sql_kill nvarchar(100)
/*
判斷參數@filename文件格式合法性,以防止用戶輸入類似d: 或者 c:/a/ 等非法文件名
參數@filename里面必須有'/'并且不以'/'結尾
*/
if right(@filename,1)<>'/' and charindex('/',@filename)<>0
begin
 select @sql_cmd='dir '[email protected]
 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='''[email protected]+'''')
 /*將臨時表導入表變量中,并且計算出相應得路徑*/
 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<[email protected]_ids
 begin
  select @file_bak_path=file_path
   from @tem where [email protected]_time
  select @sql_cmd='dir '[email protected]_bak_path
  exec @proc_result = master..xp_cmdshell @sql_cmd,no_output
  /*系統存儲過程xp_cmdshell返回代碼值:0(成功)或1(失敗)*/
  if (@proc_result<>0)
   select @[email protected]_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 @[email protected]_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 [email protected] with '
 select @[email protected][email protected]_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<[email protected]_ids
  begin
   select @sql_kill='kill '+convert(nvarchar(20),spid)
    from #temp
    where [email protected]_time
   execute sp_executesql @sql_kill
   select @[email protected]_time+1 
  end
 end 
 drop table #temp
 execute sp_executesql @sql,@par,@[email protected]_db_name,@[email protected]
 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

 

以上過程和函數在ms sql2000運行成功,由于ms sql7不支持用戶自定義函數和表變量,要在ms sql7下使用可以把函數fn_getfilepath改寫成過

程,把過程pr_restore_db中的表變量改寫為臨時表即可運行,有興趣的朋友可以試試!
我的email:[email protected]
歡迎大家交流

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 珠海市| 邢台市| 抚顺市| 普兰县| 城步| 永年县| 白河县| 韶山市| 阿城市| 区。| 蕉岭县| 德阳市| 内江市| 泾阳县| 来安县| 屏山县| 乐平市| 江都市| 疏勒县| 灌云县| 汝阳县| 河西区| 孙吴县| 和林格尔县| 衡山县| 库伦旗| 南城县| 斗六市| 新竹市| 遂宁市| 泸水县| 大方县| 慈利县| 临清市| 广东省| 新干县| 邳州市| 潮安县| 安阳县| 观塘区| 平遥县|