I have a table named customers
, that has many columns: 'id', 'name', 'age', 'weight', 'hight', 'blood', 'sex', 'last name', 'datetime'
.
I want to add a checking logic before inserting, which is if the datetime already exists, then update/overwrite the whole existing row.
My code:
INSERT INTO customers (id,name,age,weight,hight,blood,sex,last name,datetime)
VALUES('1','2','3','4','5','6','7','8','2022-12-30')
ON CONFLICT (datetime)
DO
UPDATE SET name = EXCLUDED.name || ';' || customers.name;
....
UPDATE SET name = EXCLUDED.datetime || ';' || customers.datetime;
Because the table has many columns, and I want to update/overwrite the entire row if the datetime
is already exists, so is there any easier way to do this instead of UPDATE SET for each column?
2
Answers
Use merge
Good examples here
https://www.sqlshack.com/understanding-the-sql-merge-statement/
Your target table will be the
Customers
table, and you will need to create a virtual table with the record you want to insert/update, let’s call itRecord
Solution not complete, you need to put your values into a virtual table and update the insert and the update statements, but you get the idea.
You can do this…
But it’s a bad idea for a number of reasons.
Assuming this is to record changes for auditing later, use an audit table to store the old values. This can be a specific table which mirrors the original column by column, but it’s easier to use a single table and JSON.
Then you can have a trigger write to the audit table on every change.
event
can be used to record whether it was an insert, update, or delete. You can also record what user made the change.