Re: plpgsql recursion

From: Rod Taylor <rbt(at)rbt(dot)ca>
To: Stefano Vita Finzi <stefano(dot)vita(at)pronesis(dot)it>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: plpgsql recursion
Date: 2003-05-23 01:59:43
Message-ID: 1053655182.279.28.camel@jester
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> IF t_rec.node = testing THEN
> RETURN ''Circular'';
> ELSE
> PERFORM dba_test(t_rec.node,testing);
> END IF;
> I would use this function BEFORE inserting the new row. But if i try SELECT
> dba_test(4,1); i don't have the result i expect. Can i you give me an hint
> where am i wrong?

It's probably working as it's written :) Once it finds 'Curcular' and
returns it to the recursed call of dba_test, you immediately throw away
the return value.

PERFORM should probably be replaced with:

ELSE
-- ret is a variable
SELECT dba_test(t_rec.node, testing) INTO ret;

IF ret = ''Circular'' THEN
RETURN ret;
END IF;
END IF;

More to the point, if you're attempting to prevent circular items, and
this function will be used in the form of a trigger, then simply RAISE
EXCEPTION. This way you can ignore having to deal with returned values.

--
Rod Taylor <rbt(at)rbt(dot)ca>

PGP Key: http://www.rbt.ca/rbtpub.asc

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Alexey Dashevsky 2003-05-23 05:51:49 Re: numeric fields and null
Previous Message Bruno Wolff III 2003-05-23 01:48:57 Re: AS?