pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: KÖPFERL Robert <robert.koepferl@sonorys.at>
To: 'A. Kulikov' <a.kulikov@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: User Defined Functions Errors
Date: Tue, 19 Apr 2005 09:23:46 +0200
Message-ID: <ED4E30DD9C43D5118DFB00508BBBA76EB166BB@neptun.sonorys.at> (raw)

Have a try with RAISE NOTE or RAISE EXCEPTION
keep in mind that exceptions should be exceptional. So a good idea of
whether to use them is to ask 'Do I expect such error' or 'is an explicit
error useful for the caller'. I'ts often better to just return an empty
relation

|-----Original Message-----
|From: A. Kulikov [mailto:a.kulikov@gmail.com]
|Sent: Montag, 18. April 2005 20:32
|To: pgsql-sql@postgresql.org
|Subject: [SQL] User Defined Functions Errors
|
|
|How to I return an error from inside a user defined function? For
|example the following:
|
|CREATE or REPLACE FUNCTION drop_node (integer) RETURNS text
|AS '
|
|DECLARE
|
|mleft INTEGER; 
|mright INTEGER;
|
|BEGIN
|
|-- Check if the desired node exists
|SELECT lft, rgt FROM structure WHERE id = $1 INTO mleft, mright;
|
|IF mleft IS NULL THEN
|  RETURN ''No entry found with an id of ''||$2;
|END IF;
|
|-- Drop the node and its subtree
|DELETE FROM structure WHERE lft >= mleft AND rgt <= mright;
|
|-- Close the gap
|UPDATE structure SET rgt = rgt - (mright - mleft + 1) WHERE 
|rgt > mright;
|UPDATE structure SET lft = lft - (mright - mleft + 1) WHERE 
|lft > mleft;
|
|RETURN ''ok'';
|
|END;
|'
|LANGUAGE 'plpgsql';
|
|Should be terminated with an error @ RETURN "No Entry found"; instead
|of returning the error text.
|
|best regards and thanks,
|
|Alex
|-- 
|The mind is essential -- http://essentialmind.com/
|
|---------------------------(end of 
|broadcast)---------------------------
|TIP 4: Don't 'kill -9' the postmaster
|



view thread (3+ messages)

Message-ID: <ED4E30DD9C43D5118DFB00508BBBA76EB166BB@neptun.sonorys.at>
Permalink:  ../ED4E30DD9C43D5118DFB00508BBBA76EB166BB@neptun.sonorys.at/
Also on:    postgresql.org/message-id/ED4E30DD9C43D5118DFB00508BBBA76EB166BB@neptun.sonorys.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: robert.koepferl@sonorys.at, a.kulikov@gmail.com
  Subject: Re: User Defined Functions Errors
  In-Reply-To: <ED4E30DD9C43D5118DFB00508BBBA76EB166BB@neptun.sonorys.at>
  References: <ED4E30DD9C43D5118DFB00508BBBA76EB166BB@neptun.sonorys.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox