如果您可以创建表格/程序,这可能是一种方法。
假设我们要运行 3 个脚本:
script1.sql:
create table table_script_1(id number);
script2.sql
create ta_ERROR_HERE_!!!!!!_ble table_script_2(id number);
script3.sql
create table table_script_3(id number);
我们可以创建一个表来存储要运行的脚本(按执行顺序)以及处理该表的过程:
create table script_to_run (num number, script varchar2(256), status varchar2(10));
create or replace procedure update_script (p_script varchar2, p_status varchar2) is
pragma autonomous_transaction;
begin
update script_to_run
set status = p_status
where script = p_script;
commit;
end;
/
这样,我们用表格来说明我们必须运行什么脚本,以及以什么顺序:
insert into script_to_run values (1, 'd:\script1.sql', 'TO_RUN');
insert into script_to_run values (2, 'd:\script2.sql', 'TO_RUN');
insert into script_to_run values (3, 'd:\script3.sql', 'TO_RUN');
commit;
此时,我们的主脚本将简单地读取表格,运行第一个脚本仍未执行,然后递归调用自己,运行下一个脚本:
main_script.sql:
column script new_val script
WHENEVER SQLERROR EXIT
select script
from script_to_run
where num = ( select min(num)
from script_to_run
where nvl(status, 'KO') != 'OK' );
start &script
exec update_script('&script', 'OK');
prompt 'Script &script OK'
start d:\main_script.sql
现在我们运行主脚本(而 script2.sql 包含错误)并检查结果:
SQL> select 1 from table_script_1;
no rows selected
SQL> select 1 from table_script_2;
select 1 from table_script_2
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select 1 from table_script_3;
select 1 from table_script_3
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from script_to_run;
NUM SCRIPT STATUS
---------- -------------------------------------------------- ----------
1 d:\script1.sql OK
2 d:\script2.sql TO_RUN
3 d:\script3.sql TO_RUN
只有 script1 运行正常,script2 有 error3,而 script3 从未运行。
修复script2.sql后,我们再次运行主脚本,不做修改;第二次运行,主脚本只执行script2和script3,不执行script1。
最终结果:
SQL> select 1 from table_script_2;
no rows selected
SQL> select 1 from table_script_3;
no rows selected
SQL> select * from script_to_run;
NUM SCRIPT STATUS
---------- -------------------------------------------------- ----------
1 d:\script1.sql OK
2 d:\script2.sql OK
3 d:\script3.sql OK
SQL>