Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
dml-returning.html53 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>6.4. Returning Data from Modified Rows</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="dml-delete.html" title="6.3. Deleting Data" /><link rel="next" href="queries.html" title="Chapter 7. Queries" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">6.4. Returning Data from Modified Rows</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="dml-delete.html" title="6.3. Deleting Data">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="dml.html" title="Chapter 6. Data Manipulation">Up</a></td><th width="60%" align="center">Chapter 6. Data Manipulation</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="queries.html" title="Chapter 7. Queries">Next</a></td></tr></table><hr /></div><div class="sect1" id="DML-RETURNING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">6.4. Returning Data from Modified Rows <a href="#DML-RETURNING" class="id_link">#</a></h2></div></div></div><a id="id-1.5.5.6.2" class="indexterm"></a><a id="id-1.5.5.6.3" class="indexterm"></a><a id="id-1.5.5.6.4" class="indexterm"></a><a id="id-1.5.5.6.5" class="indexterm"></a><p>3   Sometimes it is useful to obtain data from modified rows while they are4   being manipulated.  The <code class="command">INSERT</code>, <code class="command">UPDATE</code>,5   and <code class="command">DELETE</code> commands all have an6   optional <code class="literal">RETURNING</code> clause that supports this.  Use7   of <code class="literal">RETURNING</code> avoids performing an extra database query to8   collect the data, and is especially valuable when it would otherwise be9   difficult to identify the modified rows reliably.10  </p><p>11   The allowed contents of a <code class="literal">RETURNING</code> clause are the same as12   a <code class="command">SELECT</code> command's output list13   (see <a class="xref" href="queries-select-lists.html" title="7.3. Select Lists">Section 7.3</a>).  It can contain column14   names of the command's target table, or value expressions using those15   columns.  A common shorthand is <code class="literal">RETURNING *</code>, which selects16   all columns of the target table in order.17  </p><p>18   In an <code class="command">INSERT</code>, the data available to <code class="literal">RETURNING</code> is19   the row as it was inserted.  This is not so useful in trivial inserts,20   since it would just repeat the data provided by the client.  But it can21   be very handy when relying on computed default values.  For example,22   when using a <a class="link" href="datatype-numeric.html#DATATYPE-SERIAL" title="8.1.4. Serial Types"><code class="type">serial</code></a>23   column to provide unique identifiers, <code class="literal">RETURNING</code> can return24   the ID assigned to a new row:25</p><pre class="programlisting">26CREATE TABLE users (firstname text, lastname text, id serial primary key);27 28INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING id;29</pre><p>30   The <code class="literal">RETURNING</code> clause is also very useful31   with <code class="literal">INSERT ... SELECT</code>.32  </p><p>33   In an <code class="command">UPDATE</code>, the data available to <code class="literal">RETURNING</code> is34   the new content of the modified row.  For example:35</p><pre class="programlisting">36UPDATE products SET price = price * 1.1037  WHERE price &lt;= 99.9938  RETURNING name, price AS new_price;39</pre><p>40  </p><p>41   In a <code class="command">DELETE</code>, the data available to <code class="literal">RETURNING</code> is42   the content of the deleted row.  For example:43</p><pre class="programlisting">44DELETE FROM products45  WHERE obsoletion_date = 'today'46  RETURNING *;47</pre><p>48  </p><p>49   If there are triggers (<a class="xref" href="triggers.html" title="Chapter 39. Triggers">Chapter 39</a>) on the target table,50   the data available to <code class="literal">RETURNING</code> is the row as modified by51   the triggers.  Thus, inspecting columns computed by triggers is another52   common use-case for <code class="literal">RETURNING</code>.53  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dml-delete.html" title="6.3. Deleting Data">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="dml.html" title="Chapter 6. Data Manipulation">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries.html" title="Chapter 7. Queries">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6.3. Deleting Data </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Chapter 7. Queries</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai