Re: Plpgsql function with unknown number of args

From: Tony Caduto <tony_caduto(at)amsoftwaredesign(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Plpgsql function with unknown number of args
Date: 2005-04-18 19:58:45
Message-ID: 42641175.6060602@amsoftwaredesign.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

you coud pass in criteria as a delimted string, then
pull out each arg something like this

CREATE or REPLACE FUNCTION test_func( varchar)
RETURNS pg_catalog.void AS
$BODY$
DECLARE
IN_ARRAY text[] ;
arg1 varchar;
arg2 varchar;
arg3 varchar

begin
IN_ARRAY = string_to_array($1,'~^~');

arg1 = IN_ARRAY[1]
arg2 = IN_ARRAY[2]
arg3 = IN_ARRAY[3]

Then call the function like this:

select test_func('bla^~^bla~^~yada');

This example does not return anything, but you could build a select from the args you passed in then return a cursor.

normally PG is limited to 32 args (unless special compiled to support more), but with this technique you can pass in as many as you want.

hope this helps.

Tony Caduto
AM Software Design
Home of PG Lightning Admin for Postgresql 8.x
http://www.amsoftwaredesign.com

> CREATE FUNCTION myfunc(criteria) RETURNS ???? AS $$
>
> BEGIN
>
> BEGIN
> FOREACH crit IN criteria
> critsql := "b = 'crit' OR "
> NEXT crit
> END;
>
> PERFORM "SELECT a, b FROM mytable WHERE (" critsql ");"
>
> END;
> $$ LANGUAGE plpgsql;
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2005-04-18 20:00:39 Re: Urgent
Previous Message Harald Fuchs 2005-04-18 19:49:25 Re: Plpgsql function with unknown number of args