Re: Selecting values from comma separated string

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting values from comma separated string
Date: 2009-08-26 13:30:36
Message-ID: 20090826133036.GK32599@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

In response to Nacef LABIDI :
> Hi all,
>
> I want to write a function that takes as param a comma separated values string
> and perform a select matching these values.
>
> Here is the string '1,3,7,8'
>
> And I wan to perform a : SELECT * FROM my_table WHERE id IN (1, 3, 7, 8);

Use EXECUTE sql_string, see
http://www.postgresql.org/docs/8.4/interactive/plpgsql-control-structures.html

For instance, simple example, untested:

create function foo (my_string) returns setof record as $$
declare
sql text;
begin
sql:='SELECT * FROM my_table WHERE id IN (' || $1 || ')';
return query execute sql;
end;

The variable sql contains the whole query, and then execute that.

HTH, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2009-08-26 13:32:02 Re: Selecting values from comma separated string
Previous Message Nacef LABIDI 2009-08-26 13:19:38 Selecting values from comma separated string