Re: Passing array to PL/SQL and looping

From: Greg Johnson <gregj(at)interprose(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Passing array to PL/SQL and looping
Date: 2002-09-27 15:32:21
Message-ID: 1033140741.4254.12.camel@watto.interprose.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Is it possible to construct and return an array with plpgsql

like..

CREATE FUNCTION test_array( ) RETURNS VARCHAR[] AS '
DECLARE
return_array VARCHAR[];
BEGIN
return_array[0] := ''test'';
return_array[1] := ''test 1'';
return_array[2] := ''test 2'';
RETURN (return_array);
END;'
LANGUAGE 'plpgsql';

I get the following error when I try to run it:
ERROR: parse error at or near "[" on line 4.

If this worked I could clean up a LOT of hacky plpgsql code I have had
to write.

On Thu, 2002-09-26 at 18:20, Josh Berkus wrote:

> Peter,
>
> > I'm a newbie to PL/SQL and need help badly. I'm trying to pass my array of
> > id's to the function and then loop through until the array is empty. I know
> > there must be atleast five things I'm doing wrong.
>
> Simplified example:
>
> CREATE FUNCTION test_array (
> INT[] )
> RETURNS INT AS '
> DECLARE id_array ALIAS for $1;
> count_it INT;
> BEGIN
> count_it := 1;
> WHILE id_array[count_it] LOOP
> count_it := count_it + 1;
> END LOOP;
> RETURN (count_it - 1);
> END;'
> LANGUAGE 'plpgsql';
>
> returns the number of elements in the supplied array.
>
> --
> Josh Berkus
> josh(at)agliodbs(dot)com
> Aglio Database Solutions
> San Francisco
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
>
--
Greg Johnson <gregj(at)interprose(dot)com>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2002-09-27 16:01:27 Re: Date/Time types
Previous Message Mathieu Arnold 2002-09-27 13:27:07 Re: 7.3 schemas