http://qs321.pair.com?node_id=663977


in reply to Error while inserting null values with DBI

A couple of solutions.

1. Use placeholders:

my $sth = $dbh->prepare("insert test(id, name) values(?, ?)"); $sth->execute($id, $name);
If $id is undef then a correct NULL value will be passed to the data server (which from the error message would appear to be Sybase or MS-SQL).

An alternative solution:

$id = 'NULL' unless defined($id); $dbh->do("insert test(id, name) values($id, \"$name\")");
The latter solution is much inferior to the placeholder solution, and is incomplete and bug-prone (i.e. if $name is NULL instead of $id you need to handle this differently, not to mention quoting issues with strings...)

Michael