Created
October 16, 2014 17:21
-
-
Save prestonp/0b5cd2416b4b3853db3f to your computer and use it in GitHub Desktop.
postgres update return new values and old values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- returning updated values and old values by using a sub-expression | |
update orders | |
set type = 'delivery' | |
where id = 3767 | |
returning id, type, ( | |
select type from orders where id = 3767 | |
) as old_type; |
I just asked CHATGPT and it showed the same way as the gist showes, also it's okay to use with clause to make temp table to store the old values or use triggers with OLD keyword , but I think the subquery one is the most convenient way.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How does this make it ?