首页 > 代码库 > SQL Server 备份所有数据库代码

SQL Server 备份所有数据库代码

今天让我备份一下网上所有数据库,猛地一看,几百个呢, 坑爹呢,只好网上找找有没有简便的,没想到还真有 记下来,以后好用,哈哈。。。

 

use masterdeclare @DbName varchar(60)declare @BackSql varchar(1000)declare myCursor cursor forSELECT [name] FROM SYSDATABASESwhere [name] not in (‘master‘,‘model‘,‘msdb‘,‘tempdb‘)order by [name]open myCursorfetch next from myCursor into @DbNamewhile(@@FETCH_STATUS = 0)beginif datename(weekday, getdate())=‘星期三‘ --每周三覆盖上周三的beginselect @BackSql=‘Backup DATABASE [‘+@DbName+‘] to disk=‘‘E:\DbBackUp\‘+@DbName+‘星期三.bak‘‘ with format‘endelse--每天覆盖上一天的beginselect @BackSql=‘Backup DATABASE [‘+@DbName+‘] to disk=‘‘E:\database\alldatabase\‘+@DbName+‘2013-06-05.bak‘‘ with format‘endexec(@BackSql)fetch next from myCursor into @DbNameendclose myCursorDEALLOCATE myCursor

  

SQL Server 备份所有数据库代码