将异常错误插入 table

insert the exception error into the table

我有这样的代码:

do $$
begin
    create table yyy(a int);
    create table yyy(a int); -- this will cause an error

    create table y(b varchar(250));

exception when others then 

    raise notice 'The transaction is in an uncommittable state. '
                 'Transaction was rolled back';

--insert into y select  -- This line doesn't work.  
    raise notice '% %', SQLERRM, SQLSTATE;
end;


$$ language 'plpgsql';

我想把造成的错误插入到Y中table。

我该怎么做?

我想将 NOTICE: relation "yyy" already exists 42P07 插入 Y table。

您可能希望在单独的脚本中创建 Table Y,以便它在引发异常之前存在。

create table y(b varchar(250));

您可以在单独的块中执行您的命令,而无需创建 table Y。像这样:

begin
    create table yyy(a int);
    create table yyy(a int); -- this will cause an error

exception when others then 
    raise notice 'The transaction is in an uncommittable state. '
             'Transaction was rolled back';
    insert into y(b) values('NOTICE:  relation "yyy" already exists 42P07');
    raise notice '% %', SQLERRM, SQLSTATE;
end;