Thread: psql sqlstate return code access
Hi,
I have a requirement to log into a table the completion code for each statement being submitted in a psql script.
I've looked around and can't seem to find how to access the sqlstate completion code. Is there a system variable in psql available or some other trick to get it?
For example
insert into x select … from y;
insert into log(message, code,timestamp) values('insert into x',:SQLSTATE,now());
where :SQLSTATE contains the completion code of the prior statement.
Thanks in advance
Doug Little
On Mon, Mar 16, 2015 at 4:23 PM, Little, Doug C <Doug.Little@vend.frb.org> wrote: > insert into x select … from y; > > > > insert into log(message, code,timestamp) values('insert into > x',:SQLSTATE,now()); > > I'm pretty sure you have to wrap it into a plpgsql function: http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html Moreover, if the command executes correctly you would have no exception and sql state will be 0. Hope this helps. Luca