前两周,由于数据库简繁体的转换,大量的数据库需要转到新的服务器。 在转其中的一台的时候,原先导出来的JOBS脚本不翼而飞(原因至今未明),而恰巧这一台服务器有90多个JOB(看下图恢复后的,注意滚动条的长短),这样一来,可急坏了人了。 这么多的JOB要一个一个建立,时间去了不说,有些JOB根本不知道执行的是哪些命令,执行顺序如何,这么多的系统相互交纵复杂,如果不能正确的处理好,星期一可就乱了套了。到时谁都不会有好果子吃。 由于JOBS都是存在于msdb数据库,还好,我们平时是有备份这个数据库的,这样一来,我们应该想办法从这个数据库里取回一些有用的信息。 首先,我应该恢复一个msdb数据库为别的名字,然后赶紧求助于互联网。经过了一番摸索终于可以从该数据库中提取出脚本。 现在把这脚本也放出来,希望有此需要的人做参考使用,亦或大家做学习之用,如果大家有好的方法也请讲出。 当然,要是在系统完好之前,直接备份就用不着这么麻烦了,直接在图形介面产生的脚本又快又准确。

CREATE  PROCEDURE  Get_JobList
as
-- created by geton on 2014-08-16
declare @jobnamecc varchar(250)
declare @strsql varchar(5000)
declare @jobname varchar(30),@category_calss_i int,@category_calss varchar(50),@category_name varchar(50),
@category_type varchar(30),@category_id int,@category_type_i int declare joblist cursor for
select [name] from msdb.dbo.sysjobs --where name='HR - AUTO GET COE' open joblist
fetch next from joblist into @jobnamecc
while @@fetch_status=0
begin
begin try
select @jobname =@jobnamecc ,@category_calss = '',@category_name='',@category_type = '' select
@category_calss = case when tshc.category_class = 1 then 'JOB'
when tshc.category_class = 2 then 'ALERT'
else 'OPERATOR'
end,
@category_type = case when tshc.category_type = 1 then 'LOCAL'
when tshc.category_type = 2 then 'MULTI-SERVER'
else 'NONE'
end,
@category_name = tshc.name,
@category_type_i = category_type,
@category_calss_i = tshc.category_class,
@category_id = tshc.category_id
from
msdb.dbo.sysjobs_view as sv -- Here "msdb.dbo.sysjobs_view" should be changed to the DB which you restored
INNER JOIN msdb.dbo.syscategories as tshc on sv.category_id = tshc.category_id
where -- Here "msdb.dbo.syscategories" should be changed to the DB which you restored
(sv.name=@jobname AND tshc.category_class = 1) -- category_class = 1 -- only job type are exported PRINT 'BEGIN TRANSACTION'
PRINT 'DECLARE @ReturnCode INT'
PRINT 'SELECT @ReturnCode = 0'
PRINT 'IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N''' + @category_name +'''AND category_class=' +rtrim(@category_calss_i)+')'
PRINT 'BEGIN'
PRINT 'EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'''+ @category_calss+''', @type=N'''+@category_type+''', @name=N'''+@category_name+''''
PRINT 'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback'
PRINT 'end' declare @eventloglevel int,@emaillevel int,@netsendlevel int,@pagelevel int
declare @emailleveloprid nvarchar(256),@netsendleveloprid nvarchar(256),@pageleveloprid nvarchar(256)
declare @isenable int , @description nvarchar(1024),@owner_log_name nvarchar(512),@delete_level int
declare @jobid uniqueidentifier,@start_step_id int,@server nvarchar(512)
select
@eventloglevel=sv.notify_level_eventlog,
@emaillevel=sv.notify_level_email ,
@netsendlevel=sv.notify_level_netsend,
@pagelevel=sv.notify_level_page,
@emailleveloprid = isnull((select top 1 name from msdb..sysoperators where id = sv.notify_email_operator_id),''),
@netsendleveloprid = isnull((select top 1 name from msdb..sysoperators where id = sv.notify_netsend_operator_id),''),
@pageleveloprid = isnull((select top 1 name from msdb..sysoperators where id = sv.notify_page_operator_id),'') ,
@isenable = sv.enabled ,
@description = sv.description ,
@owner_log_name = isnull(suser_sname(sv.owner_sid), N''''),
@delete_level = sv.delete_level,
@jobid = sv.job_id,
@start_step_id = start_step_id ,
@server = '(local)'--originating_server --Due to this script would be executed on target machine,Here change the @server parameter to "LOCAL"
from msdb.dbo.sysjobs_view as sv
where (sv.name=@jobname ) -- select * from msdb.dbo.sysjobs_view
PRINT 'DECLARE @jobId BINARY(16)'
PRINT 'EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'''+@jobname+''','
PRINT ' @enabled='+RTRIM(@isenable)+', '
PRINT ' @notify_level_eventlog='+RTRIM(@eventloglevel)+', '
PRINT ' @notify_level_email='+RTRIM(@emaillevel)+', '
PRINT ' @notify_level_netsend='+RTRIM(@netsendlevel)+', '
PRINT ' @notify_level_page='+RTRIM(@pagelevel)+', '
PRINT ' @notify_email_operator_name ='''+RTRIM(@emailleveloprid)+''', '
PRINT ' @notify_netsend_operator_name='''+RTRIM(@netsendleveloprid)+''', '
PRINT ' @notify_page_operator_name='''+RTRIM(@pageleveloprid)+''', '
PRINT ' @delete_level='+RTRIM(@delete_level)+', '
PRINT ' @description=N'''+@description+''', '
PRINT ' @category_name=N'''+@category_name+''', '
PRINT ' @owner_login_name=N'''+@owner_log_name+''', '
PRINT ' @job_id = @jobId OUTPUT'
PRINT 'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback'
--SELECT * FROM msdb.dbo.syscategories declare @step_id int
declare @step_name nvarchar(512) ,@cmdexec_success_code int,@on_success_action int,@on_success_step_id int,
@on_fail_action int,@on_fail_step_id int,@retry_attempts int,@retry_interval int,@os_run_priority int,
@subsystem nvarchar(512),@database_name nvarchar(512),@flags int,@command nvarchar(max) declare jbcur cursor for
select step_id from msdb..sysjobsteps where job_id = @jobid order by step_id ;
-- "msdb..sysjobsteps" should be changed to the DB which you restored
open jbcur;
fetch next from jbcur into @step_id
while @@fetch_status = 0
begin SELECT
@step_name = step_name,
@cmdexec_success_code= cmdexec_success_code ,
@on_success_action = on_success_action ,
@on_success_step_id = on_success_step_id ,
@on_fail_action = on_fail_action,
@on_fail_step_id = on_fail_step_id ,
@retry_attempts = retry_attempts,
@retry_interval = retry_interval,
@os_run_priority = os_run_priority,
@subsystem = subsystem,
@database_name = database_name,
@command = command,
@flags = flags
FROM msdb..sysjobsteps a WHERE job_id = @jobid and step_id = @step_id PRINT ' EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, '
PRINT ' @step_name=N'''+@step_name+''', '
PRINT ' @step_id='+RTRIM(@step_id)+', '
PRINT ' @cmdexec_success_code='+RTRIM(@cmdexec_success_code)+', '
PRINT ' @on_success_action='+RTRIM(@on_success_action)+', '
PRINT ' @on_success_step_id='+RTRIM(@on_success_step_id)+', '
PRINT ' @on_fail_action='+RTRIM(@on_fail_action)+', '
PRINT ' @on_fail_step_id='+RTRIM(@on_fail_step_id)+', '
PRINT ' @retry_attempts='+RTRIM(@retry_attempts)+', '
PRINT ' @retry_interval='+RTRIM(@retry_interval)+', '
PRINT ' @os_run_priority='+RTRIM(@os_run_priority)+', @subsystem=N'''+@subsystem+''', '
PRINT ' @database_name=N'''+@database_name+''','
PRINT ' @flags='+RTRIM(@flags)+' ,'
PRINT ' @command=N'''+REPLACE(@command,'''','''''')+''''
PRINT ' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' fetch next from jbcur into @step_id end close jbcur
deallocate jbcur PRINT ' EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = '+rtrim(@start_step_id)
PRINT ' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback ' DECLARE @enabled INT,@freq_type INT,@freq_interval INT,@freq_subday_type INT,@freq_subday_interval INT
,@freq_relative_interval INT,@freq_recurrence_factor INT,@active_start_date INT,@active_end_date INT
,@active_start_time INT,@active_end_time INT,@name VARCHAR(512) SELECT
@name = a.name,
@enabled = enabled ,
@freq_interval = freq_interval ,
@freq_type = freq_type ,
@freq_subday_type=freq_subday_type,
@freq_subday_interval=freq_subday_interval,
@freq_relative_interval=freq_relative_interval ,
@freq_recurrence_factor=freq_recurrence_factor ,
@active_start_date=active_start_date ,
@active_end_date=active_end_date ,
@active_start_time=active_start_time ,
@active_end_time=active_end_time
FROM msdb..sysschedules a -- "msdb..sysschedules" should be changed to the DB which you restored
INNER JOIN msdb.dbo.sysjobschedules b ON a.schedule_id = b.schedule_id -- "msdb..sysjobschedules " should be changed to the DB which you restored
WHERE job_id = @jobid IF(@name IS not null)
begin
PRINT ' EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'''+@name+''', '
PRINT ' @enabled='+RTRIM(@enabled)+', '
PRINT ' @freq_type='+RTRIM(@freq_type)+', '
PRINT ' @freq_interval='+RTRIM(@freq_interval)+', '
PRINT ' @freq_subday_type='+RTRIM(@freq_subday_type)+', '
PRINT ' @freq_subday_interval='+RTRIM(@freq_subday_interval)+', '
PRINT ' @freq_relative_interval='+RTRIM(@freq_relative_interval)+', '
PRINT ' @freq_recurrence_factor='+RTRIM(@freq_recurrence_factor)+', '
PRINT ' @active_start_date='+RTRIM(@active_start_date)+', '
PRINT ' @active_end_date='+RTRIM(@active_end_date)+', '
PRINT ' @active_start_time='+RTRIM(@active_start_time)+', '
PRINT ' @active_end_time='+RTRIM(@active_end_time)+' '
--PRINT ' @schedule_uid=N'''+RTRIM(NEWID())+''''
PRINT ' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback'
end PRINT ' EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'''+@server+''''
PRINT ' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback'
PRINT 'COMMIT TRANSACTION'
PRINT 'GOTO endSave'
PRINT 'QuitWithRollback:'
PRINT 'IF(@@TRANCOUNT>0)ROLLBACK TRANSACTION'
PRINT 'endSave:'
PRINT ' '
PRINT 'GO' --PRINT @strsql
exec (@strsql)
end try
begin catch
declare @errmsg varchar(1000)
set @errmsg='[Prco_Get_IPDataBase]:'+ERROR_MESSAGE()
print @errmsg
print @strsql
end catch;
fetch next from joblist into @jobnamecc
end close joblist
deallocate joblist

Generate the Jobs script from msdb Database的更多相关文章

  1. P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1

    P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1       May ...

  2. ADF_Database Develop系列3_设计数据库表之Reconcile Database/Reverse Objects

    2013-05-01 Created By BaoXinjian

  3. ADF_Database Develop系列2_设计数据库表之Table Partitions/Create Users/Generate DDL

    2013-05-01 Created By BaoXinjian

  4. P6 EPPM Manual Installation Guide (Oracle Database)

    P6 EPPM Manual Installation Guide (Oracle Database) P6 EPPM Manual Installation Guide (Oracle Databa ...

  5. H2 Database入门

    H2 Database做为轻量级的内嵌数据库,功能十分强大,而且运行时只需要一个jar包即可,下表是官网的描述: 更详细的对比见官网页面: http://www.h2database.com/html ...

  6. 使用duplicate target database ... from active database复制数据库

    使用duplicate target database ... from active database复制数据库 source db:ora11auxiliary db:dupdb 1.修改监听文件 ...

  7. 使用RMAN Active duplicate创建异地auxiliary Database

    1g的RMAN duplicate 可以通过Active database duplicate和Backup-based duplicate两种方法实现.这里的测试使用的是Active databas ...

  8. EntityFramework4.1 MODEL代码生成器 database first

    原文发布时间为:2011-04-02 -- 来源于本人的百度文章 [由搬家工具导入] Generating EF Code First model classes from an existing d ...

  9. How to: Generate XPO Business Classes for Existing Data Tables 如何:为现有数据表生成 XPO 业务类

    From the Tutorial and other documentation sources, you learned how to create business classes for yo ...

随机推荐

  1. Apache二级域名配置方法

    下面这个Apache二级域名配置方法是今天在其它BBS看到的,以前我配置是都是配置每个为一个虚拟目录今天正在想如何写没想到找到了. Apache二级域名实现方法介绍 首先,你的拥有一个有泛域名解析的顶 ...

  2. Context上下文

    As described earlier, context refers to the state of the application during test playback. Because a ...

  3. 【转】Linux mount/unmount命令

    转自:http://www.cnblogs.com/xd502djj/p/3809375.html 格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有:-a 安装在/etc/fs ...

  4. DouNet学习_Excel导入导出

    Excel --->列是有限的 -->数据靠在单元格右边是数字类型,左边是字符串类型 把一个数字当初字符串来显示 在前面加个 ' -->用程序操作Excel 可以使用Excel的所有 ...

  5. Linux-sed用法

    本文为转载,原地址:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命 ...

  6. TopFreeTheme精选免费模板【20130629】

    今天给大家推荐9款最新精选的WordPress主题,它们涵盖了新闻.杂志.博客.房地产方面的主题.有些是商业模板,但现在都可以免费下载. GeoPlaces v4.6.2 – 来自Templatic的 ...

  7. NServiceBus-性能测试

    NServiceBus: 有效地处理一个消息 处理大量并发 尺度大小不同的服务器 尺度低规格的设备 的最终平衡速度和安全. 基准 许多参数会影响测量性能.最明显的是硬件服务器和CPU核的数量,大小的内 ...

  8. js运动 分享到

    <!doctype html> <html> <head> <meta charset = "utf-8"> <title&g ...

  9. DD_belatedPNG,解决 IE6 不支持 PNG-24 绝佳解决方案

    png24在ie下支持透明.终于找到下面的可行办法: 我们知道 IE6 是不支持透明的 PNG-24 的,这无疑限制了网页设计的发挥空间. 然而整个互联网上解决这个 IE6 的透明 PNG-24 的方 ...

  10. cocos2d-x 3.2 DrawNode 绘图API

    关于Cocos2d-x 3.x 版本的绘图方法有两种: 1.使用DrawNode类绘制自定义图形. 2.继承Layer类重写draw()方法. 以上两种方法都可以绘制自定义图形,根据自己的需要选择合适 ...