Re: How to pass array of values to a stored procedure - Mailing list pgsql-sql

From Tony Wasson
Subject Re: How to pass array of values to a stored procedure
Date
Msg-id 6d8daee30607181033g1c56b0b9x818087f6d5d4005c@mail.gmail.com
Whole thread Raw
In response to Re: How to pass array of values to a stored procedure  ("Tony Wasson" <ajwasson@gmail.com>)
List pgsql-sql
On 7/18/06, Tony Wasson <ajwasson@gmail.com> wrote:
> On 7/18/06, Curtis Scheer <Curtis@daycos.com> 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).
> >

Ahhh... Here's an example using Tom's recommended field=ANY (arrayvalue) SQL.


CREATE TABLE ids
(       id      INTEGER       , PRIMARY KEY (id)
);

INSERT INTO ids VALUES (1);
INSERT INTO ids VALUES (2);
INSERT INTO ids VALUES (3);

CREATE OR REPLACE FUNCTION example_array_input(INT[]) RETURNS SETOF ids AS
$BODY$
DECLARE       in_clause ALIAS FOR $1;       clause  TEXT;       rec     RECORD;
BEGIN       FOR rec IN SELECT id FROM ids WHERE id = ANY(in_clause)       LOOP               RETURN NEXT rec;       END
LOOP;      -- final return       RETURN;
 
END
$BODY$ language plpgsql;

SELECT * FROM example_array_input('{1,2,4,5,6}'::INT[]);


pgsql-sql by date:

Previous
From: "Tony Wasson"
Date:
Subject: Re: How to pass array of values to a stored procedure
Next
From: Emi Lu
Date:
Subject: Re: Like with special character