当前位置: 移动技术网 > IT编程>数据库>其他数据库 > 在SQL Server和Oracle中创建job

在SQL Server和Oracle中创建job

2017年12月08日  | 移动技术网IT编程  | 我要评论
首先说下ms server,因为我也是新手,所以首先选择了使用企业管理器进行任务设定,然后再导出设计完毕的任务脚本,这样就可以在其他数据库中直接通过执行脚本的形式来新建任务
首先说下ms server,因为我也是新手,所以首先选择了使用企业管理器进行任务设定,然后再导出设计完毕的任务脚本,这样就可以在其他数据库中直接通过执行脚本的形式来新建任务了(需要适当修改其中的一些任务名称和数据库名称等值),下面说下具体的操作步骤以及获得的任务脚本:

1. 保证启动ms server的“sql server代理服务”(即agent service)

2. 打开企业管理器 -> 连接数据服务器 -> 右键“sql server agent”节点下的“jobs”,选择“new job”,在详细任务页面,我们会看到很多的设定选项:包括“常规”、“步骤”、“计划”、“警报”、“通知”和“目标”等,在这里我们可以进行非常详细的任务设定,因为我要做的很简单,就是按时执行数据库中的某一个存储过程,所以只是简单地设定了一下常规、步骤和计划三项中的内容,其中我们通过步骤来设定我们要进行的数据库操作和顺序,然后再通过“计划”来设定具体的操作方案,相比直接写sql语句来说,显得更加的容易些,比较适合于初学者。

3. 右键选择我们刚刚创建成功的job,选择“script job as -> create to”,选择把脚本存放到文件或者直接通过编辑器打开,然后再进行保存均可。我们可以看到系统自动生成的脚本看起来颇为复杂,因为其将其中的设定参数均进行了设定,可能通过sql脚本可以省略很多的默认参数,但我们可以清晰地看到job创建的几个关键步骤。个人示例的sql脚本如后面附。经过精简后的代码如下:
复制代码 代码如下:

declare @jobid binary(16)
--create the job
exec msdb.dbo.sp_add_job @job_name = 'update table of sys_pageinfo',
        @job_id = @jobid output
--create the step of the job
exec msdb.dbo.sp_add_jobstep @job_id = @jobid,
@step_name = 'just one step',
@subsystem = 'tsql',
@command = 'exec dbo.batchupdatearchlisttotalnum',
        @database_name=n'exchanger_2'
--create the schedual of the job
exec msdb.dbo.sp_add_jobschedule @job_id = @jobid,
@name = 'the schedual',
@freq_type = 4,
@freq_interval = 1,
@active_start_time = 10000
--create the server of the job
exec msdb.dbo.sp_add_jobserver @job_id = @jobid, @server_name = n'(local)'

下面再看看oracle中是如何创建job的,这次我们直接使用sql脚本进行创建,一些常用的sql语句如下:
一、查看数据库中的任务
select * from user_jobs
二、删除数据库中指定的任务
begin
dbms_job.remove(50);
end;
三、创建一个每隔5分钟执行一次的任务
declare
jobid number;
begin
sys.dbms_job.submit(job => jobid,
what => 'batchupdatearchlisttotalnum();', // 存储过程如果没有参数,可以省略括号,但是不能省略分号
next_date => sysdate,
interval => 'sysdate+1/24/12');
commit;
end;
附:sql server任务脚本:
复制代码 代码如下:

use [msdb]
go
/****** 对象: job [更新] 脚本日期: 05/12/2009 18:14:14 ******/
begin transaction
declare @returncode int
select @returncode = 0
/****** 对象: jobcategory [database maintenance] 脚本日期: 05/12/2009 18:14:14 ******/
if not exists (select name from msdb.dbo.syscategories where name=n'database maintenance' and category_class=1)
begin
exec @returncode = msdb.dbo.sp_add_category @class=n'job', @type=n'local', @name=n'database maintenance'
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
end
declare @jobid binary(16)
exec @returncode = msdb.dbo.sp_add_job @job_name=n'job名称',
        @enabled=0,
        @notify_level_eventlog=0,
        @notify_level_email=0,
        @notify_level_netsend=0,
        @notify_level_page=0,
        @delete_level=0,
        @description=n'无描述。',
        @category_name=n'database maintenance',
        @owner_login_name=n'sa', @job_id = @jobid output
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
/****** 对象: step [更新sys_pageinfo表的内容] 脚本日期: 05/12/2009 18:14:15 ******/
exec @returncode = msdb.dbo.sp_add_jobstep @job_id=@jobid, @step_name=n'操作步骤名称',
        @step_id=1,
        @cmdexec_success_code=0,
        @on_success_action=1,
        @on_success_step_id=0,
        @on_fail_action=2,
        @on_fail_step_id=0,
        @retry_attempts=0,
        @retry_interval=0,
        @os_run_priority=0, @subsystem=n'tsql',
        @command=n'exec dbo.batchupdatearchlisttotalnum',
        @database_name=n'exchanger_2',
        @database_user_name=n'dbo',
        @flags=0
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
exec @returncode = msdb.dbo.sp_update_job @job_id = @jobid, @start_step_id = 1
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
exec @returncode = msdb.dbo.sp_add_jobschedule @job_id=@jobid, @name=n'计划名称',
        @enabled=1,
        @freq_type=4,
        @freq_interval=1,
        @freq_subday_type=1,
        @freq_subday_interval=1,
        @freq_relative_interval=0,
        @freq_recurrence_factor=0,
        @active_start_date=20090512,
        @active_end_date=99991231,
        @active_start_time=0,
        @active_end_time=235959
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
exec @returncode = msdb.dbo.sp_add_jobserver @job_id = @jobid, @server_name = n'(local)'
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
commit transaction
goto endsave
quitwithrollback:
if (@@trancount > 0) rollback transaction
endsave:

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网