HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

postgres - copy values from one column to another column (same table)

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
samepostgrescolumnoneanothervaluesfromtablecopy

Problem

I want to copy the entire content from one column to another (same datatype) and thought of a subselect to be part of the equasion.

UPDATE tab01 
  SET column2=(SELECT column1 FROM tab01);

ERROR:  more than one row returned by a subquery used as an expression


that error makes completely sense as the outpout of my subselect has many rows.

Is there a way to copy all values into the other coloumn, without creating any sort of loop around something like ...

UPDATE tab01 
  SET column2=(SELECT column1 FROM tab01 WHERE id=1)
  WHERE ID=1;

Solution

Why not just this?

UPDATE tab01 
SET column2 = column1


I believe the above should work. No need for a subquery, the field should be available of the same row which is being updated.

(Per your comments, you did not want to filter which rows get updated, so I've removed the WHERE clause.)

Code Snippets

UPDATE tab01 
SET column2 = column1

Context

StackExchange Database Administrators Q#323744, answer score: 5

Revisions (0)

No revisions yet.