oracle怎么建立定时任务
时间: 2023-07-21 22:39:45 浏览: 99
Oracle提供了DBMS_SCHEDULER包来创建和管理定时任务。下面是一个例子,展示了如何在Oracle中创建一个每天执行一次的定时任务:
1. 创建一个作业(Job)
```
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'my_job_name',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN my_procedure; END;',
start_date => SYSTIMESTAMP,
repeat_interval => 'FREQ=DAILY; BYHOUR=0; BYMINUTE=0; BYSECOND=0;',
end_date => NULL,
enabled => TRUE,
comments => 'My job comments'
);
END;
/
```
2. 创建一个程序(Program)
```
BEGIN
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name => 'my_program_name',
program_type => 'PLSQL_BLOCK',
program_action => 'BEGIN my_procedure; END;',
number_of_arguments => 0,
enabled => TRUE,
comments => 'My program comments'
);
END;
/
```
3. 创建一个凭证(Credential)
```
BEGIN
DBMS_SCHEDULER.CREATE_CREDENTIAL (
credential_name => 'my_credential_name',
username => 'my_username',
password => 'my_password',
comments => 'My credential comments'
);
END;
/
```
4. 将凭证(Credential)授权给程序(Program)
```
BEGIN
DBMS_SCHEDULER.GRANT_PROGRAM_ACCESS (
grantee_name => 'my_grantee_name',
program_name => 'my_program_name',
privilege => 'EXECUTE',
credential_name => 'my_credential_name',
comments => 'My grant comments'
);
END;
/
```
这样就可以在Oracle中创建一个每天执行一次的定时任务了。
阅读全文