I don't think this is fully doable in SQL without creating the problematic temp table. Doing this in memory should be much faster. Beware if you go along the temporary route of the table, so that you choose a unique name for your table for each function call, to avoid a race condition when your code runs twice at the same time and manages two rows of data in one temporary table.
I do not know which language you are using, but it should be possible to get a list of fields in your program. I would do it like this:
array_of_field_names = conn->get_field__list; array_of_row_values = conn->execute ("SELECT... "); array_of_row_values ["ID"] = new_id_value insert_query_string = "construct insert query string from list of field names and values"; conn->execute (insert_query_string);
Then you can encapsulate this as a function and simply call it by specifying a table, an old identifier and a new identifier, and this will work the magic.
In Perl code, the following snippet will do:
$table_name = "MYTABLE"; $field_name = "ID"; $existing_field_value = "100"; $new_field_value = "101"; my $q = $dbh->prepare ("SELECT * FROM $table_name WHERE $field_name=?"); $q->execute ($existing_field_value); my $rowdata = $q->fetchrow_hashref;
Hope this helps.
Kristoffon
source share