'尽管声明'对于运行oracle的工作

时间:2017-03-22 14:32:02

标签: sql oracle plsql jobs

我有一个光标来获取状态为RUNNING或SCHEDULED的游标数据

CURSOR cursorJobStatus IS
     SELECT d.job_name, d.state
       FROM dba_scheduler_jobs d
       WHERE REGEXP_LIKE(d.job_name, '^(P_|S_|B_)')
         AND d.state IN ('RUNNING','SCHEDULED'); 

如何在“光标作业”中存在数据时如何执行循环,即只要有“作业”这样的作业即可。在RUNNING或SCHEDULED状态下?

我试过这个' for'但它不起作用,因为只有进入' for'通过它找到的记录并且不会一直询问光标的状态

FOR lrtJobStatus IN cursorJobStatus LOOP
   BEGIN
     DBMS_LOCK.SLEEP(5);           
   EXCEPTION
     WHEN NO_DATA_FOUND THEN                    
       EXIT;                  
   END;
   EXIT WHEN cursorJobStatus%NOTFOUND;
END LOOP;

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

也许是这样的:

declare
   l_job_running pls_integer;
begin
   loop
      begin
         select 1
           into l_job_running
           from dual
          where exists (select 1
                   from dba_scheduler_jobs d
                  where regexp_like(d.job_name, '^(P_|S_|B_)')
                    and d.state in ('RUNNING', 'SCHEDULED'));
      exception
         when no_data_found then
            l_job_running := 0;
      end;

      exit when l_job_running = 0;

      dbms_lock.sleep(5);

   end loop;
end;

答案 1 :(得分:0)

尝试制作一个匿名块,它将检查当前正在运行的作业,并将继续执行,直到有正在执行的作业。我想你可以用同样的方式实现你的目标。

set serveroutput on;
declare
var1 number default '1';
begin
loop
dbms_output.put_line('inside the loop');
select count(*) into var1 from dba_jobs_running;
exit when var1=0;
dbms_output.put_line('job/jobs are executing');
end loop;
dbms_output.put_line('anonymous block completes');
end;