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

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

<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">利用T-sql语句,实现数据库的备份与还原的功能 <SPAN style="COLOR: #008080">
--
--<SPAN style="COLOR: #008080">体现了sql Server中的四个知识点: <SPAN style="COLOR: #008080">
--
--<SPAN style="COLOR: #008080">1. 获取sql Server服务器上的默认目录 <SPAN style="COLOR: #008080">
--
--<SPAN style="COLOR: #008080">2. 备份sql语句的使用 <SPAN style="COLOR: #008080">
--
--<SPAN style="COLOR: #008080">3. 恢复sql语句的使用,同时考虑了强制恢复时关闭其他用户进程的处理 <SPAN style="COLOR: #008080">
--
--<SPAN style="COLOR: #008080">4. 作业创建sql语句的使用<SPAN style="COLOR: #008080">

<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">1.--得到数据库文件目录@dbname 指定要取得目录的数据库
如果指定的数据不存在,返回安装sql时设置的默认数据目录
如果指定NULL,则返回默认的sql备份目录名
<SPAN style="COLOR: #008080">
/<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">--调用示例
select 数据库文件目录=dbo.f_getdbpath('tempdb')
,[默认sql SERVER数据目录]=dbo.f_getdbpath('')
,[默认sql SERVER备份目录]=dbo.f_getdbpath(null)
--<SPAN style="COLOR: #008080">
/
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #808080">exists (<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #808080"> <SPAN style="COLOR: #0000ff">from dbo.sysobjects <SPAN style="COLOR: #0000ff">where id <SPAN style="COLOR: #808080">= <SPAN style="COLOR: #ff00ff">object_id(N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">[dbo].[f_getdbpath]<SPAN style="COLOR: #ff0000">') <SPAN style="COLOR: #808080">and xtype <SPAN style="COLOR: #808080">in (N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">FN<SPAN style="COLOR: #ff0000">',N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">IF<SPAN style="COLOR: #ff0000">',N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">TF<SPAN style="COLOR: #ff0000">'))
<SPAN style="COLOR: #0000ff">drop <SPAN style="COLOR: #0000ff">function <SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000">dbo<SPAN style="COLOR: #ff0000">].<SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000">f_getdbpath<SPAN style="COLOR: #ff0000">]
<SPAN style="COLOR: #0000ff">GO<SPAN style="COLOR: #0000ff">create <SPAN style="COLOR: #0000ff">function f_getdbpath(<SPAN style="COLOR: #008000">@dbname sysname)
<SPAN style="COLOR: #0000ff">returns <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260)
<SPAN style="COLOR: #0000ff">as
<SPAN style="COLOR: #0000ff">begin
<SPAN style="COLOR: #0000ff">declare <SPAN style="COLOR: #008000">@re <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260)
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #008000">@dbname <SPAN style="COLOR: #0000ff">is <SPAN style="COLOR: #0000ff">null <SPAN style="COLOR: #808080">or <SPAN style="COLOR: #ff00ff">db_id(<SPAN style="COLOR: #008000">@dbname) <SPAN style="COLOR: #0000ff">is <SPAN style="COLOR: #0000ff">null
<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #008000">@re<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">rtrim(<SPAN style="COLOR: #ff00ff">reverse(filename)) <SPAN style="COLOR: #0000ff">from master..sysdatabases <SPAN style="COLOR: #0000ff">where name<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">master<SPAN style="COLOR: #ff0000">'
<SPAN style="COLOR: #0000ff">else
<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #008000">@re<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">rtrim(<SPAN style="COLOR: #ff00ff">reverse(filename)) <SPAN style="COLOR: #0000ff">from master..sysdatabases <SPAN style="COLOR: #0000ff">where name<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@dbname<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #008000">@dbname <SPAN style="COLOR: #0000ff">is <SPAN style="COLOR: #0000ff">null
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@re<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">reverse(<SPAN style="COLOR: #ff00ff">substring(<SPAN style="COLOR: #008000">@re,<SPAN style="COLOR: #ff00ff">charindex(<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">\<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@re)<SPAN style="COLOR: #808080">+<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">5,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260))<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">BACKUP<SPAN style="COLOR: #ff0000">'
<SPAN style="COLOR: #0000ff">else
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@re<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">reverse(<SPAN style="COLOR: #ff00ff">substring(<SPAN style="COLOR: #008000">@re,<SPAN style="COLOR: #008000">@re),<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260))
<SPAN style="COLOR: #0000ff">return(<SPAN style="COLOR: #008000">@re)
<SPAN style="COLOR: #0000ff">end
<SPAN style="COLOR: #0000ff">go
<SPAN style="COLOR: #008080">/
<SPAN style="COLOR: #008080">2.--备份数据库<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">/<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'--<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #808080">exists (<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #808080"> <SPAN style="COLOR: #0000ff">from dbo.sysobjects <SPAN style="COLOR: #0000ff">where id <SPAN style="COLOR: #808080">= <SPAN style="COLOR: #ff00ff">object_id(N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">[dbo].[p_backupdb]<SPAN style="COLOR: #ff0000">') <SPAN style="COLOR: #808080">and <SPAN style="COLOR: #ff00ff">OBJECTPROPERTY(id,N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">IsProcedure<SPAN style="COLOR: #ff0000">') <SPAN style="COLOR: #808080">= <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1)
<SPAN style="COLOR: #0000ff">drop <SPAN style="COLOR: #0000ff">procedure <SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000">dbo<SPAN style="COLOR: #ff0000">].<SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000">p_backupdb<SPAN style="COLOR: #ff0000">]
<SPAN style="COLOR: #0000ff">GO<SPAN style="COLOR: #0000ff">create <SPAN style="COLOR: #0000ff">proc p_backupdb
<SPAN style="COLOR: #008000">@dbname sysname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">要备份的数据库名称,不指定则备份当前数据库 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@bkpath <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260)<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">备份文件的存放目录,不指定则使用sql默认的备份目录 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@bkfname <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260)<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">备份文件名,文件名中可以用\DBNAME\代表数据库名,\DATE\代表日期,\TIME\代表时间 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@bktype <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">10)<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DB<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">备份类型:'DB'备份数据库,'DF' 差异备份,'LOG' 日志备份 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@appendfile <SPAN style="COLOR: #0000ff">bit<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1 <SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">追加/覆盖备份文件 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">as
<SPAN style="COLOR: #0000ff">declare <SPAN style="COLOR: #008000">@sql <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000)
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #ff00ff">isnull(<SPAN style="COLOR: #008000">@dbname,<SPAN style="COLOR: #ff0000">'')<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'' <SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@dbname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">db_name()
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #ff00ff">isnull(<SPAN style="COLOR: #008000">@bkpath,<SPAN style="COLOR: #ff0000">'')<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'' <SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@bkpath<SPAN style="COLOR: #808080">=dbo.fgetdbpath(<SPAN style="COLOR: #0000ff">null)
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #ff00ff">isnull(<SPAN style="COLOR: #008000">@bkfname,<SPAN style="COLOR: #ff0000">'')<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'' <SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@bkfname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">\DBNAME_\DATE\
\TIME.BAK<SPAN style="COLOR: #ff0000">'
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@bkfname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">replace(<SPAN style="COLOR: #ff00ff">replace(<SPAN style="COLOR: #ff00ff">replace(<SPAN style="COLOR: #008000">@bkfname,<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">\DBNAME\<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@dbname)
,<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">\DATE\<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #ff00ff">convert(<SPAN style="COLOR: #0000ff">varchar,<SPAN style="COLOR: #ff00ff">getdate(),<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">112))
,<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">\TIME\<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #ff00ff">replace(<SPAN style="COLOR: #ff00ff">convert(<SPAN style="COLOR: #0000ff">varchar,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">108),<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">:<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #ff0000">''))
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">backup <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@bktype <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">LOG<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">log <SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">database <SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">end <SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@dbname
<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000"> to disk=<SPAN style="COLOR: #ff0000">'''<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@bkpath<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@bkfname
<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">'''<SPAN style="COLOR: #ff0000"> with <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@bktype <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DF<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DIFFERENTIAL,<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #ff0000">'' <SPAN style="COLOR: #0000ff">end
<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@appendfile <SPAN style="COLOR: #0000ff">when <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1 <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">NOINIT<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">INIT<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">end
<SPAN style="COLOR: #0000ff">print <SPAN style="COLOR: #008000">@sql
<SPAN style="COLOR: #0000ff">exec(<SPAN style="COLOR: #008000">@sql)
<SPAN style="COLOR: #0000ff">go<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">3.--恢复数据库<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">/<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_20031015_log.bak',@retype='LOG'--<SPAN style="COLOR: #008080">
/<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #808080">exists (<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #808080">* <SPAN style="COLOR: #0000ff">from dbo.sysobjects <SPAN style="COLOR: #0000ff">where id <SPAN style="COLOR: #808080">= <SPAN style="COLOR: #ff00ff">object_id(N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">[dbo].[p_RestoreDb]<SPAN style="COLOR: #ff0000">') <SPAN style="COLOR: #808080">and <SPAN style="COLOR: #ff00ff">OBJECTPROPERTY(id,N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">IsProcedure<SPAN style="COLOR: #ff0000">') <SPAN style="COLOR: #808080">= <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1)
<SPAN style="COLOR: #0000ff">drop <SPAN style="COLOR: #0000ff">procedure <SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000">dbo<SPAN style="COLOR: #ff0000">].<SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000">p_RestoreDb<SPAN style="COLOR: #ff0000">]
<SPAN style="COLOR: #0000ff">GO<SPAN style="COLOR: #0000ff">create <SPAN style="COLOR: #0000ff">proc p_RestoreDb
<SPAN style="COLOR: #008000">@bkfile <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1000),<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">定义要恢复的备份文件名 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@dbname sysname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">定义恢复后的数据库名,默认为备份的文件名 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@dbpath <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260)<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">恢复后的数据库存放目录,不指定则为sql的默认数据目录 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@retype <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">10)<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DB<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">恢复类型:'DB'完事恢复数据库,'DBNOR' 为差异恢复,日志恢复进行完整恢复,'DF' 差异备份的恢复,'LOG' 日志恢复 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@filenumber <SPAN style="COLOR: #0000ff">int<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1,<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">恢复的文件号 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@overexist <SPAN style="COLOR: #0000ff">bit<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1,<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">是否覆盖已经存在的数据库,仅@retype为 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@killuser <SPAN style="COLOR: #0000ff">bit<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1 <SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">是否关闭用户使用进程,仅@overexist=1时有效 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">as
<SPAN style="COLOR: #0000ff">declare <SPAN style="COLOR: #008000">@sql <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000)<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">得到恢复后的数据库名 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #ff00ff">isnull(<SPAN style="COLOR: #008000">@dbname,<SPAN style="COLOR: #ff0000">'')<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">''
<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">reverse(<SPAN style="COLOR: #008000">@bkfile)
,<SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff00ff">charindex(<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">.<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@sql)<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0 <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #008000">@sql
<SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #ff00ff">substring(<SPAN style="COLOR: #008000">@sql,<SPAN style="COLOR: #ff00ff">charindex(<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">.<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@sql)<SPAN style="COLOR: #808080">+<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1000) <SPAN style="COLOR: #0000ff">end
,<SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff00ff">charindex(<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">\<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@sql)<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0 <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #008000">@sql
<SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #808080">left(<SPAN style="COLOR: #008000">@sql,<SPAN style="COLOR: #008000">@sql)<SPAN style="COLOR: #808080">-<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1) <SPAN style="COLOR: #0000ff">end
,<SPAN style="COLOR: #008000">@dbname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">reverse(<SPAN style="COLOR: #008000">@sql)<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">得到恢复后的数据库存放目录 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #ff00ff">isnull(<SPAN style="COLOR: #008000">@dbpath,<SPAN style="COLOR: #ff0000">'')<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'' <SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@dbpath<SPAN style="COLOR: #808080">=dbo.f_getdbpath(<SPAN style="COLOR: #ff0000">'')<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">生成数据库恢复语句 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">restore <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@retype <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">LOG<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">log <SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">database <SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">end<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@dbname
<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000"> from disk=<SPAN style="COLOR: #ff0000">'''<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@bkfile<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">''''
<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000"> with file=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">cast(<SPAN style="COLOR: #008000">@filenumber <SPAN style="COLOR: #0000ff">as <SPAN style="COLOR: #0000ff">varchar)
<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #008000">@overexist<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1 <SPAN style="COLOR: #808080">and <SPAN style="COLOR: #008000">@retype <SPAN style="COLOR: #808080">in(<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DB<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DBNOR<SPAN style="COLOR: #ff0000">') <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">,replace<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #ff0000">'' <SPAN style="COLOR: #0000ff">end
<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@retype <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DBNOR<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">,NORECOVERY<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">,RECOVERY<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">end
<SPAN style="COLOR: #0000ff">print <SPAN style="COLOR: #008000">@sql
<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">添加移动逻辑文件的处理 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #008000">@retype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DB<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #808080">or <SPAN style="COLOR: #008000">@retype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">DBNOR<SPAN style="COLOR: #ff0000">'
<SPAN style="COLOR: #0000ff">begin
<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">从备份文件获取逻辑文件名 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">declare <SPAN style="COLOR: #008000">@lfn <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">128),<SPAN style="COLOR: #008000">@tp <SPAN style="COLOR: #0000ff">char(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1),<SPAN style="COLOR: #008000">@i <SPAN style="COLOR: #0000ff">int<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">创建临时表,保存获取的信息 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">create <SPAN style="COLOR: #0000ff">table #tb(ln <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">128),pn <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">260),tp <SPAN style="COLOR: #0000ff">char(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1),fgn <SPAN style="COLOR: #0000ff">nvarchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">128),sz numeric(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">20,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0),Msz numeric(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">20,<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0))
<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">从备份文件获取信息 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">insert <SPAN style="COLOR: #0000ff">into #tb <SPAN style="COLOR: #0000ff">exec(<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">restore filelistonly from disk=<SPAN style="COLOR: #ff0000">'''<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@bkfile<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">'''')
<SPAN style="COLOR: #0000ff">declare #f <SPAN style="COLOR: #0000ff">cursor <SPAN style="COLOR: #0000ff">for <SPAN style="COLOR: #0000ff">select ln,tp <SPAN style="COLOR: #0000ff">from #tb
<SPAN style="COLOR: #0000ff">open #f
<SPAN style="COLOR: #0000ff">fetch <SPAN style="COLOR: #0000ff">next <SPAN style="COLOR: #0000ff">from #f <SPAN style="COLOR: #0000ff">into <SPAN style="COLOR: #008000">@lfn,<SPAN style="COLOR: #008000">@tp
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@i<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0
<SPAN style="COLOR: #0000ff">while <SPAN style="FONT-WEIGHT: bold; COLOR: #008000">@@fetch_status<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0
<SPAN style="COLOR: #0000ff">begin
<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">,move <SPAN style="COLOR: #ff0000">'''<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@lfn<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff0000">'''<SPAN style="COLOR: #ff0000"> to <SPAN style="COLOR: #ff0000">'''<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@dbpath<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@dbname<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">cast(<SPAN style="COLOR: #008000">@i <SPAN style="COLOR: #0000ff">as <SPAN style="COLOR: #0000ff">varchar)
<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@tp <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">D<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">.mdf<SPAN style="COLOR: #ff0000">''' <SPAN style="COLOR: #0000ff">else <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">.ldf<SPAN style="COLOR: #ff0000">''' <SPAN style="COLOR: #0000ff">end
,<SPAN style="COLOR: #008000">@i<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@i<SPAN style="COLOR: #808080">+<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1
<SPAN style="COLOR: #0000ff">fetch <SPAN style="COLOR: #0000ff">next <SPAN style="COLOR: #0000ff">from #f <SPAN style="COLOR: #0000ff">into <SPAN style="COLOR: #008000">@lfn,<SPAN style="COLOR: #008000">@tp
<SPAN style="COLOR: #0000ff">end
<SPAN style="COLOR: #0000ff">close #f
<SPAN style="COLOR: #0000ff">deallocate #f
<SPAN style="COLOR: #0000ff">end<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">关闭用户进程处理 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #008000">@overexist<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1 <SPAN style="COLOR: #808080">and <SPAN style="COLOR: #008000">@killuser<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1
<SPAN style="COLOR: #0000ff">begin
<SPAN style="COLOR: #0000ff">declare <SPAN style="COLOR: #008000">@spid <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">20)
<SPAN style="COLOR: #0000ff">declare #spid <SPAN style="COLOR: #0000ff">cursor <SPAN style="COLOR: #0000ff">for
<SPAN style="COLOR: #0000ff">select spid<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">cast(spid <SPAN style="COLOR: #0000ff">as <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">20)) <SPAN style="COLOR: #0000ff">from master..sysprocesses <SPAN style="COLOR: #0000ff">where dbid<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">db_id(<SPAN style="COLOR: #008000">@dbname)
<SPAN style="COLOR: #0000ff">open #spid
<SPAN style="COLOR: #0000ff">fetch <SPAN style="COLOR: #0000ff">next <SPAN style="COLOR: #0000ff">from #spid <SPAN style="COLOR: #0000ff">into <SPAN style="COLOR: #008000">@spid
<SPAN style="COLOR: #0000ff">while <SPAN style="FONT-WEIGHT: bold; COLOR: #008000">@@fetch_status<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0
<SPAN style="COLOR: #0000ff">begin
<SPAN style="COLOR: #0000ff">exec(<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">kill <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #808080">+<SPAN style="COLOR: #008000">@spid)
<SPAN style="COLOR: #0000ff">fetch <SPAN style="COLOR: #0000ff">next <SPAN style="COLOR: #0000ff">from #spid <SPAN style="COLOR: #0000ff">into <SPAN style="COLOR: #008000">@spid
<SPAN style="COLOR: #0000ff">end
<SPAN style="COLOR: #0000ff">close #spid
<SPAN style="COLOR: #0000ff">deallocate #spid
<SPAN style="COLOR: #0000ff">end<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">恢复数据库 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">exec(<SPAN style="COLOR: #008000">@sql)<SPAN style="COLOR: #0000ff">go<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">4.--创建作业<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">/<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 p_createjob @jobname='b',@fsinterval=4--<SPAN style="COLOR: #008080">
/
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #808080">exists (<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #808080">* <SPAN style="COLOR: #0000ff">from dbo.sysobjects <SPAN style="COLOR: #0000ff">where id <SPAN style="COLOR: #808080">= <SPAN style="COLOR: #ff00ff">object_id(N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">[dbo].[p_createjob]<SPAN style="COLOR: #ff0000">') <SPAN style="COLOR: #808080">and <SPAN style="COLOR: #ff00ff">OBJECTPROPERTY(id,N<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">IsProcedure<SPAN style="COLOR: #ff0000">') <SPAN style="COLOR: #808080">= <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1)
<SPAN style="COLOR: #0000ff">drop <SPAN style="COLOR: #0000ff">procedure <SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000">dbo<SPAN style="COLOR: #ff0000">].<SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000">p_createjob<SPAN style="COLOR: #ff0000">]
<SPAN style="COLOR: #0000ff">GO<SPAN style="COLOR: #0000ff">create <SPAN style="COLOR: #0000ff">proc p_createjob
<SPAN style="COLOR: #008000">@jobname <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">100),<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">作业名称 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@sql <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000),<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">要执行的命令 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@dbname sysname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">默认为当前的数据库名 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@freqtype <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">6)<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">day<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">时间周期,month 月,week 周,day 日 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@fsinterval <SPAN style="COLOR: #0000ff">int<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1,<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">相对于每日的重复次数 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@time <SPAN style="COLOR: #0000ff">int<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">170000 <SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">开始执行时间,对于重复执行的作业,将从0点到23:59分 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">as
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #ff00ff">isnull(<SPAN style="COLOR: #008000">@dbname,<SPAN style="COLOR: #ff0000">'')<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'' <SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@dbname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">db_name()<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">创建作业 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">exec msdb..sp_add_job <SPAN style="COLOR: #008000">@job_name<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@jobname<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">创建作业步骤 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">exec msdb..sp_add_jobstep <SPAN style="COLOR: #008000">@job_name<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@jobname,
<SPAN style="COLOR: #008000">@step_name <SPAN style="COLOR: #808080">= <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">数据处理<SPAN style="COLOR: #ff0000">',
<SPAN style="COLOR: #008000">@subsystem <SPAN style="COLOR: #808080">= <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">Tsql<SPAN style="COLOR: #ff0000">',
<SPAN style="COLOR: #008000">@database_name<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@dbname,
<SPAN style="COLOR: #008000">@command <SPAN style="COLOR: #808080">= <SPAN style="COLOR: #008000">@sql,
<SPAN style="COLOR: #008000">@retry_attempts <SPAN style="COLOR: #808080">= <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">5,<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">重试次数 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@retry_interval <SPAN style="COLOR: #808080">= <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">5 <SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">重试间隔<SPAN style="COLOR: #008080">

