在postgresql中,PRAGMA EXCEPTION等价于什么?



如何为 postgresql 实现编译指示exception_init。 在这里编写我想迁移到postgresql的oracle代码,我想用户定义的异常或错误代码而不是PostgreSQL错误代码。

declare
not_dropable exception;
pragma exception_init (not_dropable, -942);      
begin
execute immediate 'drop table    &t' ;
exception
when not_dropable then
dbms_output.put_line ( 'Table &t  does not exist ' );
end;

Postgres中没有直接的等价物。

您唯一的选择是检查错误代码。报告不存在的表,其中包含 SQLSTATE42P01或名称undefined_object

等效的将是这样的:

do
$$
declare
l_tablename text := '....';
begin
execute format('drop table %I', l_tablename);
exception 
when undefined_object then 
raise notice 'Table % does not exist', l_tablename;
end;
$$

或者,您可以直接检查错误代码when sqlstate '42704' then ...

最新更新