Reputation: 63
I have this situation I'm doing a update statement on sql script in postgres, when I found matching record I want to update content and also save to other table for example id_user for who's I did update is it possible with use plpgsql. Can you give me any tips what terms I will should search on google ?
ex.
update User set Status "Active" where any_condition when success insert to UserHistoryTable
Upvotes: 1
Views: 37
Reputation: 19613
Use a normal update with a RETURNING *
inside a CTE, and from there insert the data in the history table:
WITH j AS (
UPDATE tbuser SET status = 'Active'
WHERE id = 42
RETURNING *)
INSERT INTO user_hist SELECT * FROM j;
Demo: db<>fiddle
If you want to save the value before the update I suggest you take a look at triggers
.
Upvotes: 1