Search code examples
sqloracle-databaseplsqljobs

'While Statement' for jobs running oracle


I have a cursor to get the data of the cursors in state RUNNING or 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'); 

how i can execute a loop while exist data in 'cursorJobStatus', That is, as long as there are 'jobs' in state RUNNING or SCHEDULED?

i tried with this 'for' but its not works, because only go in the 'for' by the records it finds and does not ask all the time for the state of the cursor

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

thanks for any help!


Solution

  • Maybe something like this:

    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;