项目描述
前端动态管理筛选逻辑,存储到数据库中;数据库定时按照筛选逻辑从较大数据集合中筛选数据。
解决思路
数据库要定时按照筛选逻辑筛选数据,首选我们需要写一个存储过程(参数为筛选条件)按照条件去筛选数据,然后将存储过程挂到定时器上定时执行;筛选条件是可编辑的,就需要对定时器有个管理的功能,例如筛选条件删除了那么我们就需要删除指定的定时器,如果筛选条件改变了我们也需要对指定定时器的存储过程参数做相应的修改,如果新增加了筛选条件那么我们也要及时增加一个新的定时器;筛选条件是动态添加的我们不知道什么时候会添加,所以我们还需要一个定时器去检查对比筛选条件是否发生变化。
具体方法
1. 创建一个带参数的存储过程,去按照筛选逻辑筛选数据
create or replace procedure procedures_warning(bjtjdm varchar2, zbdmstr varchar2, ysfhdm varchar2, lxcs number, bjz number, tjdm varchar2, qs varchar2)
is
begin
end
procedures_warning;
create or replace procedure procedures_warning(bjtjdm varchar2, zbdmstr varchar2, ysfhdm varchar2, lxcs number, bjz number, tjdm varchar2, qs varchar2)
is
begin
end
procedures_warning;
2.创建一个存储过程,将逻辑处理的存储过程procedures_warning绑定到一个定时器(作业或者任务)中,并将当前任务记录到数据表中
create or replace procedure procedures_WarningCreateJob(bjtjdmP varchar2,zbdmP varchar2,ysfhdmP varchar2,lxcsP number,bjzP number,tjdmP varchar2,qsP varchar2) is
countNum number;
programName varchar2(80);
jobName varchar2(80);
p_name VARCHAR2(100);
p_sqlerrm VARCHAR2(1000);
p_error VARCHAR2(1000);
begin
programName:=f_get_programName();--为避免出现相同名称而做的处理
dbms_scheduler.create_program(program_name => programName,
program_type => 'STORED_PROCEDURE',
program_action => 'procedures_warning',
number_of_arguments => 7,
enabled => FALSE,
comments => '报警存储项');
--声明输入参数
dbms_scheduler.define_program_argument(program_name =>programName,
argument_name => 'bjtjdm',
argument_position => 1,
argument_type => 'VARCHAR2',
default_value => '');
dbms_scheduler.define_program_argument(program_name =>programName,
argument_name => 'zbdmstr',
argument_position => 2,
argument_type => 'VARCHAR2',
default_value => '');
dbms_scheduler.define_program_argument(program_name =>programName,
argument_name => 'ysfhdm',
argument_position => 3,
argument_type => 'VARCHAR2',
default_value => '');
dbms_scheduler.define_program_argument(program_name => programName,
argument_name => 'lxcs',
argument_position => 4,
argument_type => 'number',
default_value => 0);
dbms_scheduler.define_program_argument(program_name =>programName,
argument_name => 'bjz',
argument_position => 5,
argument_type => 'number',
default_value => 0);
dbms_scheduler.define_program_argument(program_name => programName,
argument_name => 'tjdm',
argument_position => 6,
argument_type => 'VARCHAR2',
default_value => '');
dbms_scheduler.define_program_argument(program_name => programName,
argument_name => 'qs',
argument_position => 7,
argument_type => 'VARCHAR2',
default_value => '');
dbms_scheduler.enable(NAME => programName);
jobName:=f_get_jobName();
dbms_scheduler.create_job(job_name =>jobName,
program_name => programName,
repeat_interval => 'sysdate+2/24', --job执行频率
enabled => TRUE,
auto_drop => FALSE,
comments => '根据条件信息创建job'); --job描述
--给参数赋值
dbms_scheduler.set_job_argument_value(job_name => jobName,
argument_position => 1, --参数序号
argument_value => bjtjdmP);
dbms_scheduler.set_job_argument_value(job_name => jobName,
argument_position => 2,
argument_value => zbdmP);
dbms_scheduler.set_job_argument_value(job_name => jobName,
argument_position => 3, --参数序号
argument_value => ysfhdmP);
dbms_scheduler.set_job_argument_value(job_name => jobName,
argument_position => 4,
argument_value => lxcsP);
dbms_scheduler.set_job_argument_value(job_name =>jobName,
argument_position => 5, --参数序号
argument_value => bjzP);
dbms_scheduler.set_job_argument_value(job_name => jobName,
argument_position => 6,
argument_value => tjdmP);
dbms_scheduler.set_job_argument_value(job_name => jobName,
argument_position => 7,
argument_value => qsP);
select count(*) into countNum from OR_DBBJTJGLJOB a where a.yjtjdm=bjtjdmP ;
if(countNum>0) then
update OR_DBBJTJGLJOB a set a.zbdm=zbdmP,a.ysfhdm=ysfhdmP,a.lxcs=lxcsP,a.bjz=bjzP,a.tjdm=tjdmP,a.jobid=jobName,a.programid=programname,a.qs=qsP where a.yjtjdm=bjtjdmP ;
else
insert into OR_DBBJTJGLJOB a (a.Zbdm,a.Ysfhdm,a.Lxcs,a.Bjz,a.tjdm,a.jobid,a.Programid,a.yjtjdm,a.qs) values(zbdmP,ysfhdmP,lxcsP,bjzP,tjdmP,jobname,programName,bjtjdmP,qsP);
end if;
commit;
EXCEPTION
when others then
p_name := 'procedures_WarningCreateJob'; --获取存储过程名称;fn_getname(函数名称)
p_sqlerrm := sqlerrm; --错误信息
p_error := dbms_utility.format_error_backtrace(); --错误行
--插入错误日志表中
insert into OR_WARNINGLOG
(p_name, p_date, p_sqlerrm, p_error)
values
(p_name, --存储过程名称
sysdate, --跑批时间
p_sqlerrm, --错误信息
p_error --错误位置
);
commit;
end procedures_WarningCreateJob;
3.因为我们的筛选条件可能在某段时间是无效的或者是需要删除的,那么我们需要对对应的任务也要删除
create or replace procedure procedures_WarningRemoveJob(jobid varchar2,programid varchar2) is
countNum number;
p_name VARCHAR2(100);
p_sqlerrm VARCHAR2(1000);
p_error VARCHAR2(1000);
begin
select count(*) into countNum from SYS.DBA_SCHEDULER_JOBS where job_name=jobid;
if countNum>0 then
begin
dbms_scheduler.drop_job(jobid);
commit;
end;
end if;
select count(*) into countNum from dba_scheduler_programs where program_name=programid;
if countNum>0 then
begin
dbms_scheduler.drop_program(programid);
commit;
end;
end if;
EXCEPTION
when others then
p_name := 'procedures_WarningRemoveJob'; --获取存储过程名称;fn_getname(函数名称)
p_sqlerrm := sqlerrm; --错误信息
p_error := dbms_utility.format_error_backtrace(); --错误行
--插入错误日志表中
insert into OR_WARNINGLOG
(p_name, p_date, p_sqlerrm, p_error)
values
(p_name, --存储过程名称
sysdate, --跑批时间
p_sqlerrm, --错误信息
p_error --错误位置
);
commit;
end procedures_WarningRemoveJob;
4. 何时去删除任务何时应该添加新的任务我们需要一个定时的存储过程去管理
create or replace procedure procedures_WarningJob is
begin
--删除定时器
for raw_rowNoExistJob in curNoExistjob
loop
begin
procedures_WarningRemoveJob(raw_rowNoExistJob.Jobid,raw_rowNoExistJob.programid);
delete from or_djbjxx where bjtj =raw_rowNoExistJob.Yjtjdm;
end;
end loop;
--移除旧的定时器根据新参数重新创建(定时器刷新)
for raw_rowExist in curExistJob
loop
begin
procedures_WarningRemoveJob(raw_rowExist.Jobid,raw_rowExist.programid);
delete from or_djbjxx where bjtj =raw_rowNoExistJob.Yjtjdm;
commit;
procedures_WarningCreateJob(raw_rowExist.Yjtjdm,raw_rowExist.zbdm,raw_rowExist.Ysfhdm, raw_rowExist.Lxcs,raw_rowExist.Bjz,raw_rowExist.tjdm,raw_rowExist.Qs);
end;
end loop;
--创建新的之前不存在的定时器
for raw_row in cur
loop
begin
procedures_WarningCreateJob(raw_row.Yjtjdm,raw_row.zbdm,raw_row.Ysfhdm, raw_row.Lxcs,raw_row.Bjz,raw_row.tjdm,raw_row.qs);
end;
end loop;
commit;
EXCEPTION
when others then
p_name := 'procedures_WarningJob'; --获取存储过程名称;fn_getname(函数名称)
p_sqlerrm := sqlerrm; --错误信息
p_error := dbms_utility.format_error_backtrace(); --错误行
--插入错误日志表中
insert into OR_WARNINGLOG
(p_name, p_date, p_sqlerrm, p_error)
values
(p_name, --存储过程名称
sysdate, --跑批时间
p_sqlerrm, --错误信息
p_error --错误位置
);
commit;
end procedures_WarningJob;
create or replace procedure procedures_WarningJob is
begin
--删除定时器
for raw_rowNoExistJob in curNoExistjob
loop
begin
procedures_WarningRemoveJob(raw_rowNoExistJob.Jobid,raw_rowNoExistJob.programid);
delete from or_djbjxx where bjtj =raw_rowNoExistJob.Yjtjdm;
end;
end loop;
--移除旧的定时器根据新参数重新创建(定时器刷新)
for raw_rowExist in curExistJob
loop
begin
procedures_WarningRemoveJob(raw_rowExist.Jobid,raw_rowExist.programid);
delete from or_djbjxx where bjtj =raw_rowNoExistJob.Yjtjdm;
commit;
procedures_WarningCreateJob(raw_rowExist.Yjtjdm,raw_rowExist.zbdm,raw_rowExist.Ysfhdm, raw_rowExist.Lxcs,raw_rowExist.Bjz,raw_rowExist.tjdm,raw_rowExist.Qs);
end;
end loop;
--创建新的之前不存在的定时器
for raw_row in cur
loop
begin
procedures_WarningCreateJob(raw_row.Yjtjdm,raw_row.zbdm,raw_row.Ysfhdm, raw_row.Lxcs,raw_row.Bjz,raw_row.tjdm,raw_row.qs);
end;
end loop;
commit;
EXCEPTION
when others then
p_name := 'procedures_WarningJob'; --获取存储过程名称;fn_getname(函数名称)
p_sqlerrm := sqlerrm; --错误信息
p_error := dbms_utility.format_error_backtrace(); --错误行
--插入错误日志表中
insert into OR_WARNINGLOG
(p_name, p_date, p_sqlerrm, p_error)
values
(p_name, --存储过程名称
sysdate, --跑批时间
p_sqlerrm, --错误信息
p_error --错误位置
);
commit;
end procedures_WarningJob;