postgresql中的PRAGMA EXCEPTION等价物是什么?

问题描述 投票:0回答:1

如何为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;
postgresql postgresql-10
1个回答
1
投票

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 ...

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.