<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">创建调度 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">declare <SPAN style="COLOR: #008000">@ftype <SPAN style="COLOR: #0000ff">int,<SPAN style="COLOR: #008000">@fstype <SPAN style="COLOR: #0000ff">int,<SPAN style="COLOR: #008000">@ffactor <SPAN style="COLOR: #0000ff">int
<SPAN style="COLOR: #0000ff">select <SPAN style="COLOR: #008000">@ftype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@freqtype <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">day<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">4
<SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">week<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8
<SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">month<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">16 <SPAN style="COLOR: #0000ff">end
,<SPAN style="COLOR: #008000">@fstype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@fsinterval <SPAN style="COLOR: #0000ff">when <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1 <SPAN style="COLOR: #0000ff">then <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0 <SPAN style="COLOR: #0000ff">else <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8 <SPAN style="COLOR: #0000ff">end
<SPAN style="COLOR: #0000ff">if <SPAN style="COLOR: #008000">@fsinterval<SPAN style="COLOR: #808080"><><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1 <SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@time<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@ffactor<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff00ff">case <SPAN style="COLOR: #008000">@freqtype <SPAN style="COLOR: #0000ff">when <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">day<SPAN style="COLOR: #ff0000">' <SPAN style="COLOR: #0000ff">then <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0 <SPAN style="COLOR: #0000ff">else <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1 <SPAN style="COLOR: #0000ff">end<SPAN style="COLOR: #0000ff">EXEC msdb..sp_add_jobschedule <SPAN style="COLOR: #008000">@job_name<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@jobname,
<SPAN style="COLOR: #008000">@name <SPAN style="COLOR: #808080">= <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">时间安排<SPAN style="COLOR: #ff0000">',
<SPAN style="COLOR: #008000">@freq_type<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@ftype,<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">每天,8 每周,16 每月 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@freq_interval<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1,<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">重复执行次数 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@freq_subday_type<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@fstype,<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">是否重复执行 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@freq_subday_interval<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@fsinterval,<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">重复周期 <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #008000">@freq_recurrence_factor<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@ffactor,
<SPAN style="COLOR: #008000">@active_start_time<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #008000">@time <SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">下午17:00:00分执行<SPAN style="COLOR: #008080">

