my $old_data = $dbh->selectrow_hashref("select * from table_to_change where primary_key_id = ?", $table_row_id); ## update the table $dbh->do("INSERT INTO d_table table_name = ?, operation = 'insert'", $table_name); my $d_table_id = $dbh->selectrow_array("select last_insert_id()"); foreach my $f (@updated_field_names) { $dbh->do("INSERT INTO d_field (d_table_id, field_name, old_value) VALUES (?, ?, ?)", $d_table_id, $f, $old_data->{$f}); }