Application Development Guide

Fetching Results in Perl

Because the Perl DBI Module only supports dynamic SQL, you do not use host variables in your Perl DB2 applications. To return results from an SQL query, perform the following steps:

Step  1.

Create a database handle, as described in Connecting to a Database Using Perl.

Step  2.

Create a statement handle from the database handle. For example, you can call prepare with an SQL statement as a string argument to return statement handle $sth from the database handle, as demonstrated in the following Perl statement:

   my $sth = $dbhandle->prepare( 
      'SELECT firstnme, lastname 
         FROM employee '
      );

Step  3.

Execute the SQL statement by calling execute on the statement handle. A successful call to execute associates a result set with the statement handle. For example, you can execute the statement prepared in the previous example using the following Perl statement:

   #Note: $rc represents the return code for the execute call
   my $rc = $sth->execute();

Step  4.

Fetch a row from the result set associated with the statement handle with a call to fetchrow(). The Perl DBI returns a row as an array with one value per column. For example, you can return all of the rows from the statement handle in the previous example using the following Perl statement:

   while (($firstnme, $lastname) = $sth->fetchrow()) {
      print "$firstnme $lastname\n";
   }


[ Top of Page | Previous Page | Next Page ]