X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 0C6099FA5BF for ; Tue, 18 Jul 2006 14:29:06 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 63638-01-3 for ; Tue, 18 Jul 2006 14:28:56 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nz-out-0102.google.com (nz-out-0102.google.com [64.233.162.198]) by postgresql.org (Postfix) with ESMTP id 55C419FB1E4 for ; Tue, 18 Jul 2006 14:24:38 -0300 (ADT) Received: by nz-out-0102.google.com with SMTP id z3so561614nzf for ; Tue, 18 Jul 2006 10:24:36 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=IVvJ3Y1aGoL4EcXad4lWf0zYnfpDOQayiiTRaTyncuhu302/fUBvyrtNkcRyT5qRLIUAoZsONrPPvTD0ATbXAe2JE6ibi3AV7k4hRz5JaM41v4jUqW0vzCUDdNNgcAjYOPeNjD6RdmDjOuvhh+j1Ve7XfzHEQa3n3ZEaFCPbg3g= Received: by 10.65.121.20 with SMTP id y20mr3278371qbm; Tue, 18 Jul 2006 10:24:36 -0700 (PDT) Received: by 10.65.181.16 with HTTP; Tue, 18 Jul 2006 10:24:36 -0700 (PDT) Message-ID: <6d8daee30607181024w74cf4673gf2f55066219dc21@mail.gmail.com> Date: Tue, 18 Jul 2006 10:24:36 -0700 From: "Tony Wasson" To: "Curtis Scheer" Subject: Re: How to pass array of values to a stored procedure Cc: pgsql-sql@postgresql.org In-Reply-To: <031936836C46D611BB1B00508BE7345D048DAAB8@gatekeeper.daycos.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <031936836C46D611BB1B00508BE7345D048DAAB8@gatekeeper.daycos.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/234 X-Sequence-Number: 25808 On 7/18/06, 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). > Here's a very simple example. However, I think passing arrays of INTs around is dirty. I wasn't able to do this without a FOR ...IN EXECUTE statement. 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 -- conver the array to a text string and make it LOOK like an IN statement clause := in_clause; clause := trim(leading '{' FROM clause); clause := trim(trailing '}' FROM clause); FOR rec IN EXECUTE 'SELECT id FROM ids WHERE id 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[]);