通过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 pcreatejob <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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


本篇内容主要讲解“sqlalchemy的常用数据类型怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“sqlalche...
今天小编给大家分享一下sqlServer实现分页查询的方式有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家
这篇文章主要介绍“sqlmap之osshell怎么使用”,在日常操作中,相信很多人在sqlmap之osshell怎么使用问题上存在疑惑,小编查阅了各式资料,整理出
本篇内容介绍了“SQL注入的知识点有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧...
1. mssql权限sa权限:数据库操作,文件管理,命令执行,注册表读取等system。是mssql的最高权限db权限:文件管理,数据库操作等 users-administratorspublic权限:数据库操作 guest-users2、sql server注入执行命令查
sql执行计划如何查看?在SPL庞大的数据中我们不知道如何查看实际数据库中发生了什么事情,有必要定期进行查询优化和索引否则会影响我们后期的SQL的查询速度。那么针对这样的问题我们必须要知道SQL执行的计划,在本文中winwin7小编给大家分享下SQL执
SQL Server 是Microsoft 公司推出的关系型数据库管理系统。具有使用方便可伸缩性好与相关软件集成程度高等优点应用非常广泛。不过在使用中,我们会遇到非常多的错误,面对这么庞大的数据库环境,当然会有精确的错误代码的对照季,下面小编分享的
SQL Server本地账户无法登陆出现错误提示:error:40-Could not open a connenction to SQL Server的问题很常见,对于初学者来说可能不知道如何解决,一起来看看下面的解决方案。解决步骤如下:1、这种情况需要开启 SQL Server service
微软推出的SQL2008是一款非常好用的数据库软件,它稳定、功能强大,为众多企业提供了最佳的数据库解决方案,那么我们如何在Windows中安装它呢,一些朋友对SQL Server 2008的安装过程还不是很熟悉,下面就一起来看看SQL Server 2008详细安装图解...
本页概要如果您使用的是 SQL Server 2005备份和还原Sp_detach_db 和 Sp_attach_db 存储过程关于排序规则的说明导入和导出数据(在 SQL Server 数据库之间复
DBCC CHECKIDENT 检查指定表的当前标识值,如有必要,还对标识值进行更正。 语法 DBCC CHECKIDENT ( &#39;table_name&#39; [ , { NORESEED
这里对 SQL Server 字符串函数进行分门别类地列出,便于查阅和记忆,相信大家都在其它方面有高深的编程基础,从字面上来说大家都知道这些函数的意义,就不对这些函数作过多的解释了,主要谈些经验,具体
查询及删除重复记录的方法 1、查找表(people)中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select*frompeoplewherepeopleIdin(selectpe
微软发SQL Server 2008第二个CTP预览版from: http://news.csdn.net/n/20070807/107158.html8月7日消息,微软公司本周发布了SQL Serv
症状当您将数据库备份恢复到另一台服务器时,可能会遇到孤立用户的问题。SQL Server 联机丛书中的孤立用户疑难解答主题中没有讲述解决此问题的具体步骤。本文介绍了如何解决孤立用户问题。更多信息虽然术
当登录SQL Server 2005时可能碰到错误: &#39;No Process is on the Other End of the Pipe&#39;。解决方法:(1)Open up SQL
概要本文描述如何映射标准登录和集成登录来解决在运行 SQL Server 的服务器之间移动数据库时的权限问题。更多信息当您将数据库从一个运行 SQL Server 的服务器移到另一个运行 SQL Se
----------------------------------------问题:该用户与可信的SQL SERVER 连接无关联使用sa用户或自建用户使用“SQL SERVER 身份认证”连接数据
更新日期: 2007 年 5 月 20 日 使用下表可以确定各种版本的 Microsoft SQL Server 2005 支持哪些功能。有关 SQL Server 2005 Enterprise E
当从Excel导入数据到Sql Sever中,可能会出现以下问题:&#xD;&#xA;对于指定的缓冲区大小而言,源列的数据太大