Skip to content
Advertisement

Best way to restoring Audit table without existing primary keys – postgres

Table A has an audit table that I need to restore a specific column from.

Rows were deleted from table A, I then regenerated them and via matching on specific times (these are appointment records) I’ve found the match between table A and it’s audit tables using the following SQL.

As you can see, I’m returning the meta and the affected row’s id. I now need to loop through and update each affected row and update the meta, but I’m struggling to write a query that will do that.

I’ve tried using the with clause (report_answers being the subquery described above), but I keep getting multiple rows returned error no matter how I write it. Any tips?

The update is to update the column ‘meta’ on table B Schema:

1 row in table B for each value in student_ids in table A.

Example would be

Advertisement

Answer

Based on your queries posted in your question, If I understood correctly you want to update the data generated by first query into table B. Assuming your first query is working fine then try below query:

Note: I don’t think join of Table a is required in update query. you can use it or remove it as per your requirement.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement