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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl - Oracle row count

Status
Not open for further replies.

uuperl

Programmer
Feb 20, 2006
33
US
All,

I have problem getting rows count from Oracle DB:

$sth = $dbh->prepare("select * from tbl")
or die "Can not execute: $DBI::errstr";
$sth->execute();
$total_rec = $sth->rows;

Some how the $total_rec is always zero even there are many records. I have read some other people posting, this is not working for Oracle. Is there any other solution?

thx.
 
Do a select count in oracle? Or maybe push all the results in to an array and see how many entries in the array you get?
 
From the DBI docs:
Code:
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.

So, either iterate through your resultset and count the rows, or use the DB itself to return the count (SELECT COUNT(*) FROM ... )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top