简体   繁体   English

如何使用Perl DBI检索DB2 SQL sproc的返回值?

[英]How do you retrieve the return value of a DB2 SQL sproc using Perl DBI?

I need to retrieve the value returned by a DB2 sproc that I have written. 我需要检索由我编写的DB2 sproc返回的值。 The sproc returns the number of rows in a table and is used by the calling process to decide whether or not to update other data. sproc返回表中的行数,并由调用过程用来确定是否更新其他数据。

I have looked at several similar questions on SO but they refer to the use of out parameters instead of using the sproc's return value, for example: 我已经看过关于SO的几个类似问题,但是它们涉及使用out参数而不是使用sproc的返回值,例如:

Perl Dbi and stored procedures Perl Dbi和存储过程

I am using a standard DBI connection to the database with both RaiseError and PrintError enabled. 我在启用RaiseError和PrintError的情况下使用到数据库的标准DBI连接。

$sql_stmt = "call MY_TABLE_SPACE.MY_SPROC('2011-10-31')";
    $sth = $dbh->prepare($sql_stmt)
         or die "Unable to prepare SQL '$sql_stmt': $rps_met_dbh->errstr";

    $rsp = 0;
    $rsp = $sth->execute();
    unless($rsp) {
        print(STDERR "Unable to execute sproc: $rps_met_dbh->errstr\n");
    }

    print(STDERR "$?\n");

I have tried looking at $h->err for both the statement handle and the db handle. 我试图在$ h-> err中查找语句句柄和数据库句柄。

I would really prefer communicating the number of rows via a return code rather than using SQLSTATE mechanism if I can. 如果可以的话,我真的更希望通过返回代码而不是使用SQLSTATE机制来传达行数。

Edit: 编辑:

I have finished up using a dedicated out parameter to communicate the number of rows updated as follows: 我已经完成使用专用的out参数来传达更新的行数,如下所示:

$sql_stmt = "call MY_TABLE_SPACE.MY_SPROC('2011-10-31')";
    $sth = $dbh->prepare($sql_stmt)
         or die "Unable to prepare SQL '$sql_stmt': $rps_met_dbh->errstr";
    $sth = $dbh->bind_param_inout(1, $rows_updated, 128)
         or die "Unable to prepare SQL '$sql_stmt': $rps_met_dbh->errstr";

    $rows_updated = 0;
    $rsp = 0;
    $rsp = $sth->execute();
    unless($rsp) {
        print(STDERR "Unable to execute sproc: $rps_met_dbh->errstr\n");
    }

    print(STDERR "$rows_updated\n");

Edit 2: 编辑2:

And now thinking about this further I have realised that I should apply the PragProg principle of "Tell. Don't Ask." 现在,我进一步思考了这一点,我意识到我应该应用PragProg的“告诉。不要问”的原则。 That is, I shouldn't call the sproc. 也就是说,我不应该调用该存储过程。 then have it give me back a number before I decide whether or not to call the anopther sproc, ie "Ask". 然后让它给我回一个号码,然后再决定是否调用该anopther sproc,即“询问”。

I should just call the first sproc. 我应该只调用第一个存储过程。 and have it decide whether it should call the other sproc or not, ie "Tell" and let it decide. 并让它决定是否应调用另一个存储过程,即“告诉”并让它决定。

What is wrong with using an output parameter in your procedure. 在过程中使用输出参数有什么问题。 I've not got a working DB2 lying around right now or I'd provide an example but when I was using it I'm sure you can define output parameters in procedures and bind them with bind_param_inout. 我现在还没有正在工作的DB2,或者我将提供一个示例,但是当我使用它时,我确定您可以在过程中定义输出参数并将其与bind_param_inout绑定。 I cannot remember if a DB2 procedure can return a value (like a function) but if it can them using "? = call MY_TABLE_SPACE.MY_SPROC('2011-10-31')" would allow you to bind the output return value. 我不记得DB2过程是否可以返回值(如函数),但是如果可以使用“?=调用MY_TABLE_SPACE.MY_SPROC('2011-10-31')”它们将允许您绑定输出返回值。 If this doesn't work you could use a DB2 function which definitely can return a value. 如果这不起作用,则可以使用DB2函数,该函数肯定可以返回值。 However, at the end of the day the way you get data out of a procedure/function is to bind output parameters - that is just the way it is. 但是,归根结底,从过程/函数中获取数据的方法是绑定输出参数-就是这样。

I've no idea what you mean by "using SQLSTATE". 我不知道“使用SQLSTATE”是什么意思。 I've also no idea what you mean by looking at $h->err as that is only set if the procedure fails or you cannot call the procedure (SQL error etc). 我也不知道通过查看$ h-> err是什么意思,因为只有在过程失败或您无法调用该过程(SQL错误等)时才设置该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM