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

    Well

    my $dbquery eq "UPDATE table_name SET column_name=$inputUpdate[0] WHERE column_name=$input[1]";

    was just an example. So there is no error message to show you.

    I wanted to find all of the the fields that were changed and put them in the 'SET' part of the query and all of the ones that didn't change in the 'WHERE' part.

    That is what I meant by 'most accurate'. As opposed to putting just one field in the 'WHERE' section (unless all other fields where changed) just in case there where more than one row in that column with the same entry.

    I don't know if you understand what I mean. I do appreciate your help though.