1

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 

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.