pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
User Defined Functions Errors
3+ messages / 3 participants
[nested] [flat]

* User Defined Functions Errors
@ 2005-04-18 18:32 A. Kulikov <a.kulikov@gmail.com>
  2005-04-18 19:07 ` Re: User Defined Functions Errors Michael Fuhr <mike@fuhr.org>
  0 siblings, 1 reply; 3+ messages in thread

From: A. Kulikov @ 2005-04-18 18:32 UTC (permalink / raw)
  To: pgsql-sql

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/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: User Defined Functions Errors
  2005-04-18 18:32 User Defined Functions Errors A. Kulikov <a.kulikov@gmail.com>
@ 2005-04-18 19:07 ` Michael Fuhr <mike@fuhr.org>
  0 siblings, 0 replies; 3+ messages in thread

From: Michael Fuhr @ 2005-04-18 19:07 UTC (permalink / raw)
  To: A. Kulikov <a.kulikov@gmail.com>; +Cc: pgsql-sql

On Mon, Apr 18, 2005 at 10:32:26PM +0400, A. Kulikov wrote:
>
> How to I return an error from inside a user defined function?

Use RAISE.  See "Errors and Messages" in the PL/pgSQL documentation.

http://www.postgresql.org/docs/8.0/interactive/plpgsql-errors-and-messages.html

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: User Defined Functions Errors
@ 2005-04-19 07:23 KÖPFERL Robert <robert.koepferl@sonorys.at>
  0 siblings, 0 replies; 3+ messages in thread

From: KÖPFERL Robert @ 2005-04-19 07:23 UTC (permalink / raw)
  To: 'A. Kulikov' <a.kulikov@gmail.com>; +Cc: pgsql-sql

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
|



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2005-04-19 07:23 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-18 18:32 User Defined Functions Errors A. Kulikov <a.kulikov@gmail.com>
2005-04-18 19:07 ` Michael Fuhr <mike@fuhr.org>
2005-04-19 07:23 Re: User Defined Functions Errors KÖPFERL Robert <robert.koepferl@sonorys.at>

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