如何使用 Perl DBI 检索 DB2 SQL 存储过程的返回值?
我需要检索我编写的 DB2 存储过程返回的值。存储过程返回表中的行数,并由调用进程用来决定是否更新其他数据。
我已经看过几个类似的问题,但它们指的是使用 out 参数而不是使用 sproc 的返回值,例如:
我使用标准 DBI 连接到数据库,并启用了 RaiseError 和 PrintError。
$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");
我尝试查看 $h->err 来查找语句句柄和数据库句柄。
如果可以的话,我真的更喜欢通过返回代码来传达行数,而不是使用 SQLSTATE 机制。
编辑:
我已经完成使用专用输出参数来传达更新的行数,如下所示:
$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");
编辑2:
现在进一步思考这一点,我意识到我应该应用 PragProg 的“告诉,不要问”原则。也就是说,我不应该调用存储过程。然后让它在我决定是否调用另一个存储过程之前给我返回一个号码,即“询问”。
我应该只调用第一个存储过程。并让它决定是否应该调用另一个存储过程,即“告诉”并让它决定。
I need to retrieve the value returned by a DB2 sproc that I have written. 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.
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:
Perl Dbi and stored procedures
I am using a standard DBI connection to the database with both RaiseError and PrintError enabled.
$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.
I would really prefer communicating the number of rows via a return code rather than using SQLSTATE mechanism if I can.
Edit:
I have finished up using a dedicated out parameter to communicate the number of rows updated as follows:
$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:
And now thinking about this further I have realised that I should apply the PragProg principle of "Tell. Don't Ask." 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, i.e. "Ask".
I should just call the first sproc. and have it decide whether it should call the other sproc or not, i.e. "Tell" and let it decide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在程序中使用输出参数有什么问题。我现在还没有可用的 DB2,或者我会提供一个示例,但是当我使用它时,我确信您可以在过程中定义输出参数并使用 bind_param_inout 绑定它们。我不记得 DB2 过程是否可以返回值(如函数),但如果可以,则使用“? = call MY_TABLE_SPACE.MY_SPROC('2011-10-31')”将允许您绑定输出返回值。如果这不起作用,您可以使用肯定可以返回值的 DB2 函数。然而,归根结底,从过程/函数中获取数据的方式是绑定输出参数——事实就是如此。
我不知道“使用 SQLSTATE”是什么意思。我也不知道查看 $h->err 是什么意思,因为只有在过程失败或无法调用过程(SQL 错误等)时才设置它。
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. 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. If this doesn't work you could use a DB2 function which definitely can return a value. 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". 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).