常用脚本--生成Agent Job的创建脚本
USE [master]
GO /****** Object: StoredProcedure [dbo].[DumpJobsql] Script Date: 02/07/2014 11:38:46 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE PROCEDURE [dbo].[usp_DumpJobsql]
AS
BEGIN /********************************************************************
* 过程说明: 只适用于不带邮件、报警的作业 *
* 需要参数: *
* @target_loginname 为需要创建的服务器的登录名 *
* @i_enabled 为是否执行( 0=禁用 ,1=启用 ) *
* *
* create by linda 08/22/12 12:21:47 PM *
* *
* 调用示例(需要以文本格式显示执行结果): *
* EXEC [dbo].[usp_DumpJobsql] * *
* *
********************************************************************/
--declare @i_enabled tinyint
--declare @i_target_loginname varchar(500)
--set @i_enabled = 0
--set @i_target_loginname ='aaa' SET NOCOUNT ON
DECLARE @i_enabled TINYINT
DECLARE @sql VARCHAR(MAX)
DECLARE @i_job_name VARCHAR(1000)
DECLARE @i_notify_level_eventlog INT
DECLARE @i_notify_level_email INT
DECLARE @i_notify_level_netsend INT
DECLARE @i_notify_level_page INT
DECLARE @i_delete_level INT
DECLARE @i_description VARCHAR(1000)
DECLARE @i_category_name VARCHAR(1000)
DECLARE @i_owner_login_name VARCHAR(1000)
DECLARE @i_category_class INT DECLARE @i_start_step_id INT
DECLARE @i_step_name VARCHAR(1000)
DECLARE @i_step_id INT
DECLARE @i_cmdexec_success_code INT
DECLARE @i_on_success_action INT
DECLARE @i_on_success_step_id INT
DECLARE @i_on_fail_action INT
DECLARE @i_on_fail_step_id INT
DECLARE @i_retry_attempts BIGINT
DECLARE @i_retry_interval INT
DECLARE @i_os_run_priority INT
DECLARE @i_subsystem VARCHAR(1000)
DECLARE @i_command VARCHAR(8000)
DECLARE @i_database_name VARCHAR(100)
DECLARE @i_flags INT DECLARE @i_class VARCHAR(10) ,@i_type VARCHAR(10)
DECLARE @c_jobid UNIQUEIDENTIFIER ,@c_categoryid INT DECLARE @loop_stepid INT
DECLARE @m_stepid INT
DECLARE @loop_scheduleid INT
DECLARE @m_scheduleid INT DECLARE @i_schedule_enabled TINYINT
DECLARE @i_freq_type INT
DECLARE @i_schedule_name VARCHAR(1000)
DECLARE @i_freq_interval INT
DECLARE @i_freq_subday_type INT
DECLARE @i_freq_subday_interval INT
DECLARE @i_freq_relative_interval INT
DECLARE @i_freq_recurrence_factor INT
DECLARE @i_active_start_date BIGINT
DECLARE @i_active_end_date BIGINT
DECLARE @i_active_start_time BIGINT
DECLARE @i_active_end_time BIGINT
DECLARE @i_schedule_uid VARCHAR(1000)
SET @i_class = 'JOB'
SET @i_type = 'LOCAL' DECLARE job CURSOR FOR
SELECT a.job_id ,a.category_id FROM msdb.dbo.sysjobs a , msdb.dbo.syscategories c
WHERE a.category_id = c.category_id
AND c.name NOT LIKE '%Database Maintenance%'
AND c.name NOT LIKE '%REPL%'
AND c.name <> 'Log Shipping'
AND a.name <> 'syspolicy_purge_history'
OPEN job
FETCH job INTO @c_jobid ,@c_categoryid
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = ''
SELECT @i_job_name = a.name ,
@i_enabled = [enabled] ,
@i_notify_level_eventlog = notify_level_eventlog ,
@i_notify_level_email = notify_level_email ,
@i_notify_level_netsend = notify_level_netsend ,
@i_notify_level_page = notify_level_page ,
@i_delete_level = delete_level ,
@i_description = [description] ,
@i_category_name = c.name ,
@i_owner_login_name = ISNULL(SUSER_SNAME(a.owner_sid), N'''') ,
@i_category_class = category_class
FROM msdb.dbo.sysjobs a ,msdb.dbo.syscategories c
WHERE a.category_id=c.category_id AND a.job_id=@c_jobid AND a.category_id = @c_categoryid SET @sql=@sql+CHAR(13)+CHAR(10) + 'USE [msdb]'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'GO'
SET @sql=@sql+CHAR(13)+CHAR(10) + '/****** Object: Job ['+ @i_job_name +'] Script Date: '+CONVERT(VARCHAR,GETDATE(),22)+' ******/'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'BEGIN TRANSACTION'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'DECLARE @ReturnCode INT'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'SELECT @ReturnCode = 0'
SET @sql=@sql+CHAR(13)+CHAR(10) + '/****** Object: JobCategory ['+ @i_category_name +'] Script Date: 08/20/2012 12:35:16 ******/'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'''+ @i_category_name +''' AND category_class='+ CAST(@i_category_class AS VARCHAR) +' )'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'BEGIN'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'''+ @i_class +''', @type=N'''+ @i_type +''', @name=N'''+ @i_category_name +''''
SET @sql=@sql+CHAR(13)+CHAR(10) + 'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback'
SET @sql=@sql+CHAR(13)+CHAR(10) + ''
SET @sql=@sql+CHAR(13)+CHAR(10) + 'END'
SET @sql=@sql+CHAR(13)+CHAR(10) + ''
SET @sql=@sql+CHAR(13)+CHAR(10) + 'DECLARE @jobId BINARY(16)'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'''+ @i_job_name +''','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @enabled='+ CAST(@i_enabled AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @notify_level_eventlog='+ CAST(@i_notify_level_eventlog AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @notify_level_email='+ CAST(@i_notify_level_email AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @notify_level_netsend='+ CAST(@i_notify_level_netsend AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @notify_level_page='+ CAST(@i_notify_level_page AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @delete_level='+ CAST(@i_delete_level AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @description=N'''+ @i_description +''','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @category_name=N'''+ @i_category_name +''','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @owner_login_name=N'''+ @i_owner_login_name +''', @job_id = @jobId OUTPUT'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback'
IF EXISTS ( SELECT TOP 1 1 FROM msdb.dbo.sysjobsteps WHERE job_id = @c_jobid )
BEGIN
SELECT @loop_stepid = MIN(step_id) ,@m_stepid = MAX(step_id) FROM msdb.dbo.sysjobsteps WHERE job_id = @c_jobid
WHILE (@loop_stepid < = @m_stepid)
BEGIN
SELECT @i_start_step_id = start_step_id,
@i_step_name = step_name ,
@i_step_id = step_id,
@i_cmdexec_success_code = cmdexec_success_code ,
@i_on_success_action = on_success_action ,
@i_on_success_step_id = on_success_step_id ,
@i_on_fail_action = on_fail_action ,
@i_on_fail_step_id = on_fail_step_id ,
@i_retry_attempts = retry_attempts ,
@i_retry_interval = retry_interval ,
@i_os_run_priority = os_run_priority ,
@i_subsystem = subsystem ,
@i_command = command ,
@i_database_name = database_name ,
@i_flags = flags
FROM msdb.dbo.sysjobs a ,msdb.dbo.sysjobsteps b
WHERE a.job_id = b.job_id AND step_id = @loop_stepid AND a.job_id = @c_jobid SET @sql=@sql+CHAR(13)+CHAR(10) + '/****** Object: Step ['+ @i_step_name +'] Script Date: '+CONVERT(VARCHAR,GETDATE(),22)+' ******/'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'''+ @i_step_name +''','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @step_id='+ CAST(@i_step_id AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @cmdexec_success_code='+ CAST(@i_cmdexec_success_code AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @on_success_action='+ CAST(@i_on_success_action AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @on_success_step_id='+ CAST(@i_on_success_step_id AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @on_fail_action='+ CAST(@i_on_fail_action AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @on_fail_step_id='+ CAST(@i_on_fail_step_id AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @retry_attempts='+ CAST(@i_retry_attempts AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @retry_interval='+ CAST(@i_retry_interval AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @os_run_priority='+ CAST(@i_os_run_priority AS VARCHAR) +', @subsystem=N'''+ @i_subsystem +''','
SET @sql=@sql+CHAR(13)+CHAR(10) + ISNULL(' @command=N''' + REPLACE(@i_command ,'''' ,'''''') + ''',' ,'')
SET @sql=@sql+CHAR(13)+CHAR(10) + ISNULL(' @database_name=N'''+ @i_database_name +''',' ,'')
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @flags='+ CAST(@i_flags AS VARCHAR)
SET @sql=@sql+CHAR(13)+CHAR(10) + 'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' SET @loop_stepid = ( SELECT TOP 1 step_id FROM msdb.dbo.sysjobsteps WHERE job_id = @c_jobid AND step_id > @loop_stepid ORDER BY step_id )
END
END SET @sql=@sql+CHAR(13)+CHAR(10) + 'EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = '+ CAST(@i_start_step_id AS VARCHAR)
SET @sql=@sql+CHAR(13)+CHAR(10) + 'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback'
IF EXISTS ( SELECT TOP 1 1 FROM msdb.dbo.sysschedules c ,msdb.dbo.sysjobschedules d WHERE c.schedule_id = d.schedule_id AND job_id = @c_jobid )
BEGIN
SELECT @loop_scheduleid= MIN(c.schedule_id) ,@m_scheduleid = MAX(c.schedule_id)
FROM msdb.dbo.sysschedules c ,msdb.dbo.sysjobschedules d
WHERE c.schedule_id = d.schedule_id AND job_id = @c_jobid
WHILE ( @loop_scheduleid <= @m_scheduleid )
BEGIN
SELECT @i_schedule_enabled = [enabled] ,
@i_freq_type = freq_type ,
@i_schedule_name = name,
@i_freq_interval = freq_interval ,
@i_freq_subday_type = freq_subday_type ,
@i_freq_subday_interval = freq_subday_interval ,
@i_freq_relative_interval = freq_relative_interval ,
@i_freq_recurrence_factor = freq_recurrence_factor ,
@i_active_start_date = active_start_date ,
@i_active_end_date = active_end_date ,
@i_active_start_time = active_start_time ,
@i_active_end_time = active_end_time ,
@i_schedule_uid = schedule_uid
FROM msdb.dbo.sysschedules c LEFT JOIN msdb.dbo.sysjobschedules d
ON c.schedule_id = d.schedule_id
WHERE d.job_id = @c_jobid AND c.schedule_id = @loop_scheduleid SET @sql=@sql+CHAR(13)+CHAR(10) + 'EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'''+ @i_schedule_name +''','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @enabled='+ CAST(@i_schedule_enabled AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @freq_type='+ CAST(@i_freq_type AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @freq_interval='+ CAST(@i_freq_interval AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @freq_subday_type='+ CAST(@i_freq_subday_type AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @freq_subday_interval='+ CAST(@i_freq_subday_interval AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @freq_relative_interval='+ CAST(@i_freq_relative_interval AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @freq_recurrence_factor='+ CAST(@i_freq_recurrence_factor AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @active_start_date='+ CAST(@i_active_start_date AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @active_end_date='+ CAST(@i_active_end_date AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @active_start_time='+ CAST(@i_active_start_time AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @active_end_time='+ CAST(@i_active_end_time AS VARCHAR) +','
SET @sql=@sql+CHAR(13)+CHAR(10) + ' @schedule_uid=N'''+ @i_schedule_uid +''''
SET @sql=@sql+CHAR(13)+CHAR(10) + 'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' SET @loop_scheduleid = ( SELECT TOP 1 c.schedule_id FROM msdb.dbo.sysschedules c ,msdb.dbo.sysjobschedules d
WHERE c.schedule_id = d.schedule_id AND job_id = @c_jobid AND c.schedule_id > @loop_scheduleid )
END
END SET @sql=@sql+CHAR(13)+CHAR(10) + 'EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N''(local)'''
SET @sql=@sql+CHAR(13)+CHAR(10) + 'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'COMMIT TRANSACTION'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'GOTO EndSave'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'QuitWithRollback:'
SET @sql=@sql+CHAR(13)+CHAR(10) + ' IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION'
SET @sql=@sql+CHAR(13)+CHAR(10) + 'EndSave:'
SET @sql=@sql+CHAR(13)+CHAR(10) + ''
SET @sql=@sql+CHAR(13)+CHAR(10) + 'GO' PRINT @sql
SELECT LEN(@sql)
FETCH NEXT FROM job INTO @c_jobid ,@c_categoryid
END
CLOSE job
DEALLOCATE job END GO
直接执行
EXEC [master].[dbo].[usp_DumpJobsql]
便可以获取所有Job的创建脚本
常用脚本--生成Agent Job的创建脚本的更多相关文章
- (喷血分享)利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句
(喷血分享)利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句 在我们RDIFramework.NET代码生成器中,有这样一个应用,就是通过数据库表自动生成表的CREA ...
- 利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句
利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句 (喷血分享)利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句 在我们RDIFram ...
- Python脚本生成可执行文件&(恋爱小脚本)
Python脚本生成可执行文件&(恋爱小脚本) 参考文献: http://c.biancheng.net/view/2690.html; https://blog.csdn.net/qq_39 ...
- Oracle工具类-生成数据库现有Job的创建脚本
生成Oracle数据库现有Job的创建脚本 -- 生成现有Job的创建脚本 create or replace procedure proc_generate_job_create_sql is be ...
- 用SQL脚本 生成INSERT SQL语句
配置表B 中的数据,可以从A表中查询到,在实际配置时,通过sql脚本生成B表的insert脚本,最多用到的是sql中连接符[||],以及双引号[''''] 例1:电销系统中地区出单机构关系表配置数据生 ...
- 利用Ant脚本生成war包的详细步骤
使用ant脚本前的准备 1.下载一个ant安装包.如:apache-ant-1.8.4-bin.zip.解压到E盘. 2.配置环境变量.新增ANT_HOME:E:\apache-ant-1.8.4:P ...
- Unity VS 创建脚本自动添加头注释-时间-描述-作者等信息
Unity生成脚本自动添加头注释 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心 ...
- Python脚本生成sitemap
项目须要用脚本生成sitemap,中间学习了一下sitemap的格式和lxml库的使用方法.把结果记录一下,方便以后须要直接拿来用. 来自Python脚本生成sitemap 安装lxml 首先须要pi ...
- SQL Server 2008 转换为 SQL 2005 数据库 脚本生成
Tips: 本文讨论如何把数据库从SQL Server 2008版本降低到2005,因为在本地开发是以SQL Server 2008 Express Edition版本进行的,而主机提供商现在提供的M ...
随机推荐
- 【ZZ】浅谈大型web系统架构 | 菜鸟教程
浅谈大型web系统架构 http://www.runoob.com/w3cnote/large-scale-web-system-architecture.html
- thinkPHP volist标签循环输出多维数组
<volist name="company" id="vo">{$vo.company_name}<volist name="vo[ ...
- Autofac容器使用属性进行WebApi自动注入
背景 使用Autofac进行依赖注入时,经常遇到的场景是在容器中进行类似如下代码的注入操作: builder.RegisterType<BackInStockSubscriptionServic ...
- mangle和demangle
转:https://www.cnblogs.com/robinex/p/7892795.html. mangle和demangle C/C++语言在编译以后,函数的名字会被编译器修改,改成编译器内部的 ...
- 【Directshow】IFilterGraph::AddFilter方法参数问题<转>
看dx里dshow的例子和别人的程序,用IFilterGraph::AddFilter()这个API添加filter到Graph的时候,第二个参数是一个设备友好名称: 我看有的填的是通过IProper ...
- 【转】JPG打包压缩后比原来尺寸还大
作者:刘源链接:https://www.zhihu.com/question/40371280/answer/86262934来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...
- YARN 多租户资源池配置
简介: YARN 多租户资源池配置 当多用户同在一个 hadoop 集群作业时,就需要对资源进行有效的限制,例如区分测试.正式资源等 一.查看默认资源池 # 访问:http://192.168.1.2 ...
- js 实现数组深度copy
1. slice() slice() 方法可从已有的数组中返回选定的元素.arrayObject.slice(start,end) ,返回一个新的数组,包含从 start 到 end (不包括该元素) ...
- linux 解析json
epel源 yum install jq ====================== [root@mhc NaHan-master]# curl 127.0.0.1:5000/v2/_catalog ...
- 什么是2MSL
[什么是2MSL] MSL是Maximum Segment Lifetime英文的缩写,中文可以译为“报文最大生存时间”,他是任何报文在网络上存在的最长时间,超过这个时间报文将被丢弃.因为tcp报文( ...