代码之家  ›  专栏  ›  技术社区  ›  emx

Perl DBI with mysql:如何从存储过程获取返回值?

  •  1
  • emx  · 技术社区  · 15 年前

    实施 绑定参数输入输出 方法? 我在试用时收到以下错误消息:

    DBD::mysql::st bind\u param\u inout 实现了[for语句“call spCreateTransactionRecord(?)?, ?)" 具有 ParamValues:0=空!,1=空!]在 ./配电盘第23行

    我的代码:

    #!/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;
    

    CREATE PROCEDURE spCreateTransactionRecord (
        IN  p_TypeID INTEGER,
        OUT p_RecID  INTEGER
    )
    
    2 回复  |  直到 10 年前
        1
  •  2
  •   DVK    15 年前

    http://bugs.mysql.com/bug.php?id=23554

    这个bug报告还包含一个可能的解决方法。

    the source code for the current (4.017) version 仍然有错误:

    if (is_inout)
    {
       do_error(sth, JW_ERR_NOT_IMPLEMENTED, "Output parameters not implemented", NULL);
       return FALSE;
    }
    
        2
  •  4
  •   emx    15 年前

    新代码有一个解决方法:

    # 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";
    

    不合适(两个数据库调用而不是一个数据库调用),但执行该任务。