Search Postgresql Archives

Note on scalar subquery syntax

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux