<p>In SQL, how do update a table, setting a column to a different value for each row?</p>

<p>I want to update some rows in a PostgreSQL database, setting one column to a number from a sequence, where that column has a unique constraint. I hoped that I could just use:</p>

<pre><code>update person set unique_number = (select nextval('number_sequence') );
</code></pre>

<p>but it seems that <em>nextval</em> is only called once, so the update uses the same number for every row, and I get a 'duplicate key violates unique constraint' error. What should I do instead?</p>

Don't use a subselect, rather use the nextval function directly, like this:
```
```
update person set unique_number = nextval('number_sequence');
```
```