我试图在我的匿名PL / SQL块中捕获异常
DECLARE
...
BEGIN
FOR herstell_row IN (
...
)
LOOP
...
DECLARE
table_does_not_exists exception;
pragma exception_init( table_does_not_exists, -942 );
BEGIN
INSERT INTO SMART_MONITORING_MACHINE_NAV_B (
MACHINE,
NAVIGATION_LEVEL_ID
)
SELECT
old_binding.MACHINE,
pv_id
FROM
SMART_MACHINE_NAV_BINDING old_binding
WHERE
old_binding.NAVIGATION_LEVEL_ID = herstell_row.HENAME1;
EXCEPTION
WHEN table_does_not_exists THEN null;
END;
END LOOP;
END;
我知道SMART_MACHINE_NAV_BINDING表在我的情况下不存在,所以我需要嵌套的匿名块来忽略它的代码。但我总是得到这个错误:
错误报告 - ORA-06550:第41行,第14栏: PL / SQL:ORA-00942:表或视图不存在 ORA-06550:第33行,第10栏: PL / SQL:忽略SQL语句
答案 0 :(得分:0)
Oracle不知道这个错误table_does_not_exists它是用户定义的,所以你应该在其他人处理时,例如。
Exception
When Others then
null;
-- or at this time you can raise your error table_does_not_exists
raise table_does_not_exists;
-- and handle it in another parent block
end;
Exception
when table_does_not_exists then
null;
end;
答案 1 :(得分:0)
您无法使用不存在的表编译代码,但您可以尝试使用EXECUTE EMMEDIATE
执行它:
DECLARE
...
BEGIN
FOR herstell_row IN (
...
)
LOOP
...
DECLARE
table_does_not_exists exception;
pragma exception_init( table_does_not_exists, -942 );
BEGIN
execute immediate
'INSERT INTO SMART_MONITORING_MACHINE_NAV_B (
MACHINE,
NAVIGATION_LEVEL_ID
)
SELECT
old_binding.MACHINE,
pv_id
FROM
SMART_MACHINE_NAV_BINDING old_binding
WHERE
old_binding.NAVIGATION_LEVEL_ID = :P' using herstell_row.HENAME1;
EXCEPTION
WHEN table_does_not_exists THEN null;
END;
END LOOP;
END;
此处您不需要例外,您可以使用系统视图检查表的存在:
declare
table_created number;
begin
select count(*)
into table_created
from all_tables
where table_name = ...
and owner = ...;
if table_created > 0 then
execute immediate 'insert into ...';
end if;
end;
有关EXECUTE IMMEDIATE
声明的更多信息:http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/executeimmediate_statement.htm#LNPLS01317