Skip to content

Commit 8ad5827

Browse files
committed
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.
1 parent b6bc481 commit 8ad5827

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

doc/src/sgml/ref/insert.sgml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,20 @@ INSERT INTO tictactoe (game, board)
298298
<programlisting>
299299
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
300300
RETURNING did;
301+
</programlisting>
302+
</para>
303+
304+
<para>
305+
Increment the sales count of the salesperson who manages the
306+
account for Acme Corporation, and record the whole updated row
307+
along with current time in a log table:
308+
<programlisting>
309+
WITH upd AS (
310+
UPDATE employees SET sales_count = sales_count + 1 WHERE id =
311+
(SELECT sales_person FROM accounts WHERE name = 'Acme Corporation')
312+
RETURNING *
313+
)
314+
INSERT INTO employees_log SELECT *, current_timestamp FROM upd;
301315
</programlisting>
302316
</para>
303317
</refsect1>

0 commit comments

Comments
 (0)