Re: Returning a row from a function with an appended array field - Mailing list pgsql-general

From Wes Cravens
Subject Re: Returning a row from a function with an appended array field
Date
Msg-id 4EBB2EC7.7010103@cortex-it.com
Whole thread Raw
In response to Re: Returning a row from a function with an appended array field  (David Johnston <polobo@yahoo.com>)
Responses Re: Returning a row from a function with an appended array field  (Alban Hertroys <haramrae@gmail.com>)
List pgsql-general
On 11/9/2011 7:34 PM, David Johnston wrote:
> On Nov 9, 2011, at 20:19, Wes Cravens <wcravens@cortex-it.com> wrote:
>
>> I have an adjacency list kind of table
>>
>> CREATE TABLE thingy (
>>    id int,
>>    parent int
>> );
>>
>> I'd like to be able to write a procedural function that returns a row or
>> rows from this table with an appended field that represents the children.
>>
>> Something like this pseudo code:
>>
>> FOR row IN SELECT * FROM thingy
>> LOOP
>>  RETURN NEXT row,[SELECT id FROM thingy WHERE parent_id = id]
>> END LOOP,
>> RETURN
>>
>> Any help much appreciated,
>>
>> Wes
>>
>
> Use "WITH RECURSIVE" instead of a function.
>

I apologize but I don't know how that would work.  An example would help.

Also... my pseudo code above was a little flawed:
  RETURN NEXT row,[SELECT id FROM thingy WHERE parent_id = id]
should be...
  RETURN NEXT row,[SELECT id FROM thingy WHERE parent_id = row.id]


pgsql-general by date:

Previous
From: David Johnston
Date:
Subject: Re: Returning a row from a function with an appended array field
Next
From: Tom Lane
Date:
Subject: Re: PostgreSQL 9.0.5 concat Issue