1

Please help with Perl script to fetch stored procedure from MySQL database. The following Perl Code Snippet is supposed to fetch 2 result sets in accordance with the result of the following MySQL stored procedure execution.

Is there a "next()" method for DBI fetchrow() viz.,

while(my $row = $sth->fetchrow()) {
    say($row);
    $sth->fetchrow(next);
}

Perl:

use feature ':5.22';
use strict;
use warnings;
use DBI;
my $dsn = "DBI:mysql:procdb";
my $username = "root";
my $password = '';
my $dbh = DBI->connect($dsn, $username, $password);
die "failed to connect to MySQL database:DBI->errstr()" unless($dbh);
say "Connected to MySQL Server successfully."; 
my $sth = $dbh->prepare("CALL prepend('abcdefg', \@inOutParam);") or die "prepare statement failed: $dbh->errstr()";
$sth->execute() or die "execution failed: $dbh->errstr()"; 
$sth->dump_results();
$sth->finish();
$dbh->disconnect();

Perl Output:

Connected to MySQL Server successfully.
'abcdefg'
1 rows

0 rows

MySQL:

DROP DATABASE IF EXISTS procdb;
DELIMITER $$
CREATE DATABASE procdb;$$
DELIMITER ;
USE procdb;
DROP PROCEDURE IF EXISTS prepend;
DELIMITER $$
CREATE PROCEDURE prepend
(
 IN inParam VARCHAR(255), 
 INOUT inOutParam INT
)
BEGIN
 DECLARE z INT;
 SET z = inOutParam + 1;
 SET inOutParam = z;
 SELECT inParam;
 SELECT CONCAT('zyxw', inParam);
END;$$
DELIMITER ;
USE procdb;
CALL prepend('abcdefg', @inOutParam);

MySQL Output:

abcdefg
zyxwabcdefg

Thanks

Impavid
  • 435
  • 1
  • 6
  • 17
  • Interestingly, this is the third question in the last two or three weeks that asks pretty much this. They just all were for different RDBMS. I'll find you the answer and if it's what you need we can close as duplicate. – simbabque Feb 05 '17 at 17:54
  • Possible duplicate: http://stackoverflow.com/a/41441486/1331451 – simbabque Feb 05 '17 at 17:56
  • 2
    @simbabque If you want to close as a dup, can you generalize that answer? In DBD::mysql, it's just [`$sth->more_results`](https://metacpan.org/pod/DBD::mysql#MULTIPLE-RESULT-SETS), not `$sth->{odbc_more_results}`. If we close this as a dup of that, I think it will be confusing, especially for anonymous users, who are redirected immediately...imagine seeing something about fetching multiple results from a MySQL stored proc in the Google blurb and then being sent to a page that says "use `odbc_more_results`". (Nice answer, btw) – ThisSuitIsBlackNot Feb 05 '17 at 18:15
  • @ThisSuitIsBlackNot yeah that makes sense – simbabque Feb 05 '17 at 19:41

0 Answers0