1

我有一些使用 DBMS_SCHEDULER 运行 bat 文件窗口的脚本。但是当有多个数据循环时会出现一些错误。

CREATE OR REPLACE procedure SEAT.ss_print_fundoshi_run_weld_tst (ptype varchar2)
is

cursor c_data is
select * from ss_print_fundoshi_tmp_file
where vdesc = ptype
order by vno;

vname varchar2(100);
v_dir varchar2(200) := 'SEATMAP';
fhandle            UTL_FILE.FILE_TYPE;

BEGIN
for i in c_data loop
 vname := 'RUNPRINT_WELD';-;
dbms_scheduler.drop_job (vname);
dbms_scheduler.create_job( 
job_name => vname,
job_type => 'EXECUTABLE',
job_action => 'C:\Windows\System32\cmd.exe',
job_class => 'DEFAULT_JOB_CLASS',
--comments => 'test job',
auto_drop => false,
number_of_arguments => 3,
enabled => FALSE);
dbms_scheduler.set_job_argument_value( job_name => vname, argument_position => 1, argument_value => '/q'); 
dbms_scheduler.set_job_argument_value( job_name => vname, argument_position => 2, argument_value => '/c'); 
dbms_scheduler.set_job_argument_value( job_name => vname, argument_position => 3, argument_value => '"'||i.vfile||'"'); 
dbms_scheduler.enable( vname);
dbms_lock.sleep(5);


end loop;

END;

但是因为有几个数据,所以作业一直说作业名称为 RUNPRINT_WELD 的作业仍在运行,所以它不能丢弃作业。

如果只有一排,则运行良好。

什么是最好的解决方案?Tq

4

1 回答 1

1

你在哪里居然快到了!

你只需要等待RUNPRINT_WELD你开始的工作结束 - 看起来它可能比5s-> 如果没有完成,等待另一个5s,仍然带有dbms_lock.sleep (5). 您可以通过从dba_scheduler_running_jobs表中获取数据来做到这一点:实际运行的作业:

CREATE OR REPLACE procedure SEAT.ss_print_fundoshi_run_weld_tst (ptype varchar2)
is
  vname varchar2(100);
  v_cnt Number;
  v_dir varchar2(200) := 'SEATMAP';
  fhandle            UTL_FILE.FILE_TYPE;
BEGIN
  for i in (
        select sspf.*
          from ss_print_fundoshi_tmp_file sspf
         where vdesc = ptype
        order by vno;
  ) loop
   vname := 'RUNPRINT_WELD'; -- add a number after job name to differentiate
--   dbms_scheduler.drop_job (vname);  -- why drop it? you must find a way to decide if it really need to be dropped.
   dbms_scheduler.create_job( 
       job_name => vname,
       job_type => 'EXECUTABLE',
       job_action => 'C:\Windows\System32\cmd.exe',
       job_class => 'DEFAULT_JOB_CLASS',
       --comments => 'test job',
       auto_drop => false,
       number_of_arguments => 3,
       enabled => FALSE
    );
    dbms_scheduler.set_job_argument_value( job_name => vname, argument_position => 1, argument_value => '/q'); 
    dbms_scheduler.set_job_argument_value( job_name => vname, argument_position => 2, argument_value => '/c'); 
    dbms_scheduler.set_job_argument_value( job_name => vname, argument_position => 3, argument_value => '"'||i.vfile||'"'); 
    dbms_scheduler.enable( vname);
    -- a while loop to wait end of job
    v_cnt := 1;
    WHILE v_cnt>=1
    LOOP
      SELECT count(1) INTO v_cnt 
        FROM dba_scheduler_running_jobs srj
       WHERE srj.job_name IN ('RUNPRINT_WELD')
      ;
      IF v_cnt>0 THEN
        dbms_lock.sleep (5);
      END IF;
    END LOOP;
  end loop;
END;
/
于 2017-04-26T08:47:59.827 回答