RE: Number of rows from SELECT (was Oracle error handling)
From: Srikanth (Srikanth.Madani_at_vodafone.com)
Date: 02/24/05
- Next message: Michael A Chase tech: "Re: perl oracle connection not working from browser"
- Previous message: Hardy Merrill: "RE: Oracle error handling"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 24 Feb 2005 15:01:54 +0100 To: <dbi-users@perl.org>, "susan lam" <susanzlam@yahoo.com>
susan lam wrote on Donnerstag, 24. Februar 2005 14:09
> $sql = qq {select * from mytable};
> $sth = $dbh->prepare($sql);
> $sth->execute;
> $dbh->disconnect;
> Also, if mytable does exist, how can I obtain the row
> count returned from the above query and assign it to a
> variable?
>From the docs: <http://cpan.uwinnipeg.ca/htdocs/DBI/DBI.html>
$rv = $sth->rows;Returns the number of rows affected by the last row
affecting command, or -1 if the number of rows is not known or not
available.
Generally, you can only rely on a row count after a non-SELECT execute
(for some specific operations like UPDATE and DELETE), or after fetching
all the rows of a SELECT statement.
For SELECT statements, it is generally not possible to know how many
rows will be returned except by fetching them all. Some drivers will
return the number of rows the application has fetched so far, but others
may return -1 until all rows have been fetched. So use of the rows
method or $DBI::rows with SELECT statements is not recommended.
One alternative method to get a row count for a SELECT is to execute a
``SELECT COUNT(*) FROM ...'' SQL statement with the same ``...'' as your
query and then fetch the row count from that.
Cheers,
Srikanth Madani
All vulgarity is due to the incapacity to resist a stimulus.
-Nietzsche/Twilight of the Idols
- Next message: Michael A Chase tech: "Re: perl oracle connection not working from browser"
- Previous message: Hardy Merrill: "RE: Oracle error handling"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|