in reply to Best way to check which field(s) was changed
Personally, I wouldn't bother with having a "most accurate" SQL, as most databases need to rewrite the whole row anyway.
my $dbquery eq "UPDATE table_name SET column_name=$inputUpdate[0] WHERE column_name=$input[1]";doesn't do what you think it does, so it might help if you showed us the actual error message that Perl tells you.
If you're bent on providing a "most accurate" SQL statement, most likely the following code can give you such a statement:
use strict; my @columns = qw(name title state); my @input = qw(foo bar baz); my @inputUpdate = qw(boing bar boo); my $table_name = 'mytable'; my @new_value_clause = map { "$columns[$_] = ?" } grep { $input[$_] ne + $inputUpdate[$_] } 0..$#inputUpdate; my @where_clause = map { "$columns[$_] = ?" } 0..$#input; my $sql = join "\n", "UPDATE $table_name", "SET " . join ",", @new_value_clause, "WHERE " . join " and ", @where_clause; my $sth = $dbh->prepare($sql); $dbh->execute(@inputUpdate, @input);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Best way to check which field(s) was changed
by josh097 (Initiate) on Dec 29, 2008 at 18:13 UTC |