欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sqlserver 自动备份所有数据库的SQL

程序员文章站 2023-12-04 12:10:52
复制代码 代码如下:use master declare @dbname varchar(60) declare @backsql varchar(1000) declar...
复制代码 代码如下:

use master
declare @dbname varchar(60)
declare @backsql varchar(1000)
declare mycursor cursor for
select [name] from sysdatabases
where [name] not in ('master','model','msdb','tempdb')
order by [name]
open mycursor
fetch next from mycursor into @dbname
while(@@fetch_status = 0)
begin
if datename(weekday, getdate())='星期三' --每周三覆盖上周三的
begin
select @backsql='backup database ['+@dbname+'] to disk=''e:\dbbackup\'+@dbname+'星期三.bak'' with format'
end
else--每天覆盖上一天的
begin
select @backsql='backup database ['+@dbname+'] to disk=''e:\dbbackup\'+@dbname+'autoback.bak'' with format'
end
exec(@backsql)
fetch next from mycursor into @dbname
end
close mycursor
deallocate mycursor