匿名 PL/SQL 块检查异常
Anonymous PL/SQL block checked exception
我试图在我的匿名 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;
我知道 table SMART_MACHINE_NAV_BINDING 在我的案例中不存在,所以我需要嵌套的匿名块来忽略它的代码。但我总是得到这个错误:
Error report -
ORA-06550: line 41, column 14:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 33, column 10:
PL/SQL: SQL Statement ignored
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;
你不能用不存在的 table 编译代码,但你可以尝试使用 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;
此外,您在这里不需要例外,您可以使用系统视图检查 table 是否存在:
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
我试图在我的匿名 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;
我知道 table SMART_MACHINE_NAV_BINDING 在我的案例中不存在,所以我需要嵌套的匿名块来忽略它的代码。但我总是得到这个错误:
Error report - ORA-06550: line 41, column 14: PL/SQL: ORA-00942: table or view does not exist ORA-06550: line 33, column 10: PL/SQL: SQL Statement ignored
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;
你不能用不存在的 table 编译代码,但你可以尝试使用 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;
此外,您在这里不需要例外,您可以使用系统视图检查 table 是否存在:
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