Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dbi module and stored procedures

Status
Not open for further replies.

wimvanherp

Programmer
Mar 3, 2001
149
BE
I work with DBI and ODBC . It's easy to work with sql scripts in perl, but i don't manage to get to work with stored procedures. I do not find how I can get the result back from a stored procedure into perl .
I can start the procedure with a sql script like : exec procedure arg1 arg2. but the problem is how to get the result back

Wim Vanherp
Wim.Vanherp@belgacom.net
 
try something like this:

$sth = $dbh->prepare( "BEGIN my.packagw.proc(?,?,?);END;" );
$sth->bind_param( 1, $ARGV[0]);
$sth->bind_param( 2, $ARGV[1]);
$sth->bind_param_inout( 3, \$ret_val, 50 );

using an in_out parameter
 
Below is my wrapper around DBI, which will work when I use CSV files, MySQL and currently ODBC to MS SQL Server 7. I've taken out my error checking and the like, to make it more readable, and you simply let DBI handle all the returning for you.

sub GetQuery {
my ($dbi,$type,$sql,@args) = @_;
# prepare the sql statement for executing
my $sth = $dbi->prepare($sql);

# execute the SQL using any values sent to the function
# to be placed in the sql
$sth->execute(@args)) {

my @result;
# grab the data in the right way
if ( $type eq 'array' ) {
while ( my $row = $sth->fetchrow_arrayref() ) {
push @result, [@{$row}];
}
} else {
while ( my $row = $sth->fetchrow_hashref() ) {
push @result, $row;
}
}

# finish with our statement handle
$sth->finish;
# return the found datastructure
return @result;
}

Barbie
Leader of Birmingham Perl Mongers
 
missbarbell, how do you find that this affects performance?

Thanks,

fish
 
homefish: Affect performance between what? If you meant between CSV, MySQL and MS SQL Server, it all depends where your DB server is located. The latter in my case is remote and the others are local, so its not a true representation of perfomance. However, all three are not too dissimilar in performance, depending upon your application.

Barbie
Leader of Birmingham Perl Mongers
 
barbie,edog1


thanks for your reply, but i do not see how i can get the result back from a stored procedure when there is no parameter passed to this procedure. f.i. there is a stored procedure on the database itself that does need no parameters but returns an integer. the procedure is called :pr_dosomething and the way to access it through sql is : " exec pr_dosomething" . But how can I get retreive the return value ?

Wim Vanherp
Wim.Vanherp@belgacom.net
 
my $row = $sth->fetchrow_arrayref();
my $value_from_stored_procedure = $row->[0]->[0];

If you're unsure of values in a hash, array or object, it's a good idea to have something like the following:

use Data::Dumper qw(DumperX);
print STDERR DumperX($row);

so you can see what's there.

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top