如何为postgresql实现pragma 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中没有直接的等价物。
你唯一的选择是检查error code。使用SQLSTATE 42P01
或名称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 ...