So I have the following snippet code:
my $sql = "INSERT INTO mytbl (first_name, last_name, birthdate) VALUES (?, ?, ?)";
my $sth = $dbh->prepare($sql) or die "Error:".$dbh->errstr;
$sth->bind_param(1, $fname);
$sth->bind_param(2, $lname);
$sth->bind_param(3, $bdate);
$sth->execute() or die "Execution failed: " . $dbh->errstr;
$sth->finish();
When performing insertion in Microsoft SQL with the following data for birthdate, $bdate = "2010-01-06"; I have to cast it into the datetime function with SQL,
$bdate = "cast ('$eml_form_end_dt' as datetime)";
This, however, caused problem as perl doesn't recognize it. What I had to do instead is:
$bdate = "cast ('$eml_form_end_dt' as datetime)";
my $sql = "INSERT INTO mytbl (first_name, last_name, birthdate VALUES (?, ?, '$bdate')";
my $sth = $dbh->prepare($sql) or die "Error:".$dbh->errstr;
$sth->bind_param(1, $fname);
$sth->bind_param(2, $lname);
# This causes problem
# $sth->bind_param(3, "cast ('$eml_form_end_dt' as datetime)");
$sth->execute() or die "Execution failed: " . $dbh->errstr;
$sth->finish();
How do I escape the SQL function in this case?
EDIT: this is the error message I received: DBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server Driver]Invalid precisi on value (SQL-HY104) at filename.pl.