sqlserver 自动备份所有数据库的SQL
程序员文章站
2023-12-14 16:03:28
复制代码 代码如下: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