From: | Kevin Murphy <murphy(at)genome(dot)chop(dot)edu> |
---|---|
To: | PostgreSQL general <pgsql-general(at)postgresql(dot)org> |
Subject: | Note on scalar subquery syntax |
Date: | 2005-08-03 13:40:26 |
Message-ID: | 42F0C94A.2030607@genome.chop.edu |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
I thought this might be helpful in the future to other duffers such as
myself.
The following is my big contribution to the documentation of the use of
scalar subqueries ;-):
You have to wrap a scalar subquery in its own parentheses even where you
might think it to be unnecessary, such as when the subquery is the sole
argument to a function.
As an example, I wrote a function to explode, or unpack, the elements of
an array onto separate rows (its return type is SETOF whatever), but it
took me a while to figure out how to use it effectively in queries.
You have to use it like this:
RIGHT--> select * from array_explode((select array_col from table1 where
col2 = 'something'));
Note the "extra" set of parentheses. These are crucial: the syntax is
invalid without these, as in:
WRONG--> select * from array_explode(select array_col from table1 where
col2 = 'something');
And no, as mentioned in many archived list messages, you can NOT do the
following, which is what a lot of people (including me) seem to try first:
WRONG--> select array_explode(array_col) from table1 where col2 =
'something');
(The previous command results in the error message: "set-valued function
called in context that cannot accept a set").
-Kevin Murphy
From | Date | Subject | |
---|---|---|---|
Next Message | Jaime Casanova | 2005-08-03 14:17:53 | Re: Hello |
Previous Message | Richard Huxton | 2005-08-03 12:15:38 | Re: Hello |