--SQL备份所有数据库脚本
declare @CurrentDataBaseName nvarchar(100)
declare @CurrentBackFolder nvarchar(200)
declare @CurrentBackString nvarchar(2000)
set @CurrentBackFolder='D:\Test'--这里是备份的目录,所有数据库都备份到这个目录
--查询所有数据库名称
--select * from master..sysdatabases where dbid>=7
declare tb cursor local for select name,dbid from master..sysdatabases where dbid>=7;
open tb
fetch next from tb into @CurrentDataBaseName
while @@fetch_status=0
begin
--备份当前查询到的数据库到指定目录
set @CurrentBackString='
USE [master]
BACKUP DATABASE ['+@CurrentDataBaseName+'] TO DISK = '''+ @CurrentBackFolder+'\'+@CurrentDataBaseName+convert(varchar(50),getdate(),112)+'.bak'' WITH NOFORMAT, NOINIT,NAME
='''+@CurrentDataBaseName+'-完整 数据库 备份'',SKIP, NOREWIND, NOUNLOAD;';
print @CurrentBackString;
exec sp_executesql @CurrentBackString;
print '备份数据库'+@CurrentDataBaseName +'完成';
fetch next from tb into @CurrentDataBaseName
end
close tb
deallocate tb
print '备份所有数据库完成'