mirror of
https://github.com/postgres/postgres.git
synced 2025-08-05 07:41:25 +03:00
Add an example of WITH (UPDATE RETURNING) INSERT to the INSERT ref page.
Per a discussion with Gavin Flower. This barely scratches the surface of potential WITH (something RETURNING) use cases, of course, but it's one of the simplest compelling examples I can think of.
This commit is contained in:
@@ -298,6 +298,20 @@ INSERT INTO tictactoe (game, board)
|
|||||||
<programlisting>
|
<programlisting>
|
||||||
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
|
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
|
||||||
RETURNING did;
|
RETURNING did;
|
||||||
|
</programlisting>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
Increment the sales count of the salesperson who manages the
|
||||||
|
account for Acme Corporation, and record the whole updated row
|
||||||
|
along with current time in a log table:
|
||||||
|
<programlisting>
|
||||||
|
WITH upd AS (
|
||||||
|
UPDATE employees SET sales_count = sales_count + 1 WHERE id =
|
||||||
|
(SELECT sales_person FROM accounts WHERE name = 'Acme Corporation')
|
||||||
|
RETURNING *
|
||||||
|
)
|
||||||
|
INSERT INTO employees_log SELECT *, current_timestamp FROM upd;
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</para>
|
</para>
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
Reference in New Issue
Block a user