• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 數據庫備份與恢復的存儲過程

    發表于:2007-05-25來源:作者:點擊數: 標簽:數據庫EXISTS過程存儲備份
    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) --路徑

    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

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>