zoukankan      html  css  js  c++  java
  • 用游标备份单个或所有db

    CREATE procedure [dbo].[usp_BackupAllDB]
    @databasename varchar(50)=null,
    @backupdir varchar(100) =null

    as
    if @backupdir is null
    set @backupdir='G:sqldatamssqlackup'
    set nocount on
    declare @dbname varchar(50),
    @stime varchar(20),
    @fname varchar(100),
    @cmd varchar(200),
    @sql varchar(1000)

    if @databasename is not null
    declare mycursor cursor for
    select name
    from master..sysdatabases
    where name=@databasename
    else
    declare mycursor cursor for
    select name
    from master.sys.databases
    where database_id not in (2,3)
    and state_desc ='ONLINE'
    order by name

    open mycursor
    fetch next from mycursor into @dbname
    while @@fetch_status =0
    begin
    select @cmd = "exec master..xp_cmdshell 'if not exist "+@backupdir+""+@dbname+ " md "+@backupdir+""+@dbname+"'"
    --select @cmd
    exec (@CMD)

    select @stime=replace(replace(replace(convert(varchar(19),getdate(),120),':',''),' ',''),'-','')
    select @sql=" backup database "+@dbname +" to disk='"+@backupdir+""+@dbname+""+@dbname+"_"+@stime+"_D.BAK' WITH COMPRESSION"
    exec (@sql)
    select @sql
    fetch next from mycursor into @dbname
    end
    close mycursor
    deallocate mycursor


    set nocount off

  • 相关阅读:
    有关数据恢复的几个概念的理解
    cmsr 1.0.6
    Cmsr 1.0.5
    Cmsr 1.0.4
    vue中的v-model 与 .sync
    es6中clss做了些什么 怎么继承
    Cmsr 1.0.2
    Cmsr 1.0.1
    Cmsr 1.0.0
    VUE3.0新特性
  • 原文地址:https://www.cnblogs.com/sandra/p/6098898.html
Copyright © 2011-2022 走看看