国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
SQL SERVER2000數(shù)據(jù)庫(kù)備份和恢復(fù)存儲(chǔ)過程(加強(qiáng)版本)

SQL SERVER2000數(shù)據(jù)庫(kù)備份和恢復(fù)存儲(chǔ)過程(加強(qiáng)版本)

我自己寫的2個(gè)過程和一個(gè)函數(shù),用于SQL SERVER2000數(shù)據(jù)庫(kù)備份和恢復(fù)
拿出來和大家交流一下,過程和函數(shù)的詳細(xì)說明在代碼中
謝謝


/*備份數(shù)據(jù)庫(kù)的過程*/
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 name=@backup_db_name
  )
begin
 select @flag='db not exist'  /*數(shù)據(jù)庫(kù)不存在*/
 return
end
else
begin
 if right(@filename,1)<>'\' and charindex('\',@filename)<>0
 begin
  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' 
  return
 end
 else
 begin
  select @flag='file type error'  /*參數(shù)@filename輸入格式錯(cuò)誤*/
  return
 end
end

GO

說明:pr_backup_db過程是備份你的數(shù)據(jù)庫(kù)

 

 

/*創(chuàng)建函數(shù),得到文件得路徑*/
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


/*恢復(fù)數(shù)據(jù)庫(kù)的過程*/
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,    /*過程運(yùn)行的狀態(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)存儲(chǔ)過程xp_cmdshell運(yùn)行結(jié)果*/
declare @loop_time smallint  /*循環(huán)次數(shù)*/
declare @max_ids smallint    /*@tem表的ids列最大數(shù)*/
declare @file_bak_path nvarchar(260)  /*原數(shù)據(jù)庫(kù)存放路徑*/
declare @flag_file bit   /*文件存放標(biāo)志*/
declare @master_path nvarchar(260)  /*數(shù)據(jù)庫(kù)master文件路徑*/
declare @sql nvarchar(4000),@par nvarchar(1000)
declare @sql_sub nvarchar(4000)
declare @sql_cmd nvarchar(100)
declare @sql_kill nvarchar(100)
/*
判斷參數(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)存儲(chǔ)過程xp_cmdshell返回代碼值:0(成功)或1(失?。?/
 begin
  select @flag='not exist'   /*備份文件不存在*/
  return  /*退出過程*/
 end
 /*創(chuàng)建臨時(shí)表,保存由備份集內(nèi)包含的數(shù)據(jù)庫(kù)和日志文件列表組成的結(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)與臨時(shí)表基本一樣
 就是多了兩列,
 列ids(自增編號(hào)列),
 列file_path,存放文件的路徑
 */
 declare @tem table(      
     ids smallint identity,  /*自增編號(hào)列*/
     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+'''')
 /*將臨時(shí)表導(dǎo)入表變量中,并且計(jì)算出相應(yīng)得路徑*/
 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列最大數(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)存儲(chǔ)過程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 @sql_sub=''
 /*type='d'是數(shù)據(jù)文件,type='l'是日志文件 */
 /*@flag_file=1時(shí)新的數(shù)據(jù)庫(kù)文件還是存放在原來路徑,否則存放路徑和master數(shù)據(jù)庫(kù)路徑一樣*/
 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)  /*給文件編號(hào)*/
           +'.'
           +right(PhysicalName,3)  /*給文件加入后綴名,mdf or ndf*/
           +''',' 
    when 'l' then @restore_db_name
           +'_LOG'
           +convert(sysname,ids)   /*給文件編號(hào)*/
           +'.'
           +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)'
 /*關(guān)閉相關(guān)進(jìn)程,把相應(yīng)進(jìn)程狀況導(dǎo)入臨時(shí)表中*/
 select identity(int,1,1) ids, spid
  into #temp
  from master..sysprocesses
  where dbid=db_id(@restore_db_name)
 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='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輸入格式錯(cuò)誤*/
end


GO

 

 

 

 

 

 


--run

--備份數(shù)據(jù)庫(kù)test_database
declare @fl varchar(10)
execute pr_backup_db @fl out,'test_database','c:\test_database.bak'
select @fl

--恢復(fù)數(shù)據(jù)庫(kù),輸入的參數(shù)錯(cuò)誤
declare @fl varchar(20)
exec pr_restore_db @fl out,'sa','c:\'
select @fl


--恢復(fù)數(shù)據(jù)庫(kù),即創(chuàng)建數(shù)據(jù)庫(kù)test_database的復(fù)本test_db
declare @fl varchar(20)
exec pr_restore_db @fl out,'test_db','c:\test_database.bak'
select @fl

 

以上過程和函數(shù)在MS SQL2000運(yùn)行成功,由于MS SQL7不支持用戶自定義函數(shù)和表變量,要在MS SQL7下使用可以把函數(shù)fn_GetFilePath改寫成過

程,把過程pr_restore_db中的表變量改寫為臨時(shí)表即可運(yùn)行,有興趣的朋友可以試試!

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
MSSQL從文件導(dǎo)入數(shù)據(jù)
SQL Server備份數(shù)據(jù)庫(kù)清除過期備份的存儲(chǔ)過程
SQL Server數(shù)據(jù)庫(kù)備份、差異備份、日志備份腳本
MS SQL的數(shù)據(jù)庫(kù)備份和恢復(fù)存儲(chǔ)過程
數(shù)據(jù)庫(kù)備份/恢復(fù)方案
一百多個(gè)數(shù)據(jù)庫(kù),如何實(shí)現(xiàn)批量恢復(fù)數(shù)據(jù)庫(kù)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服