用於SQL2000Database Backup還原的預存程序

來源:互聯網
上載者:User

這是在網上無意間找到的,以前試了一下,備份沒有問題,但是還原時提示無法KILL自己的進程。

代碼如下:

[@more@]/*備份資料庫的過程*/
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'  /*資料庫不存在*/
 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'  /*參數@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 '+@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
           +'_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

以上過程和函數在MS SQL2000運行成功,由於MS SQL7不支援使用者自訂函數和表變數,要在MS SQL7下使用可以把函數fn_GetFilePath改寫成過程,把過程pr_restore_db中的表變數改寫為暫存資料表即可運行

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.