Unix Technical Forum

PL/pgsql: function passing argument to IN operator

This is a discussion on PL/pgsql: function passing argument to IN operator within the pgsql Sql forums, part of the PostgreSQL category; --> Hello list, I'm trying to write a function that uses one of its arguments as an input to an ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 06:00 PM
Daniel Myers
 
Posts: n/a
Default PL/pgsql: function passing argument to IN operator

Hello list,
I'm trying to write a function that uses one of its arguments as
an input to an IN operator in a WHERE clause. I.e., something like:

-- ids are actually integers
CREATE FUNCTION foo(ids SOMETYPE) RETURNS [whatever] AS $$
BEGIN
SELECT id, [other stuff] FROM table WHERE [other conditions] AND
id IN (ids);
RETURN;
END
$$ plpgsql

I had wanted to avoid using the EXECUTE statement and dynamically
creating the query string, because 1) I'd rather not recompile the
query each time and 2) I'm doing a SELECT INTO, which apparently
doesn't work with EXECUTE.

Can anyone help me out?

Thanks,
Daniel

P.S.: Sorry if capitalizing SQL keywords is bad list etiquette...

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 06:00 PM
Pavel Stehule
 
Posts: n/a
Default Re: PL/pgsql: function passing argument to IN operator

Hello

it has sense only for array type

so you can try:
create replace function foo(anyarray)
returns bool as $$
begin
return (select 1 = any($1));
end ; $$ language plpgsql;


postgres=# select foo(array[1,2,3]);
foo
-----
t
(1 row)

postgres=# select foo(array[2,3]);
foo
-----
f
(1 row)

Regards
Pavel Stehule

On 29/12/2007, Daniel Myers <danielsmyers@gmail.com> wrote:
> Hello list,
> I'm trying to write a function that uses one of its arguments as
> an input to an IN operator in a WHERE clause. I.e., something like:
>
> -- ids are actually integers
> CREATE FUNCTION foo(ids SOMETYPE) RETURNS [whatever] AS $$
> BEGIN
> SELECT id, [other stuff] FROM table WHERE [other conditions] AND
> id IN (ids);
> RETURN;
> END
> $$ plpgsql
>
> I had wanted to avoid using the EXECUTE statement and dynamically
> creating the query string, because 1) I'd rather not recompile the
> query each time and 2) I'm doing a SELECT INTO, which apparently
> doesn't work with EXECUTE.
>
> Can anyone help me out?
>
> Thanks,
> Daniel
>
> P.S.: Sorry if capitalizing SQL keywords is bad list etiquette...
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
>


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

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 01:50 PM.


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