通过T-SQL语句实现数据库备份与还原的代码

前端之家收集整理的这篇文章主要介绍了通过T-SQL语句实现数据库备份与还原的代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<SPAN style="COLOR: #008080">--@H_404_0@<SPAN style="COLOR: #008080">利用T-sql语句,实现数据库的备份与还原的功能 @H_4040@<SPAN style="COLOR: #008080">
--
--@H
404_0@<SPAN style="COLOR: #008080">体现了sql Server中的四个知识点: @H_4040@<SPAN style="COLOR: #008080">
--
--@H
404_0@<SPAN style="COLOR: #008080">1. 获取sql Server服务器上的默认目录 @H_4040@<SPAN style="COLOR: #008080">
--
--@H
404_0@<SPAN style="COLOR: #008080">2. 备份sql语句的使用 @H_4040@<SPAN style="COLOR: #008080">
--
--@H
404_0@<SPAN style="COLOR: #008080">3. 恢复sql语句的使用,同时考虑了强制恢复时关闭其他用户进程的处理 @H_4040@<SPAN style="COLOR: #008080">
--
--@H
404_0@<SPAN style="COLOR: #008080">4. 作业创建sql语句的使用@H_4040@<SPAN style="COLOR: #008080">
@H
4040@
<SPAN style="COLOR: #008080">/*@H
404_0@<SPAN style="COLOR: #008080">1.--得到数据库文件目录@dbname 指定要取得目录的数据库
如果指定的数据不存在,返回安装sql时设置的默认数据目录
如果指定NULL,则返回默认的sql备份目录名
@H_4040@<SPAN style="COLOR: #008080">*/@H4040@<SPAN style="COLOR: #008080">/*@H404_0@<SPAN style="COLOR: #008080">--调用示例
select 数据库文件目录=dbo.f_getdbpath('tempdb')
,[默认sql SERVER数据目录]=dbo.f_getdbpath('')
,[默认sql SERVER备份目录]=dbo.fgetdbpath(null)
--@H
4040@<SPAN style="COLOR: #008080">*/@H4040@
<SPAN style="COLOR: #0000ff">if@H
4040@ <SPAN style="COLOR: #808080">exists@H4040@ (<SPAN style="COLOR: #0000ff">select@H4040@ <SPAN style="COLOR: #808080">*@H4040@ <SPAN style="COLOR: #0000ff">from@H4040@ dbo.sysobjects <SPAN style="COLOR: #0000ff">where@H4040@ id <SPAN style="COLOR: #808080">=@H404_0@ <SPAN style="COLOR: #ff00ff">objectid@H4040@(N<SPAN style="COLOR: #ff0000">'@H404_0@<SPAN style="COLOR: #ff0000">[dbo].[fgetdbpath]@H4040@<SPAN style="COLOR: #ff0000">'@H4040@) <SPAN style="COLOR: #808080">and@H4040@ xtype <SPAN style="COLOR: #808080">in@H4040@ (N<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">FN@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,N<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">IF@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,N<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">TF@H4040@<SPAN style="COLOR: #ff0000">'@H4040@))
<SPAN style="COLOR: #0000ff">drop@H
4040@ <SPAN style="COLOR: #0000ff">function@H4040@ <SPAN style="COLOR: #ff0000">[@H4040@<SPAN style="COLOR: #ff0000">dbo@H4040@<SPAN style="COLOR: #ff0000">]@H4040@.<SPAN style="COLOR: #ff0000">[@H404_0@<SPAN style="COLOR: #ff0000">fgetdbpath@H4040@<SPAN style="COLOR: #ff0000">]@H4040@
<SPAN style="COLOR: #0000ff">GO@H
4040@<SPAN style="COLOR: #0000ff">create@H4040@ <SPAN style="COLOR: #0000ff">function@H404_0@ fgetdbpath(<SPAN style="COLOR: #008000">@dbname@H4040@ sysname)
<SPAN style="COLOR: #0000ff">returns@H
4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260@H4040@)
<SPAN style="COLOR: #0000ff">as@H
4040@
<SPAN style="COLOR: #0000ff">begin@H
4040@
<SPAN style="COLOR: #0000ff">declare@H
4040@ <SPAN style="COLOR: #008000">@re@H4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260@H4040@)
<SPAN style="COLOR: #0000ff">if@H
4040@ <SPAN style="COLOR: #008000">@dbname@H4040@ <SPAN style="COLOR: #0000ff">is@H4040@ <SPAN style="COLOR: #0000ff">null@H4040@ <SPAN style="COLOR: #808080">or@H404_0@ <SPAN style="COLOR: #ff00ff">dbid@H4040@(<SPAN style="COLOR: #008000">@dbname@H4040@) <SPAN style="COLOR: #0000ff">is@H4040@ <SPAN style="COLOR: #0000ff">null@H4040@
<SPAN style="COLOR: #0000ff">select@H
4040@ <SPAN style="COLOR: #008000">@re@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">rtrim@H4040@(<SPAN style="COLOR: #ff00ff">reverse@H4040@(filename)) <SPAN style="COLOR: #0000ff">from@H4040@ master..sysdatabases <SPAN style="COLOR: #0000ff">where@H4040@ name<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">master@H4040@<SPAN style="COLOR: #ff0000">'@H4040@
<SPAN style="COLOR: #0000ff">else@H
4040@
<SPAN style="COLOR: #0000ff">select@H
4040@ <SPAN style="COLOR: #008000">@re@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">rtrim@H4040@(<SPAN style="COLOR: #ff00ff">reverse@H4040@(filename)) <SPAN style="COLOR: #0000ff">from@H4040@ master..sysdatabases <SPAN style="COLOR: #0000ff">where@H4040@ name<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@dbname@H4040@<SPAN style="COLOR: #0000ff">if@H4040@ <SPAN style="COLOR: #008000">@dbname@H4040@ <SPAN style="COLOR: #0000ff">is@H4040@ <SPAN style="COLOR: #0000ff">null@H4040@
<SPAN style="COLOR: #0000ff">set@H
4040@ <SPAN style="COLOR: #008000">@re@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">reverse@H4040@(<SPAN style="COLOR: #ff00ff">substring@H4040@(<SPAN style="COLOR: #008000">@re@H4040@,<SPAN style="COLOR: #ff00ff">charindex@H4040@(<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">\@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@re@H4040@)<SPAN style="COLOR: #808080">+@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">5@H4040@,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260@H4040@))<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">BACKUP@H4040@<SPAN style="COLOR: #ff0000">'@H4040@
<SPAN style="COLOR: #0000ff">else@H
4040@
<SPAN style="COLOR: #0000ff">set@H
4040@ <SPAN style="COLOR: #008000">@re@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">reverse@H4040@(<SPAN style="COLOR: #ff00ff">substring@H4040@(<SPAN style="COLOR: #008000">@re@H4040@,<SPAN style="COLOR: #008000">@re@H4040@),<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260@H4040@))
<SPAN style="COLOR: #0000ff">return@H
4040@(<SPAN style="COLOR: #008000">@re@H4040@)
<SPAN style="COLOR: #0000ff">end@H
4040@
<SPAN style="COLOR: #0000ff">go@H
4040@
<SPAN style="COLOR: #008080">/*@H
404_0@<SPAN style="COLOR: #008080">2.--备份数据库@H_4040@<SPAN style="COLOR: #008080">*/@H4040@<SPAN style="COLOR: #008080">/*@H404_0@<SPAN style="COLOR: #008080">--调用示例--备份当前数据库
exec pbackupdb @bkpath='c:\',@bkfname='db\DATE_db.bak'--差异备份当前数据库
exec pbackupdb @bkpath='c:\',@bkfname='db\DATE_df.bak',@bktype='DF'--备份当前数据库日志
exec pbackupdb @bkpath='c:\',@bkfname='db\DATE_log.bak',@bktype='LOG'--@H_4040@<SPAN style="COLOR: #008080">*/@H4040@<SPAN style="COLOR: #0000ff">if@H4040@ <SPAN style="COLOR: #808080">exists@H4040@ (<SPAN style="COLOR: #0000ff">select@H4040@ <SPAN style="COLOR: #808080">*@H4040@ <SPAN style="COLOR: #0000ff">from@H4040@ dbo.sysobjects <SPAN style="COLOR: #0000ff">where@H4040@ id <SPAN style="COLOR: #808080">=@H404_0@ <SPAN style="COLOR: #ff00ff">objectid@H4040@(N<SPAN style="COLOR: #ff0000">'@H404_0@<SPAN style="COLOR: #ff0000">[dbo].[pbackupdb]@H4040@<SPAN style="COLOR: #ff0000">'@H4040@) <SPAN style="COLOR: #808080">and@H4040@ <SPAN style="COLOR: #ff00ff">OBJECTPROPERTY@H4040@(id,N<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">IsProcedure@H4040@<SPAN style="COLOR: #ff0000">'@H4040@) <SPAN style="COLOR: #808080">=@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@)
<SPAN style="COLOR: #0000ff">drop@H
4040@ <SPAN style="COLOR: #0000ff">procedure@H4040@ <SPAN style="COLOR: #ff0000">[@H4040@<SPAN style="COLOR: #ff0000">dbo@H4040@<SPAN style="COLOR: #ff0000">]@H4040@.<SPAN style="COLOR: #ff0000">[@H404_0@<SPAN style="COLOR: #ff0000">pbackupdb@H4040@<SPAN style="COLOR: #ff0000">]@H4040@
<SPAN style="COLOR: #0000ff">GO@H
4040@<SPAN style="COLOR: #0000ff">create@H4040@ <SPAN style="COLOR: #0000ff">proc@H404_0@ pbackupdb
<SPAN style="COLOR: #008000">@dbname@H
4040@ sysname<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">要备份的数据库名称,不指定则备份当前数据库 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@bkpath@H4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">备份文件的存放目录,不指定则使用sql默认的备份目录 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@bkfname@H4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">备份文件名,文件名中可以用\DBNAME\代表数据库名,\DATE\代表日期,\TIME\代表时间 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@bktype@H4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">10@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DB@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">备份类型:'DB'备份数据库,'DF' 差异备份,'LOG' 日志备份 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@appendfile@H4040@ <SPAN style="COLOR: #0000ff">bit@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@ <SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">追加/覆盖备份文件 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">as@H4040@
<SPAN style="COLOR: #0000ff">declare@H
404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000@H4040@)
<SPAN style="COLOR: #0000ff">if@H
4040@ <SPAN style="COLOR: #ff00ff">isnull@H4040@(<SPAN style="COLOR: #008000">@dbname@H4040@,<SPAN style="COLOR: #ff0000">''@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@ <SPAN style="COLOR: #0000ff">set@H4040@ <SPAN style="COLOR: #008000">@dbname@H4040@<SPAN style="COLOR: #808080">=@H404_0@<SPAN style="COLOR: #ff00ff">dbname@H4040@()
<SPAN style="COLOR: #0000ff">if@H
4040@ <SPAN style="COLOR: #ff00ff">isnull@H4040@(<SPAN style="COLOR: #008000">@bkpath@H4040@,<SPAN style="COLOR: #ff0000">''@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@ <SPAN style="COLOR: #0000ff">set@H4040@ <SPAN style="COLOR: #008000">@bkpath@H4040@<SPAN style="COLOR: #808080">=@H404_0@dbo.fgetdbpath(<SPAN style="COLOR: #0000ff">null@H4040@)
<SPAN style="COLOR: #0000ff">if@H
4040@ <SPAN style="COLOR: #ff00ff">isnull@H4040@(<SPAN style="COLOR: #008000">@bkfname@H4040@,<SPAN style="COLOR: #ff0000">''@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@ <SPAN style="COLOR: #0000ff">set@H4040@ <SPAN style="COLOR: #008000">@bkfname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">\DBNAME_\DATE_\TIME.BAK@H4040@<SPAN style="COLOR: #ff0000">'@H4040@
<SPAN style="COLOR: #0000ff">set@H
4040@ <SPAN style="COLOR: #008000">@bkfname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">replace@H4040@(<SPAN style="COLOR: #ff00ff">replace@H4040@(<SPAN style="COLOR: #ff00ff">replace@H4040@(<SPAN style="COLOR: #008000">@bkfname@H4040@,<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">\DBNAME\@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@dbname@H4040@)
,<SPAN style="COLOR: #ff0000">'@H
4040@<SPAN style="COLOR: #ff0000">\DATE\@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #ff00ff">convert@H4040@(<SPAN style="COLOR: #0000ff">varchar@H4040@,<SPAN style="COLOR: #ff00ff">getdate@H4040@(),<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">112@H4040@))
,<SPAN style="COLOR: #ff0000">'@H
4040@<SPAN style="COLOR: #ff0000">\TIME\@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #ff00ff">replace@H4040@(<SPAN style="COLOR: #ff00ff">convert@H4040@(<SPAN style="COLOR: #0000ff">varchar@H4040@,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">108@H4040@),<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">:@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #ff0000">''@H4040@))
<SPAN style="COLOR: #0000ff">set@H
404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">backup @H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@bktype@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">LOG@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">log @H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">database @H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@ <SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@dbname@H4040@
<SPAN style="COLOR: #808080">+@H
4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000"> to disk=@H4040@<SPAN style="COLOR: #ff0000">'''@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@bkpath@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@bkfname@H4040@
<SPAN style="COLOR: #808080">+@H
4040@<SPAN style="COLOR: #ff0000">'''@H4040@<SPAN style="COLOR: #ff0000"> with @H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@bktype@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DF@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DIFFERENTIAL,@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="COLOR: #ff0000">''@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@
<SPAN style="COLOR: #808080">+@H
4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@appendfile@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">NOINIT@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">INIT@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@
<SPAN style="COLOR: #0000ff">print@H
404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@
<SPAN style="COLOR: #0000ff">exec@H
404_0@(<SPAN style="COLOR: #008000">@sql@H_4040@)
<SPAN style="COLOR: #0000ff">go@H
4040@<SPAN style="COLOR: #008080">/*@H404_0@<SPAN style="COLOR: #008080">3.--恢复数据库@H_4040@<SPAN style="COLOR: #008080">*/@H4040@<SPAN style="COLOR: #008080">/*@H404_0@<SPAN style="COLOR: #008080">--调用示例
--完整恢复数据库
exec p_RestoreDb @bkfile='c:\db_20031015_db.bak',@dbname='db'--差异备份恢复
exec p_RestoreDb @bkfile='c:\db_20031015_db.bak',@dbname='db',@retype='DBNOR'
exec p_backupdb @bkfile='c:\db_20031015_df.bak',@retype='DF'--日志备份恢复
exec p_RestoreDb @bkfile='c:\db_20031015_db.bak',@retype='DBNOR'
exec p_backupdb @bkfile='c:\db_20031015log.bak',@retype='LOG'--@H4040@<SPAN style="COLOR: #008080">*/@H4040@<SPAN style="COLOR: #0000ff">if@H4040@ <SPAN style="COLOR: #808080">exists@H4040@ (<SPAN style="COLOR: #0000ff">select@H4040@ <SPAN style="COLOR: #808080">*@H4040@ <SPAN style="COLOR: #0000ff">from@H4040@ dbo.sysobjects <SPAN style="COLOR: #0000ff">where@H4040@ id <SPAN style="COLOR: #808080">=@H404_0@ <SPAN style="COLOR: #ff00ff">objectid@H4040@(N<SPAN style="COLOR: #ff0000">'@H404_0@<SPAN style="COLOR: #ff0000">[dbo].[pRestoreDb]@H4040@<SPAN style="COLOR: #ff0000">'@H4040@) <SPAN style="COLOR: #808080">and@H4040@ <SPAN style="COLOR: #ff00ff">OBJECTPROPERTY@H4040@(id,N<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">IsProcedure@H4040@<SPAN style="COLOR: #ff0000">'@H4040@) <SPAN style="COLOR: #808080">=@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@)
<SPAN style="COLOR: #0000ff">drop@H
4040@ <SPAN style="COLOR: #0000ff">procedure@H4040@ <SPAN style="COLOR: #ff0000">[@H4040@<SPAN style="COLOR: #ff0000">dbo@H4040@<SPAN style="COLOR: #ff0000">]@H4040@.<SPAN style="COLOR: #ff0000">[@H404_0@<SPAN style="COLOR: #ff0000">pRestoreDb@H4040@<SPAN style="COLOR: #ff0000">]@H4040@
<SPAN style="COLOR: #0000ff">GO@H
4040@<SPAN style="COLOR: #0000ff">create@H4040@ <SPAN style="COLOR: #0000ff">proc@H404_0@ pRestoreDb
<SPAN style="COLOR: #008000">@bkfile@H
4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1000@H4040@),<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">定义要恢复的备份文件名 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@dbname@H4040@ sysname<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">定义恢复后的数据库名,默认为备份的文件名 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@dbpath@H4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">恢复后的数据库存放目录,不指定则为sql的默认数据目录 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@retype@H4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">10@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DB@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">恢复类型:'DB'完事恢复数据库,'DBNOR' 为差异恢复,日志恢复进行完整恢复,'DF' 差异备份的恢复,'LOG' 日志恢复 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@filenumber@H4040@ <SPAN style="COLOR: #0000ff">int@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">恢复的文件号 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@overexist@H4040@ <SPAN style="COLOR: #0000ff">bit@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">是否覆盖已经存在的数据库,仅@retype为 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@killuser@H4040@ <SPAN style="COLOR: #0000ff">bit@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@ <SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">是否关闭用户使用进程,仅@overexist=1时有效 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">as@H4040@
<SPAN style="COLOR: #0000ff">declare@H
404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000@H4040@)<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">得到恢复后的数据库名 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">if@H4040@ <SPAN style="COLOR: #ff00ff">isnull@H4040@(<SPAN style="COLOR: #008000">@dbname@H4040@,<SPAN style="COLOR: #ff0000">''@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@
<SPAN style="COLOR: #0000ff">select@H
404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">reverse@H4040@(<SPAN style="COLOR: #008000">@bkfile@H404_0@)
,<SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff00ff">charindex@H4040@(<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">.@H4040@<SPAN style="COLOR: #ff0000">'@H404_0@,<SPAN style="COLOR: #008000">@sql@H_4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@ <SPAN style="COLOR: #0000ff">then@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@
<SPAN style="COLOR: #0000ff">else@H
4040@ <SPAN style="COLOR: #ff00ff">substring@H404_0@(<SPAN style="COLOR: #008000">@sql@H_4040@,<SPAN style="COLOR: #ff00ff">charindex@H4040@(<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">.@H4040@<SPAN style="COLOR: #ff0000">'@H404_0@,<SPAN style="COLOR: #008000">@sql@H_4040@)<SPAN style="COLOR: #808080">+@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1000@H4040@) <SPAN style="COLOR: #0000ff">end@H404_0@
,<SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff00ff">charindex@H4040@(<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">\@H4040@<SPAN style="COLOR: #ff0000">'@H404_0@,<SPAN style="COLOR: #008000">@sql@H_4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@ <SPAN style="COLOR: #0000ff">then@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@
<SPAN style="COLOR: #0000ff">else@H
4040@ <SPAN style="COLOR: #808080">left@H404_0@(<SPAN style="COLOR: #008000">@sql@H_404_0@,<SPAN style="COLOR: #008000">@sql@H_4040@)<SPAN style="COLOR: #808080">-@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@) <SPAN style="COLOR: #0000ff">end@H4040@
,<SPAN style="COLOR: #008000">@dbname@H
4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">reverse@H404_0@(<SPAN style="COLOR: #008000">@sql@H_4040@)<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">得到恢复后的数据库存放目录 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">if@H4040@ <SPAN style="COLOR: #ff00ff">isnull@H4040@(<SPAN style="COLOR: #008000">@dbpath@H4040@,<SPAN style="COLOR: #ff0000">''@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@ <SPAN style="COLOR: #0000ff">set@H4040@ <SPAN style="COLOR: #008000">@dbpath@H4040@<SPAN style="COLOR: #808080">=@H404_0@dbo.fgetdbpath(<SPAN style="COLOR: #ff0000">''@H4040@)<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">生成数据库恢复语句 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">set@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">restore @H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@retype@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">LOG@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">log @H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">database @H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@dbname@H4040@
<SPAN style="COLOR: #808080">+@H
4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000"> from disk=@H4040@<SPAN style="COLOR: #ff0000">'''@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@bkfile@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff0000">''''@H4040@
<SPAN style="COLOR: #808080">+@H
4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000"> with file=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff00ff">cast@H4040@(<SPAN style="COLOR: #008000">@filenumber@H4040@ <SPAN style="COLOR: #0000ff">as@H4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@)
<SPAN style="COLOR: #808080">+@H
4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #008000">@overexist@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@ <SPAN style="COLOR: #808080">and@H4040@ <SPAN style="COLOR: #008000">@retype@H4040@ <SPAN style="COLOR: #808080">in@H4040@(<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DB@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DBNOR@H4040@<SPAN style="COLOR: #ff0000">'@H4040@) <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">,replace@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="COLOR: #ff0000">''@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@
<SPAN style="COLOR: #808080">+@H
4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@retype@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DBNOR@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">,NORECOVERY@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">,RECOVERY@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@
<SPAN style="COLOR: #0000ff">print@H
404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@
<SPAN style="COLOR: #008080">--@H
404_0@<SPAN style="COLOR: #008080">添加移动逻辑文件的处理 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">if@H4040@ <SPAN style="COLOR: #008000">@retype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DB@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #808080">or@H4040@ <SPAN style="COLOR: #008000">@retype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">DBNOR@H4040@<SPAN style="COLOR: #ff0000">'@H4040@
<SPAN style="COLOR: #0000ff">begin@H
4040@
<SPAN style="COLOR: #008080">--@H
404_0@<SPAN style="COLOR: #008080">从备份文件获取逻辑文件名 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">declare@H4040@ <SPAN style="COLOR: #008000">@lfn@H4040@ <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">128@H4040@),<SPAN style="COLOR: #008000">@tp@H4040@ <SPAN style="COLOR: #0000ff">char@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@),<SPAN style="COLOR: #008000">@i@H4040@ <SPAN style="COLOR: #0000ff">int@H4040@<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">创建临时表,保存获取的信息 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">create@H4040@ <SPAN style="COLOR: #0000ff">table@H4040@ #tb(ln <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">128@H4040@),pn <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260@H4040@),tp <SPAN style="COLOR: #0000ff">char@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@),fgn <SPAN style="COLOR: #0000ff">nvarchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">128@H4040@),sz numeric(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">20@H4040@,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@),Msz numeric(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">20@H4040@,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@))
<SPAN style="COLOR: #008080">--@H
404_0@<SPAN style="COLOR: #008080">从备份文件获取信息 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">insert@H4040@ <SPAN style="COLOR: #0000ff">into@H4040@ #tb <SPAN style="COLOR: #0000ff">exec@H4040@(<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">restore filelistonly from disk=@H4040@<SPAN style="COLOR: #ff0000">'''@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@bkfile@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff0000">''''@H4040@)
<SPAN style="COLOR: #0000ff">declare@H
4040@ #f <SPAN style="COLOR: #0000ff">cursor@H4040@ <SPAN style="COLOR: #0000ff">for@H4040@ <SPAN style="COLOR: #0000ff">select@H4040@ ln,tp <SPAN style="COLOR: #0000ff">from@H4040@ #tb
<SPAN style="COLOR: #0000ff">open@H
4040@ #f
<SPAN style="COLOR: #0000ff">fetch@H
4040@ <SPAN style="COLOR: #0000ff">next@H4040@ <SPAN style="COLOR: #0000ff">from@H4040@ #f <SPAN style="COLOR: #0000ff">into@H4040@ <SPAN style="COLOR: #008000">@lfn@H4040@,<SPAN style="COLOR: #008000">@tp@H4040@
<SPAN style="COLOR: #0000ff">set@H
4040@ <SPAN style="COLOR: #008000">@i@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@
<SPAN style="COLOR: #0000ff">while@H
404_0@ <SPAN style="FONT-WEIGHT: bold; COLOR: #008000">@@fetchstatus@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@
<SPAN style="COLOR: #0000ff">begin@H
4040@
<SPAN style="COLOR: #0000ff">select@H
404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H404_0@<SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">,move @H4040@<SPAN style="COLOR: #ff0000">'''@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@lfn@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff0000">'''@H4040@<SPAN style="COLOR: #ff0000"> to @H4040@<SPAN style="COLOR: #ff0000">'''@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@dbpath@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@dbname@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #ff00ff">cast@H4040@(<SPAN style="COLOR: #008000">@i@H4040@ <SPAN style="COLOR: #0000ff">as@H4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@)
<SPAN style="COLOR: #808080">+@H
4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@tp@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">D@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">.mdf@H4040@<SPAN style="COLOR: #ff0000">'''@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">.ldf@H4040@<SPAN style="COLOR: #ff0000">'''@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@
,<SPAN style="COLOR: #008000">@i@H
4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@i@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@
<SPAN style="COLOR: #0000ff">fetch@H
4040@ <SPAN style="COLOR: #0000ff">next@H4040@ <SPAN style="COLOR: #0000ff">from@H4040@ #f <SPAN style="COLOR: #0000ff">into@H4040@ <SPAN style="COLOR: #008000">@lfn@H4040@,<SPAN style="COLOR: #008000">@tp@H4040@
<SPAN style="COLOR: #0000ff">end@H
4040@
<SPAN style="COLOR: #0000ff">close@H
4040@ #f
<SPAN style="COLOR: #0000ff">deallocate@H
4040@ #f
<SPAN style="COLOR: #0000ff">end@H
4040@<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">关闭用户进程处理 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">if@H4040@ <SPAN style="COLOR: #008000">@overexist@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@ <SPAN style="COLOR: #808080">and@H4040@ <SPAN style="COLOR: #008000">@killuser@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@
<SPAN style="COLOR: #0000ff">begin@H
4040@
<SPAN style="COLOR: #0000ff">declare@H
4040@ <SPAN style="COLOR: #008000">@spid@H4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">20@H4040@)
<SPAN style="COLOR: #0000ff">declare@H
4040@ #spid <SPAN style="COLOR: #0000ff">cursor@H4040@ <SPAN style="COLOR: #0000ff">for@H4040@
<SPAN style="COLOR: #0000ff">select@H
4040@ spid<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">cast@H4040@(spid <SPAN style="COLOR: #0000ff">as@H4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">20@H4040@)) <SPAN style="COLOR: #0000ff">from@H4040@ master..sysprocesses <SPAN style="COLOR: #0000ff">where@H4040@ dbid<SPAN style="COLOR: #808080">=@H404_0@<SPAN style="COLOR: #ff00ff">dbid@H4040@(<SPAN style="COLOR: #008000">@dbname@H4040@)
<SPAN style="COLOR: #0000ff">open@H
4040@ #spid
<SPAN style="COLOR: #0000ff">fetch@H
4040@ <SPAN style="COLOR: #0000ff">next@H4040@ <SPAN style="COLOR: #0000ff">from@H4040@ #spid <SPAN style="COLOR: #0000ff">into@H4040@ <SPAN style="COLOR: #008000">@spid@H4040@
<SPAN style="COLOR: #0000ff">while@H
404_0@ <SPAN style="FONT-WEIGHT: bold; COLOR: #008000">@@fetchstatus@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@
<SPAN style="COLOR: #0000ff">begin@H
4040@
<SPAN style="COLOR: #0000ff">exec@H
4040@(<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">kill @H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #808080">+@H4040@<SPAN style="COLOR: #008000">@spid@H4040@)
<SPAN style="COLOR: #0000ff">fetch@H
4040@ <SPAN style="COLOR: #0000ff">next@H4040@ <SPAN style="COLOR: #0000ff">from@H4040@ #spid <SPAN style="COLOR: #0000ff">into@H4040@ <SPAN style="COLOR: #008000">@spid@H4040@
<SPAN style="COLOR: #0000ff">end@H
4040@
<SPAN style="COLOR: #0000ff">close@H
4040@ #spid
<SPAN style="COLOR: #0000ff">deallocate@H
4040@ #spid
<SPAN style="COLOR: #0000ff">end@H
4040@<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">恢复数据库 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">exec@H404_0@(<SPAN style="COLOR: #008000">@sql@H_4040@)<SPAN style="COLOR: #0000ff">go@H4040@<SPAN style="COLOR: #008080">/*@H4040@<SPAN style="COLOR: #008080">4.--创建作业@H4040@<SPAN style="COLOR: #008080">*/@H4040@<SPAN style="COLOR: #008080">/*@H404_0@<SPAN style="COLOR: #008080">--调用示例--每月执行的作业
exec p_createjob @jobname='mm',@sql='select from syscolumns',@freqtype='month'--每周执行的作业
exec p_createjob @jobname='ww',@freqtype='week'--每日执行的作业
exec p_createjob @jobname='a',@sql='select
from syscolumns'--每日执行的作业,每天隔4小时重复的作业
exec pcreatejob @jobname='b',@fsinterval=4--@H4040@<SPAN style="COLOR: #008080">*/@H4040@
<SPAN style="COLOR: #0000ff">if@H
4040@ <SPAN style="COLOR: #808080">exists@H4040@ (<SPAN style="COLOR: #0000ff">select@H4040@ <SPAN style="COLOR: #808080">*@H4040@ <SPAN style="COLOR: #0000ff">from@H4040@ dbo.sysobjects <SPAN style="COLOR: #0000ff">where@H4040@ id <SPAN style="COLOR: #808080">=@H404_0@ <SPAN style="COLOR: #ff00ff">objectid@H4040@(N<SPAN style="COLOR: #ff0000">'@H404_0@<SPAN style="COLOR: #ff0000">[dbo].[pcreatejob]@H4040@<SPAN style="COLOR: #ff0000">'@H4040@) <SPAN style="COLOR: #808080">and@H4040@ <SPAN style="COLOR: #ff00ff">OBJECTPROPERTY@H4040@(id,N<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">IsProcedure@H4040@<SPAN style="COLOR: #ff0000">'@H4040@) <SPAN style="COLOR: #808080">=@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@)
<SPAN style="COLOR: #0000ff">drop@H
4040@ <SPAN style="COLOR: #0000ff">procedure@H4040@ <SPAN style="COLOR: #ff0000">[@H4040@<SPAN style="COLOR: #ff0000">dbo@H4040@<SPAN style="COLOR: #ff0000">]@H4040@.<SPAN style="COLOR: #ff0000">[@H404_0@<SPAN style="COLOR: #ff0000">pcreatejob@H4040@<SPAN style="COLOR: #ff0000">]@H4040@
<SPAN style="COLOR: #0000ff">GO@H
4040@<SPAN style="COLOR: #0000ff">create@H4040@ <SPAN style="COLOR: #0000ff">proc@H404_0@ pcreatejob
<SPAN style="COLOR: #008000">@jobname@H
4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">100@H4040@),<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">作业名称 @H_4040@<SPAN style="COLOR: #008080">
@H
404_0@<SPAN style="COLOR: #008000">@sql@H_4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000@H4040@),<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">要执行的命令 @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@dbname@H4040@ sysname<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">默认为当前的数据库名 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@freqtype@H4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">6@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">day@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">时间周期,month 月,week 周,day 日 @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@fsinterval@H4040@ <SPAN style="COLOR: #0000ff">int@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">相对于每日的重复次数 @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #008000">@time@H4040@ <SPAN style="COLOR: #0000ff">int@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">170000@H4040@ <SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">开始执行时间,对于重复执行的作业,将从0点到23:59分 @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">as@H4040@
<SPAN style="COLOR: #0000ff">if@H
4040@ <SPAN style="COLOR: #ff00ff">isnull@H4040@(<SPAN style="COLOR: #008000">@dbname@H4040@,<SPAN style="COLOR: #ff0000">''@H4040@)<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@ <SPAN style="COLOR: #0000ff">set@H4040@ <SPAN style="COLOR: #008000">@dbname@H4040@<SPAN style="COLOR: #808080">=@H404_0@<SPAN style="COLOR: #ff00ff">dbname@H4040@()<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">创建作业 @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">exec@H404_0@ msdb..sp_add_job <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">创建作业步骤 @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">exec@H404_0@ msdb..sp_add_jobstep <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@jobname@H404_0@,
<SPAN style="COLOR: #008000">@stepname@H4040@ <SPAN style="COLOR: #808080">=@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">数据处理@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,
<SPAN style="COLOR: #008000">@subsystem@H
4040@ <SPAN style="COLOR: #808080">=@H4040@ <SPAN style="COLOR: #ff0000">'@H404_0@<SPAN style="COLOR: #ff0000">Tsql@H_4040@<SPAN style="COLOR: #ff0000">'@H404_0@,
<SPAN style="COLOR: #008000">@databasename@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@dbname@H4040@,
<SPAN style="COLOR: #008000">@command@H
4040@ <SPAN style="COLOR: #808080">=@H404_0@ <SPAN style="COLOR: #008000">@sql@H_404_0@,
<SPAN style="COLOR: #008000">@retryattempts@H4040@ <SPAN style="COLOR: #808080">=@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">5@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">重试次数 @H_4040@<SPAN style="COLOR: #008080">
@H
404_0@<SPAN style="COLOR: #008000">@retryinterval@H4040@ <SPAN style="COLOR: #808080">=@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">5@H4040@ <SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">重试间隔@H4040@<SPAN style="COLOR: #008080">
@H
4040@
<SPAN style="COLOR: #008080">--@H
4040@<SPAN style="COLOR: #008080">创建调度 @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">declare@H4040@ <SPAN style="COLOR: #008000">@ftype@H4040@ <SPAN style="COLOR: #0000ff">int@H4040@,<SPAN style="COLOR: #008000">@fstype@H4040@ <SPAN style="COLOR: #0000ff">int@H4040@,<SPAN style="COLOR: #008000">@ffactor@H4040@ <SPAN style="COLOR: #0000ff">int@H4040@
<SPAN style="COLOR: #0000ff">select@H
4040@ <SPAN style="COLOR: #008000">@ftype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@freqtype@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">day@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">4@H4040@
<SPAN style="COLOR: #0000ff">when@H
4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">week@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8@H4040@
<SPAN style="COLOR: #0000ff">when@H
4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">month@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">16@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@
,<SPAN style="COLOR: #008000">@fstype@H
4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@fsinterval@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@
<SPAN style="COLOR: #0000ff">if@H
4040@ <SPAN style="COLOR: #008000">@fsinterval@H4040@<SPAN style="COLOR: #808080"><>@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@ <SPAN style="COLOR: #0000ff">set@H4040@ <SPAN style="COLOR: #008000">@time@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@
<SPAN style="COLOR: #0000ff">set@H
4040@ <SPAN style="COLOR: #008000">@ffactor@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff00ff">case@H4040@ <SPAN style="COLOR: #008000">@freqtype@H4040@ <SPAN style="COLOR: #0000ff">when@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">day@H4040@<SPAN style="COLOR: #ff0000">'@H4040@ <SPAN style="COLOR: #0000ff">then@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0@H4040@ <SPAN style="COLOR: #0000ff">else@H4040@ <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@ <SPAN style="COLOR: #0000ff">end@H4040@<SPAN style="COLOR: #0000ff">EXEC@H404_0@ msdb..sp_add_jobschedule <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@jobname@H4040@,
<SPAN style="COLOR: #008000">@name@H
4040@ <SPAN style="COLOR: #808080">=@H4040@ <SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">时间安排@H4040@<SPAN style="COLOR: #ff0000">'@H404_0@,
<SPAN style="COLOR: #008000">@freqtype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@ftype@H4040@,<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">每天,8 每周,16 每月 @H4040@<SPAN style="COLOR: #008080">
@H
404_0@<SPAN style="COLOR: #008000">@freqinterval@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1@H4040@,<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">重复执行次数 @H_4040@<SPAN style="COLOR: #008080">
@H
404_0@<SPAN style="COLOR: #008000">@freq_subdaytype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@fstype@H4040@,<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">是否重复执行 @H4040@<SPAN style="COLOR: #008080">
@H
404_0@<SPAN style="COLOR: #008000">@freq_subdayinterval@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@fsinterval@H4040@,<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">重复周期 @H4040@<SPAN style="COLOR: #008080">
@H
404_0@<SPAN style="COLOR: #008000">@freq_recurrencefactor@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@ffactor@H404_0@,
<SPAN style="COLOR: #008000">@active_starttime@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #008000">@time@H4040@ <SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">下午17:00:00分执行@H4040@<SPAN style="COLOR: #008080">
@H
4040@
<SPAN style="COLOR: #0000ff">go@H
4040@<SPAN style="COLOR: #008080">/*@H404_0@<SPAN style="COLOR: #008080">--应用案例--备份方案:
完整备份(每个星期天一次)+差异备份(每天备份一次)+日志备份(每2小时备份一次)调用上面的存储过程来实现
--@H_4040@<SPAN style="COLOR: #008080">*/@H4040@<SPAN style="COLOR: #0000ff">declare@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000@H4040@)
<SPAN style="COLOR: #008080">--@H
4040@<SPAN style="COLOR: #008080">完整备份(每个星期天一次) @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">set@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H404_0@<SPAN style="COLOR: #ff0000">exec pbackupdb @dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">要备份的数据库名@H_4040@<SPAN style="COLOR: #ff0000">'''@H4040@
<SPAN style="COLOR: #0000ff">exec@H
404_0@ pcreatejob <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">每周备份@H4040@<SPAN style="COLOR: #ff0000">'@H404_0@,<SPAN style="COLOR: #008000">@sql@H_4040@,<SPAN style="COLOR: #008000">@freqtype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">week@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">差异备份(每天备份一次) @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">set@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H404_0@<SPAN style="COLOR: #ff0000">exec pbackupdb @dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">要备份的数据库名@H_4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">,@bktype=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@DF<SPAN style="COLOR: #ff0000">''@H4040@
<SPAN style="COLOR: #0000ff">exec@H
404_0@ pcreatejob <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">每天差异备份@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@freqtype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">day@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #008080">--@H4040@<SPAN style="COLOR: #008080">日志备份(每2小时备份一次) @H4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">set@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H404_0@<SPAN style="COLOR: #ff0000">exec pbackupdb @dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">要备份的数据库名@H_4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">,@bktype=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff00ff">LOG@H4040@<SPAN style="COLOR: #ff0000">''@H4040@
<SPAN style="COLOR: #0000ff">exec@H
404_0@ pcreatejob <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">每2小时日志备份@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@freqtype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">day@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@fsinterval@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">2@H4040@<SPAN style="COLOR: #008080">/*@H404_0@<SPAN style="COLOR: #008080">--应用案例2生产数据核心库:PRODUCE备份方案如下:
1.设置三个作业,分别对PRODUCE库进行每日备份,每周备份,每月备份
2.新建三个新库,分别命名为:每日备份,每月备份
3.建立三个作业,分别把三个备份库还原到以上的三个新库。目的:当用户在produce库中有任何的数据丢失时,均可以从上面的三个备份库中导入相应的TABLE数据。
--@H_4040@<SPAN style="COLOR: #008080">*/@H4040@<SPAN style="COLOR: #0000ff">declare@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@ <SPAN style="COLOR: #0000ff">varchar@H4040@(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000@H4040@)<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">1.建立每月备份和生成月备份数据库的作业,每月每1天下午16:40分进行: @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">set@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">
declare @path nvarchar(260),@fname nvarchar(100)
set @fname=@H
4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">PRODUCE@H_4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">+convert(varchar(10),getdate(),112)+@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">m.bak@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">
set @path=dbo.f_getdbpath(null)+@fname--备份
exec pbackupdb @dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">PRODUCE@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">,@bkfname=@fname--根据备份生成每月新库
exec pRestoreDb @bkfile=@path,@dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">PRODUCE月@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">--为周数据库恢复准备基础数据库
exec pRestoreDb @bkfile=@path,@dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">PRODUCE周@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">,@retype=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">DBNOR@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">--为日数据库恢复准备基础数据库
exec pRestoreDb @bkfile=@path,@dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">PRODUCE日@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">,@retype=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">DBNOR@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">
@H
4040@<SPAN style="COLOR: #ff0000">'@H4040@
<SPAN style="COLOR: #0000ff">exec@H
404_0@ pcreatejob <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">每月备份@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@freqtype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">month@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@time@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">164000@H4040@<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">2.建立每周差异备份和生成周备份数据库的作业,每周日下午17:00分进行: @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">set@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">
declare @path nvarchar(260),112)+@H
4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">w.bak@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">
set @path=dbo.f_getdbpath(null)+@fname--差异备份
exec pbackupdb @dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">PRODUCE@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">,@bkfname=@fname,@bktype=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">DF@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">--差异恢复周数据库
exec pbackupdb @bkfile=@path,@retype=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">DF@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">
@H
4040@<SPAN style="COLOR: #ff0000">'@H4040@
<SPAN style="COLOR: #0000ff">exec@H
404_0@ pcreatejob <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">每周差异备份@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@freqtype@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">week@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@time@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">170000@H4040@<SPAN style="COLOR: #008080">--@H404_0@<SPAN style="COLOR: #008080">3.建立每日日志备份和生成日备份数据库的作业,每周日下午17:15分进行: @H_4040@<SPAN style="COLOR: #008080">
@H
4040@<SPAN style="COLOR: #0000ff">set@H404_0@ <SPAN style="COLOR: #008000">@sql@H_4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">
declare @path nvarchar(260),112)+@H
4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">l.bak@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">
set @path=dbo.f_getdbpath(null)+@fname--日志备份
exec pbackupdb @dbname=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">PRODUCE@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">,@bktype=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">LOG@H4040@<SPAN style="COLOR: #ff0000">''@H404_0@<SPAN style="COLOR: #ff0000">--日志恢复日数据库
exec pbackupdb @bkfile=@path,@retype=@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">LOG@H4040@<SPAN style="COLOR: #ff0000">''@H4040@<SPAN style="COLOR: #ff0000">
@H
4040@<SPAN style="COLOR: #ff0000">'@H4040@
<SPAN style="COLOR: #0000ff">exec@H
404_0@ pcreatejob <SPAN style="COLOR: #008000">@jobname@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="COLOR: #ff0000">'@H4040@<SPAN style="COLOR: #ff0000">每周差异备份@H4040@<SPAN style="COLOR: #ff0000">'@H4040@,<SPAN style="COLOR: #008000">@time@H4040@<SPAN style="COLOR: #808080">=@H4040@<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">171500@H404_0@

猜你在找的MsSQL相关文章