SQLSERVER备份脚本

SQLSERVER备份脚本,第1张

概述看网上的例子写的sqlserver数据库备份脚本,这个脚本不是很好,不能删除以前备份的。 declare @CurrentDataBaseName nvarchar(100) declare @CurrentBackFolder nvarchar(200) declare @WeekDay VARCHAR(20) declare @CurrentBackString nvarchar(2000)

看网上的例子写的sqlserver数据库备份脚本,这个脚本不是很好,不能删除以前备份的。

declare @CurrentDataBasename nvarchar(100)

declare @CurrentBackFolder nvarchar(200)

declare @WeekDay VARCHAR(20)

declare @CurrentBackString nvarchar(2000)

declare @day VARCHAR(20)

--SELECT GETDATE() AS 'Current Date'

set @day=convert(varchar(100),getdate(),112)

set @WeekDay = DATEPART(WEEKDAY,GETDATE())

set @CurrentBackFolder='D:\Test'

--set @CurrentDataBasename='ceshi'

--+convert(varchar(50),112),dbID

--select * from   master..sysdatabases   where   dbID>=7

--weekday 1 表示星期日

if @WeekDay = '1'

begin

declare tb cursor local for select name 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+'.bak'' WITH NOFORMAT,NOINIT,name='''+@CurrentDataBasename+'-'+@day+''',SKIP,norEWIND,NOUNLOAD;';

print @CurrentBackString;

exec sp_executesql @CurrentBackString;

print '备份数据库'+@CurrentDataBasename +'完成';

fetch next from tb into @CurrentDataBasename

end

close tb

deallocate tb

print '备份所有数据库完成'

end

else

begin

declare tb cursor local for select name 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+'.bak'' WITH NOFORMAT,name='''+@CurrentDataBasename+'-'+@day+'-diff'',DIFFERENTIAL,

norEWIND,NOUNLOAD,RETAINDAYS=6;';

print @CurrentBackString;

exec sp_executesql @CurrentBackString;

print '差异备份数据库'+@CurrentDataBasename +'完成';

fetch next from tb into @CurrentDataBasename

end

close tb

deallocate tb

print '差异备份所有数据库完成'

end

总结

以上是内存溢出为你收集整理的SQLSERVER备份脚本全部内容,希望文章能够帮你解决SQLSERVER备份脚本所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1174413.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存