Home  >  Article  >  Database  >  sqlserver 自动备份所有数据库的SQL

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

WBOY
WBOYOriginal
2016-06-07 18:00:021102browse

可自动备份除系统数据库外的所有数据库。备份文件的周期保存周期可以更改。

代码如下:
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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn