Re: Putting an aggregate value in an UPDATE statement...

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Leif Biberg Kristensen <leif(at)solumslekt(dot)org>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Putting an aggregate value in an UPDATE statement...
Date: 2010-06-01 21:34:39
Message-ID: 9733.1275428079@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Leif Biberg Kristensen <leif(at)solumslekt(dot)org> writes:
>> You need them to syntactically separate the sub-select from the outer
>> select. If SQL didn't require them, then in something like
>>
>> UPDATE question_choices SET total_rows =
>> select count(*) from care_lesson where something
>>
>> it wouldn't be clear whether the WHERE clause was meant to attach
>> to the sub-select or the outer UPDATE.

> A couple of days ago, a was a little stumped by this. I had written a plain
> SQL function with one integer parameter, and then tried to use a SELECT as
> input parameter as in

> SELECT myfunc(SELECT foo FROM bar WHERE baz);

> It took a while before I realized that I needed to put the query in another
> set of parentheses:

> SELECT myfunc((SELECT foo FROM bar WHERE baz));

> worked just fine. I fail to see the ambiguity here, though.

Well, the main point is that the possible ambiguity means that SQL has
mandated you put parentheses around any sub-select. However, if you're
claiming there is no possible ambiguity inside a function call, compare:

select myfunc((select integer_col from foo where bar = 5+4))
select myfunc((select integer_col from foo where bar = 5)+4)

These mean different things, and you couldn't tell 'em apart without
the inner parentheses.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Leif Biberg Kristensen 2010-06-01 21:43:32 Re: Putting an aggregate value in an UPDATE statement...
Previous Message Leif Biberg Kristensen 2010-06-01 21:26:05 Re: Putting an aggregate value in an UPDATE statement...