Oracle database stored procedure development:
A complete stored procedure development includes the following processes:
1. Stored procedure header Package definition: a group of related procedures, functions, variables, constants #SinaEditor_Temp_FontName, types and cursors and other PL/SQL programming elements combination
Header definition:
create or replace package package_namae is ---- package_namae Baotou name
procedure procedure_name ; -- procedure_name stored procedure name
procedure sp_write_log( -- sp_write_log stored procedure logging
job_name varchar, --job_name Job名称
extraction_timepoint TIMESTAMP , -- extraction_timepoint stores the extraction time
ins_start_timestamp TIMESTAMP , -- ins_start_timestamp Job execution start time
ins_finish_timestamp TIMESTAMP , -- ins_finish_timestamp Job execution end time
record_count number , -- record_count record statistics
job_typechar, --job_type Job类型
status char , --status Job running status
error varchar2 ); --error Job failed information
end package_namae ; --package_namae Baotou name
2. PackageBody definition of the stored procedure package body: It is the specific implementation of the package definition part. It is responsible for providing specific implementations for the subprograms declared in the package header. The private elements of the package can also be declared in the package body.
create or replace package body package_namae is --package_namae Baotou name
procedure procedure_name as -- procedure_name stored procedure name
job_name varchar, --job_name Job名称
extraction_timepoint TIMESTAMP , -- extraction_timepoint stores the extraction time
ins_start_timestamp TIMESTAMP , -- ins_start_timestamp Job execution start time
ins_finish_timestamp TIMESTAMP , -- ins_finish_timestamp Job execution end time
record_count number , -- record_count record statistics
job_typechar, --job_type Job类型
status char , --status Job running status
error varchar2 ); --error Job failed information
begin
job_name := 'job_name'; --job_name Job名称
extraction_timepoint := null ; -- ins_start_timestamp Job execution start time
ins_start_timestamp := sysdate ; -- -- ins_finish_timestamp Job execution end time
job_type := 'D' ; -- define the type of job operation: D-daily H-Hourly etc.
-- Start of specific SQL script content
-----------------------------SQL specific logic------------------- --------
--End of specific SQL script content
commit;
status := 'S';
-------------write log---------------------------------------------------------------------------
sp_write_log(job_name,
extraction_timepoint,
ins_start_timestamp,
ins_finish_timestamp,
record_count,
job_type,
status,
error);
---------------EXCEPTION-----------------------------------------------------------------------------
exception
when others then
begin
rollback;
sp_write_log(job_name,
extraction_timepoint,
ins_start_timestamp,
ins_finish_timestamp,
record_count,
job_type,
'F',
sqlcode || ' ' || sqlerrm);
end;
end procedure_name;
--------------------------------------------------------------------------
----------------------LOG日志存过-----------------------------
------------------------------------------------------------------------
procedure sp_write_log(job_name varchar,
extraction_timepoint timestamp,
ins_start_timestamp timestamp,
ins_finish_timestamp timestamp,
record_count number,
type char,
status char,
error varchar2) as
begin
insert into vom_stats_log --vom_stats_log日志记录表
values
(job_name,
extraction_timepoint,
ins_start_timestamp,
ins_finish_timestamp,
record_count,
type,
status,
error);
commit;
end;
end;
3.存储过程定时程序Program定义:
定义program程序:
begin
sys.dbms_scheduler.create_program(
program_name => ' scheme .program_name',
program_type => 'stored_procedure',
program_action => 'package_namae.procedure_name',
number_of_arguments => 1,
enabled => false,
comments => 'Program备注描述');
sys.dbms_scheduler.define_program_argument(
program_name => 'scheme .program_name',
argument_position => 1,
argument_name => 'job_type',
argument_type => 'char',
default_value => 'D');
sys.dbms_scheduler.enable(name => 'scheme .program_name');
end;
/
4.存储过程定时调度Schedule定义:
定义schedule调度:
begin
sys.dbms_scheduler.create_schedule(
schedule_name => 'scheme.schedule_name',
start_date => to_date('start_date', 'dd-mm-yyyy hh24:mi:ss'),
repeat_interval => 'Freq=Daily;Interval=1',
end_date => to_date(null),
comments => 'Schedule备注描述');
end;
/
5.存储过程定时任务Job定义:
定义Job任务:
begin
sys.dbms_scheduler.create_job(
job_name => 'scheme.job_name',
program_name => 'scheme.program_name',
schedule_name => 'scheme.schedule_name',
job_class => 'DEFAULT_JOB_CLASS',
enabled => false,
auto_drop => false,
comments => 'Job备注描述');
sys.dbms_scheduler.enable(name => 'scheme.job_name');
end;
/
PS:包头和包主体分开编译,并作为两个分开的对象分别存放在数据库字典中