To be more precise about the "both" comment
GrandFather made, the Perl error is caused by the error DBD::ODBC is reporting.
The prepare fails, and that failure returns undef. The execute can't be performed on the statement handle because the statement handle is undefined.
DBD::ODBC is, in fact, reporting that there is a syntax error in the INSERT INTO statement and that it got a failure back fro the database server for that reason. Either the syntax of $FinalInsertSQL is wrong in general or one of the variables concatenated to form it is wrong, undefined, or empty.
Getting your debugging on is a good idea. The database server doesn't appear to give very helpful error hints other than the fact that it is a syntax error in the INSERT statement.