pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Sub-query as function argument
4+ messages / 3 participants
[nested] [flat]

* Sub-query as function argument
@ 2005-12-20 13:42 Michael Burke <michael@engtech.ca>
  2005-12-20 14:52 ` Re: Sub-query as function argument Jaime Casanova <systemguards@gmail.com>
  2005-12-20 15:07 ` Re: Sub-query as function argument Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 4+ messages in thread

From: Michael Burke @ 2005-12-20 13:42 UTC (permalink / raw)
  To: pgsql-sql

Is it possible to execute a SELECT query as an argument to a function?

Example:

SELECT my_func('Sample', NULL, SELECT MIN(year) FROM audio);

In my particular case, my_func inserts columns into another table; I wish to 
use values from another table as the arguments.  The interior SELECT will 
return only a single value; perhaps it needs to be wrapped in another 
function?

TIA.
Mike.

-- 
Michael Burke
michael@engtech.ca



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Sub-query as function argument
  2005-12-20 13:42 Sub-query as function argument Michael Burke <michael@engtech.ca>
@ 2005-12-20 14:52 ` Jaime Casanova <systemguards@gmail.com>
  2005-12-20 15:04   ` Re: Sub-query as function argument Michael Burke <michael@engtech.ca>
  1 sibling, 1 reply; 4+ messages in thread

From: Jaime Casanova @ 2005-12-20 14:52 UTC (permalink / raw)
  To: Michael Burke <michael@engtech.ca>; +Cc: pgsql-sql

On 12/20/05, Michael Burke <michael@engtech.ca> wrote:
> Is it possible to execute a SELECT query as an argument to a function?
>

have you tried?

--
regards,
Jaime Casanova
(DBA: DataBase Aniquilator ;)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Sub-query as function argument
  2005-12-20 13:42 Sub-query as function argument Michael Burke <michael@engtech.ca>
  2005-12-20 14:52 ` Re: Sub-query as function argument Jaime Casanova <systemguards@gmail.com>
@ 2005-12-20 15:04   ` Michael Burke <michael@engtech.ca>
  0 siblings, 0 replies; 4+ messages in thread

From: Michael Burke @ 2005-12-20 15:04 UTC (permalink / raw)
  To: pgsql-sql

On December 20, 2005 10:52 am, Jaime Casanova wrote:
> have you tried?

Yes:

=> SELECT my_func('Sample', NULL, SELECT MIN(year) FROM audio);
ERROR:  syntax error at or near "SELECT" at character 32

PostgreSQL 7.4.9, myfunc is pl/pgsql.

Just found a working method, though:

=> SELECT my_func('Sample', NULL, MIN(year)) FROM audio [ WHERE ... ];

-- 
Michael Burke
michael@engtech.ca



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Sub-query as function argument
  2005-12-20 13:42 Sub-query as function argument Michael Burke <michael@engtech.ca>
@ 2005-12-20 15:07 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 4+ messages in thread

From: Tom Lane @ 2005-12-20 15:07 UTC (permalink / raw)
  To: Michael Burke <michael@engtech.ca>; +Cc: pgsql-sql

Michael Burke <michael@engtech.ca> writes:
> Is it possible to execute a SELECT query as an argument to a function?

> SELECT my_func('Sample', NULL, SELECT MIN(year) FROM audio);

You need parentheses around the sub-SELECT.

  SELECT my_func('Sample', NULL, (SELECT MIN(year) FROM audio));

This is generally true everywhere in expressions, not just in
function arguments.  Without the parens, it's often ambiguous
what's subselect and what's outer query.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2005-12-20 15:07 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-12-20 13:42 Sub-query as function argument Michael Burke <michael@engtech.ca>
2005-12-20 14:52 ` Jaime Casanova <systemguards@gmail.com>
2005-12-20 15:04   ` Michael Burke <michael@engtech.ca>
2005-12-20 15:07 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox