Re: Scaler forms as function arguments - Mailing list pgsql-sql

From Joe Conway
Subject Re: Scaler forms as function arguments
Date
Msg-id 3FC6E15E.2030300@joeconway.com
Whole thread Raw
In response to Re: Scaler forms as function arguments  (Richard Huxton <dev@archonet.com>)
Responses Re: Scaler forms as function arguments  (Greg Stark <gsstark@mit.edu>)
List pgsql-sql
Richard Huxton wrote:
> On Wednesday 26 November 2003 15:40, Andreas Tille wrote:
>>I want to write a function of the following type
>>
>>   CREATE FUNCTION test ( <scalar form type> )
>>         RETURNS setof MyTable
>>         AS
>>         'SELECT * FROM MyTable WHERE id IN $1'
>>         LANGUAGE 'SQL' ;
> 
> Not as you've done it. You could pass in text "(1,2,3)", build your query and 
> use EXECUTE to execute it. Alternatively, you might be able to do it with an 
> array parameter (sorry, I don't use arrays, so I can't be sure).

In 7.4 you could use an array. It would look like this:

CREATE TABLE mytable (id int, idval text);
INSERT INTO mytable VALUES (1,'a');
INSERT INTO mytable VALUES (2,'b');
INSERT INTO mytable VALUES (3,'c');

CREATE FUNCTION test (int[]) RETURNS setof MyTable AS '
SELECT * FROM mytable WHERE id = ANY ($1)
' LANGUAGE 'SQL' ;

regression=# SELECT * FROM test(ARRAY[1,3]); id | idval
----+-------  1 | a  3 | c
(2 rows)

HTH,

Joe




pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: Scaler forms as function arguments
Next
From: Greg Stark
Date:
Subject: Re: Scaler forms as function arguments