agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedUpdate Returning as subquery
3+ messages / 3 participants
[nested] [flat]
* Update Returning as subquery
@ 2014-08-10 16:15 pascal+postgres@ensieve.org
2014-08-10 17:17 ` Re: Update Returning as subquery Tom Lane <tgl@sss.pgh.pa.us>
2014-08-10 18:10 ` Re: Update Returning as subquery David G Johnston <david.g.johnston@gmail.com>
0 siblings, 2 replies; 3+ messages in thread
From: pascal+postgres@ensieve.org @ 2014-08-10 16:15 UTC (permalink / raw)
To: pgsql-sql
Hi,
I want to update some values in a table, and need to count the number of values actually changed; but ROW_COUNT returns the number of total rows touched.
But this gives a syntax error:
SELECT count(*) INTO my_count
FROM (
UPDATE stuff
SET value = maybe_null(key)
--^
WHERE value IS NULL
RETURNING value ) AS t
WHERE value IS NOT NULL;
Why is that forbidden? Isn't the purpose of a RETURNING clause to return values like a SELECT statement would, and shouldn't it therefore be allowed to occur in the same places?
I switched it around using a CTE in this case:
WITH new_values AS (
SELECT key, maybe_null(key) AS value
FROM stuff WHERE value IS NULL)
UPDATE stuff AS s
SET value = n.value
FROM new_values AS n
WHERE n.key = s.key
AND n.value IS NOT NULL;
Which only touches rows that will be changed and returns a useful ROW_COUNT, but needs a join.
Cheers,
--
Pascal Germroth
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Update Returning as subquery
2014-08-10 16:15 Update Returning as subquery pascal+postgres@ensieve.org
@ 2014-08-10 17:17 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2014-08-10 17:17 UTC (permalink / raw)
To: pascal+postgres@ensieve.org; +Cc: pgsql-sql
pascal+postgres@ensieve.org writes:
> Why is that forbidden? Isn't the purpose of a RETURNING clause to return values like a SELECT statement would, and shouldn't it therefore be allowed to occur in the same places?
An INSERT/UPDATE/DELETE RETURNING can be used as a CTE, not as a
sub-select. Sub-selects aren't sufficiently independent of the outer
query, eg they might be evaluated more than once, or not at all, or
only partially.
regards, tom lane
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Update Returning as subquery
2014-08-10 16:15 Update Returning as subquery pascal+postgres@ensieve.org
@ 2014-08-10 18:10 ` David G Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: David G Johnston @ 2014-08-10 18:10 UTC (permalink / raw)
To: pgsql-sql
pascal+postgres wrote
> Hi,
>
> I want to update some values in a table, and need to count the number of
> values actually changed; but ROW_COUNT returns the number of total rows
> touched.
>
> But this gives a syntax error:
>
> SELECT count(*) INTO my_count
> FROM (
> UPDATE stuff
> SET value = maybe_null(key)
> --^
> WHERE value IS NULL
> RETURNING value ) AS t
> WHERE value IS NOT NULL;
>
> Why is that forbidden? Isn't the purpose of a RETURNING clause to return
> values like a SELECT statement would, and shouldn't it therefore be
> allowed to occur in the same places?
>
>
>
> I switched it around using a CTE in this case:
>
> WITH new_values AS (
> SELECT key, maybe_null(key) AS value
> FROM stuff WHERE value IS NULL)
> UPDATE stuff AS s
> SET value = n.value
> FROM new_values AS n
> WHERE n.key = s.key
> AND n.value IS NOT NULL;
>
> Which only touches rows that will be changed and returns a useful
> ROW_COUNT, but needs a join.
>
> Cheers,
The following should work...
WITH do_uodate AS (
UPDATE ... WHERE value IS NULL
RETURNING value
)
SELECT count(*) FROM do_update WHERE value IS NOT NULL
I don't know why it doesn't work in subquery form but other than syntax this
and your first form are equivalent.
David J.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/Update-Returning-as-subquery-tp5814366p5814370.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2014-08-10 18:10 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-08-10 16:15 Update Returning as subquery pascal+postgres@ensieve.org
2014-08-10 17:17 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-08-10 18:10 ` David G Johnston <david.g.johnston@gmail.com>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox