X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 0C9BC30F0E7 for ; Tue, 17 Jun 2003 09:59:02 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 93349-03 for ; Tue, 17 Jun 2003 06:58:30 -0300 (ADT) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by svr1.postgresql.org (Postfix) with ESMTP id E7A1B30F0E5 for ; Tue, 17 Jun 2003 06:58:29 -0300 (ADT) Received: from [192.168.5.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 4.0.6) with ESMTP-TLS id 1805821; Tue, 17 Jun 2003 02:32:11 -0700 Message-ID: <3EEEE5CE.1000509@joeconway.com> Date: Tue, 17 Jun 2003 02:56:30 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Radu-Adrian Popescu Cc: pgsql-sql@postgresql.org Subject: Re: Catching DML exceptions in PL/pgSQL References: <5.2.1.1.0.20030617121526.02185a50@192.168.0.1> In-Reply-To: <5.2.1.1.0.20030617121526.02185a50@192.168.0.1> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200306/175 X-Sequence-Number: 13933 Radu-Adrian Popescu wrote: > ..................... > begin > _res.code:=1; > select id into iid from log where id=_id; > if not found then begin > _res.msg:=''insert''; > *insert into log (log, data) values (_log, _data); > if not found* then begin > _res.msg:=_res.msg || '' error''; > _res.code:=-1; > end; > end if; > end; > else begin > ..................... > The thing is if _data (parameter) is null and table has a (data <> null) check, > the insert would fail and abort the function before my "if not found" test. You could test for _data is null, and if so check attnotnull in pg_attribute. E.g. something like: declare iattnotnull bool [...] begin if _data is null then select into iattnotnull attnotnull from pg_catalog.pg_attribute where attrelid = 'log'::regclass and attname = 'data'; if iattnotnull then _res.code := -1; [...] > Is there anything I can do to make sure the function always returns _res ? > Something along the lines of Oracle's exception handling, or the @@error trick > in mssql ? There is currently no way to "catch" the exception in PL/pgSQL, but maybe the above would work for you. HTH, Joe