If you had turned on strict and/or warnings, you would see what your problem is.
You're writing
if (...) {
my $sql = ...;
} else {
my $sql = ...;
}
execute($sql);
Which means that the $sql
variables that you declare in the if
branches aren't in scope and you're trying to execute completely empty SQL.