Unix Technical Forum

How to pass array of values to a pgplsql function

This is a discussion on How to pass array of values to a pgplsql function within the Pgsql General forums, part of the PostgreSQL category; --> Does anyone have any examples of how I would make a stored procedure in plpgsql that would allow for ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 10:39 AM
Curtis Scheer
 
Posts: n/a
Default How to pass array of values to a pgplsql function

Does anyone have any examples of how I would make a stored procedure in
plpgsql that would allow for passing a list or arrays of values to be used
in an sql IN clause? Like so: select * from table where field1 in (values).
Is this possible?


Thanks,

Curtis

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 10:40 AM
Erik Jones
 
Posts: n/a
Default Re: How to pass array of values to a pgplsql function

Curtis Scheer wrote:
> Does anyone have any examples of how I would make a stored procedure in
> plpgsql that would allow for passing a list or arrays of values to be used
> in an sql IN clause? Like so: select * from table where field1 in (values).
> Is this possible?
>
>

Well, a good thing to note here is that there is a very distinct
semantic difference between an array in postgres and what IN clauses
take as input: and array
is a data type whereas IN clauses take a parenthesized list of comma
separated values. So, if you pass an array into a function wherein
you then need
to use those values in an IN clause, you can build yourself an string of
the values in the array, comma separated of course.

e.g.

CREATE OR REPLACE FUNCTION somefunc(ids int[]) RETURNS boolean AS $sf$
DECLARE
in_values varchar;
good int;
BEGIN
FOR i IN array_upper(ids, 1) LOOP
in_values := in_values || ids[i] || ',';
END LOOP;
in_values := substring(in_values FROM 1 FOR
character_length(in_values) - 1); -- this will chop off the last comma

EXECUTE 'SELECT 1 FROM blah WHERE blah.id IN (' || in_values ||');'
INTO good;
IF(good = 1) THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;
$sf$ LANGUAGE plpgsql;

Or, it may be easier given whatever your situation to simply use the
array as the argument to a row-wise AND or SOME expression.

--
erik jones <erik@myemma.com>
software development
emma(r)


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 10:40 AM
Erik Jones
 
Posts: n/a
Default Re: How to pass array of values to a pgplsql function

Erik Jones wrote:
> Curtis Scheer wrote:
>> Does anyone have any examples of how I would make a stored procedure in
>> plpgsql that would allow for passing a list or arrays of values to be
>> used
>> in an sql IN clause? Like so: select * from table where field1 in
>> (values).
>> Is this possible?
>>

> Well, a good thing to note here is that there is a very distinct
> semantic difference between an array in postgres and what IN clauses
> take as input: and array
> is a data type whereas IN clauses take a parenthesized list of comma
> separated values. So, if you pass an array into a function wherein
> you then need
> to use those values in an IN clause, you can build yourself an string
> of the values in the array, comma separated of course.
>
> e.g.
>
> CREATE OR REPLACE FUNCTION somefunc(ids int[]) RETURNS boolean AS $sf$
> DECLARE
> in_values varchar;
> good int;
> BEGIN
> FOR i IN array_upper(ids, 1) LOOP
> in_values := in_values || ids[i] || ',';
> END LOOP;
> in_values := substring(in_values FROM 1 FOR
> character_length(in_values) - 1); -- this will chop off the last comma
>
> EXECUTE 'SELECT 1 FROM blah WHERE blah.id IN (' || in_values ||');'
> INTO good;
> IF(good = 1) THEN
> RETURN TRUE;
> ELSE
> RETURN FALSE;
> END IF;
> END;
> $sf$ LANGUAGE plpgsql;
>
> Or, it may be easier given whatever your situation to simply use the
> array as the argument to a row-wise AND or SOME expression.
>

Whoa, replied to this out of the General mailing list before I saw the
other answers on the SQL list... Sorry guys

--
erik jones <erik@myemma.com>
software development
emma(r)


---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:07 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com