patternsqlMinor
postgres - copy values from one column to another column (same table)
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.
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);
ERROR: more than one row returned by a subquery used as an expressionthat 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?
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
UPDATE tab01
SET column2 = column1I 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 = column1Context
StackExchange Database Administrators Q#323744, answer score: 5
Revisions (0)
No revisions yet.