1. 程式人生 > 其它 >Oracle 開發篇+批量建立JOB(lightweight)

Oracle 開發篇+批量建立JOB(lightweight)

技術標籤:資料庫+Oracle建立LIGHTWEIGHTLIGHTWEIGHT job管道pipe批量建立LIGHTWEIGHT

說明:本文為面向PL/SQL開發初學者的指導手冊
標籤:PL/SQL、批量建立JOB、LIGHTWEIGHT、匿名塊、pipe、管道、
說明:lightweight job適用於多個頻繁執行的小作業
易學:文中刪去了不需要的多餘部分,讓初學者一目瞭然一學就會
溫馨提示:如果您發現本文哪裡寫的有問題或者有更好的寫法請留言或私信我進行修改優化


--建立常規儲存過程

set serveroutput on
set timing on

create or replace procedure p_select
is
 i    simple_integer := 1;
 j    simple_integer := 2000000000;
 v    simple_integer := 0;
 message char(10);
 pipename char(20) := 'pipe_zzt_select';
begin
 for i in 1 .. j loop
 select /*+ no_result_cache */
  count(*) into v
  from zzt.info a, zzt.info b
  where a.sal != b.sal
   and a.id < 100
   and a.sal < 50000;
  if dbms_pipe.receive_message(pipename, 0) = 0 then
   dbms_pipe.unpack_message(message);
   exit when message = 'stop';
  end if;
 end loop;
 v := dbms_pipe.remove_pipe(pipename);
end;
/


--建立lightweight-job需要的儲存過程/轉換常規

EXEC DBMS_SCHEDULER.DROP_PROGRAM('ZZT_P_L_SELECT');

begin
 dbms_scheduler.create_program(program_name  => 'ZZT_P_L_SELECT',
                program_type  => 'STORED_PROCEDURE',
                program_action => 'ZZT.P_SELECT',
                enabled    => true,
                comments    => 'lightweight job program');
end;
/

select count(*) from dba_scheduler_programs where PROGRAM_NAME='ZZT_P_L_SELECT';


--批量建立lightweight-job

declare
 i     simple_integer := 1;
 j     simple_integer := 20;
 interval  varchar2(50) := 'freq=secondly;interval=60'; --間隔xx秒
 starttime date := sysdate;      --立即開始執行
 endtime  date := sysdate + 1 / 24 / 60 * 10; --持續10分鐘
 v_job_name varchar2(30);
begin
 for i in 1 .. j loop
  v_job_name := 'ZZT_JOB_A_' || i;
  dbms_scheduler.create_job(job_name    => v_job_name,
               program_name  => 'ZZT_P_L_SELECT',
               job_style    => 'LIGHTWEIGHT',
               repeat_interval => interval,
               start_date   => starttime,
               end_date    => endtime,
               enabled     => true,
               comments    => 'zzt測試lightweight-job');
 end loop;
end;
/

--檢視相關資訊
select count(*) from dba_scheduler_jobs j where j.owner='ZZT' and j.JOB_STYLE='LIGHTWEIGHT';
select * from dba_scheduler_jobs j where j.owner='ZZT' and j.JOB_STYLE='LIGHTWEIGHT';
select * from dba_scheduler_running_jobs;


※ 如果您覺得文章寫的還不錯,別忘了在文末給作者點個贊哦 ~

over