<SPAN style="COLOR: #0000ff">go<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">--应用案例--备份方案:
完整备份(每个星期天一次)+差异备份(每天备份一次)+日志备份(每2小时备份一次)调用上面的存储过程来实现
--<SPAN style="COLOR: #008080">
/<SPAN style="COLOR: #0000ff">declare <SPAN style="COLOR: #008000">@sql <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000)
<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">完整备份(每个星期天一次) <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">exec p_backupdb @dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">要备份的数据库名<SPAN style="COLOR: #ff0000">'''
<SPAN style="COLOR: #0000ff">exec p_createjob <SPAN style="COLOR: #008000">@jobname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">每周备份<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@sql,<SPAN style="COLOR: #008000">@freqtype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">week<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">差异备份(每天备份一次) <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">exec p_backupdb @dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">要备份的数据库名<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">,@bktype=<SPAN style="COLOR: #ff0000">'DF<SPAN style="COLOR: #ff0000">''
<SPAN style="COLOR: #0000ff">exec p_createjob <SPAN style="COLOR: #008000">@jobname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">每天差异备份<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@freqtype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">day<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">日志备份(每2小时备份一次) <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">exec p_backupdb @dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">要备份的数据库名<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">,@bktype=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff00ff">LOG<SPAN style="COLOR: #ff0000">''
<SPAN style="COLOR: #0000ff">exec p_createjob <SPAN style="COLOR: #008000">@jobname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">每2小时日志备份<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@freqtype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">day<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@fsinterval<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">2<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">--应用案例2生产数据核心库:PRODUCE备份方案如下:
1.设置三个作业,分别对PRODUCE库进行每日备份,每周备份,每月备份
2.新建三个新库,分别命名为:每日备份,每月备份
3.建立三个作业,分别把三个备份库还原到以上的三个新库。目的:当用户在produce库中有任何的数据丢失时,均可以从上面的三个备份库中导入相应的TABLE数据。
--<SPAN style="COLOR: #008080">
/<SPAN style="COLOR: #0000ff">declare <SPAN style="COLOR: #008000">@sql <SPAN style="COLOR: #0000ff">varchar(<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8000)<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">1.建立每月备份和生成月备份数据库的作业,每月每1天下午16:40分进行: <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">
declare @path nvarchar(260),@fname nvarchar(100)
set @fname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">PRODUCE_<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">+convert(varchar(10),getdate(),112)+<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">_m.bak<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">
set @path=dbo.f_getdbpath(null)+@fname--备份
exec p_backupdb @dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">PRODUCE<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">,@bkfname=@fname--根据备份生成每月新库
exec p_RestoreDb @bkfile=@path,@dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">PRODUCE_月<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">--为周数据库恢复准备基础数据库
exec p_RestoreDb @bkfile=@path,@dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">PRODUCE_周<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">,@retype=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">DBNOR<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">--为日数据库恢复准备基础数据库
exec p_RestoreDb @bkfile=@path,@dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">PRODUCE_日<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">,@retype=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">DBNOR<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">
<SPAN style="COLOR: #ff0000">'
<SPAN style="COLOR: #0000ff">exec p_createjob <SPAN style="COLOR: #008000">@jobname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">每月备份<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@freqtype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">month<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@time<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">164000<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">2.建立每周差异备份和生成周备份数据库的作业,每周日下午17:00分进行: <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">
declare @path nvarchar(260),112)+<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">_w.bak<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">
set @path=dbo.f_getdbpath(null)+@fname--差异备份
exec p_backupdb @dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">PRODUCE<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">,@bkfname=@fname,@bktype=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">DF<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">--差异恢复周数据库
exec p_backupdb @bkfile=@path,@retype=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">DF<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">
<SPAN style="COLOR: #ff0000">'
<SPAN style="COLOR: #0000ff">exec p_createjob <SPAN style="COLOR: #008000">@jobname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">每周差异备份<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@freqtype<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">week<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@time<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">170000<SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">3.建立每日日志备份和生成日备份数据库的作业,每周日下午17:15分进行: <SPAN style="COLOR: #008080">
<SPAN style="COLOR: #0000ff">set <SPAN style="COLOR: #008000">@sql<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">
declare @path nvarchar(260),112)+<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">_l.bak<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">
set @path=dbo.f_getdbpath(null)+@fname--日志备份
exec p_backupdb @dbname=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">PRODUCE<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">,@bktype=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">LOG<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">--日志恢复日数据库
exec p_backupdb @bkfile=@path,@retype=<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">LOG<SPAN style="COLOR: #ff0000">''<SPAN style="COLOR: #ff0000">
<SPAN style="COLOR: #ff0000">'
<SPAN style="COLOR: #0000ff">exec p_createjob <SPAN style="COLOR: #008000">@jobname<SPAN style="COLOR: #808080">=<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">每周差异备份<SPAN style="COLOR: #ff0000">',<SPAN style="COLOR: #008000">@time<SPAN style="COLOR: #808080">=<SPAN style="FONT-WEIGHT: bold; COLOR: #800000">171500

原文链接:https://www.f2er.com/mssql/63906.html

猜你在找的MsSQL相关文章