Perl DBI with mysql: how to get return value from a stored procedure?
Does DBD::mysql implement the bind_param_inout method? I am getting the following error messages when trying it out:
开发者_Python百科DBD::mysql::st bind_param_inout failed: Output parameters not implemented [for Statement "call spCreateTransactionRecord(?, ?)" with ParamValues: 0=Null!, 1=Null!] at ./db.pl line 23
My code:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use DBI qw(:sql_types);
use Data::Dumper;
my ($dbh, $dsn, $sth, $sql);
my ($RecID, TypeID);
my ($user, $pass) = '';
# Open DB connection
$dsn = "dbi:mysql:database=mp;mysql_read_default_file=$ENV{HOME}/.my.cnf";
$dbh = DBI->connect($dsn, $user, $pass,
{RaiseError=>1, AutoCommit=>0, ShowErrorStatement=>1})
|| die "DB open error: $DBI::errstr";
# Call stored procedure
$sql = "call spCreateTransactionRecord(?, ?)";
$sth = $dbh->prepare($sql);
$sth->bind_param_inout(2, \$p_RecID, 11, {TYPE=>SQL_INTEGER});
$sth->execute($p_TypeID) || print $sth->errstr;
# Disconnects
$dbh->commit();
$dbh->disconnect;
The stored procedures is declared as:
CREATE PROCEDURE spCreateTransactionRecord (
IN p_TypeID INTEGER,
OUT p_RecID INTEGER
)
The new code with a workaround:
# Call stored procedure
$sql = "call spCreateTransactionRecord($p_TypeID, \@rtnVal)";
$dbh->do($sql);
$p_RecID = $dbh->selectrow_array('SELECT @rtnVal');
print "Received RecID = $p_RecID\n";
Not as proper (two database calls instead of one) but does the job.
It is a known bug with "Verified" status, meaning it never got addressed.
http://bugs.mysql.com/bug.php?id=23554
That bug reports also contains a possible workaround.
A separate confirmation that the issue is still not addressed is that the source code for the current (4.017) version still has the error:
if (is_inout)
{
do_error(sth, JW_ERR_NOT_IMPLEMENTED, "Output parameters not implemented", NULL);
return FALSE;
}
精彩评论