Solved

Sybase returning row numbers

Posted on 2001-07-08
6
2,969 Views
Last Modified: 2007-11-27
How can I get sybperl to give me the row count after executing an SQL Query (a "SELECT" query, for example).

If I query a database with ct_execute, I then call:

$count = $dbh -> ct_res_info(CS_NUMDATA)

...but $count seems to give me completely arbitrary values (i.e.: 15 for 10 rows, 58 for 713 rows, 180
for no results, etc..)

Am I missing something here?
0
Comment
Question by:Tonyboy
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 6263674
Well - it would: that's the number of columns in the result set, not the number of rows. Use CS_ROW_COUNT instead.


 $rows = $dbh->ct_res_info(CS_ROW_COUNT);
0
 

Author Comment

by:Tonyboy
ID: 6265650
Hmmm... I agree with your solution, but given the following Perl script (I hope you're familiar):

if ($restype == CS_ROW_RESULT) {
     $sql_rows = $dbh -> ct_res_info(CS_ROW_COUNT) || 1;
}

For some reason, though, CS_ROW_COUNT never returns any value, and so the 1 is used (if I put 2 there, the 2 would be used, etc...)
0
 
LVL 5

Accepted Solution

by:
amitpagarwal earned 200 total points
ID: 6265688
It's a protocol thing... Sybase doesn't know (or doesn't tell the APIs) how many rows are returned until all the rows have been fetched,   so you can't know in advance how many rows will be returned by a query    unless you do a select count(*) first.

 The CS_ROW_COUNT property is really usefull for updates/inserts/deletes,   though...

That being said, the CS_ROW_COUNT attribute is retrievable once

                          ct_results returns a $restype of CS_CMD_DONE, so you should
                          write a loop to get the number of rows affected, like this:

                          $dbh->ct_execute($query);
                          while($dbh->ct_results($restype) == CS_SUCCEED) {
                              if($restype == CS_CMD_DONE) {
                                 $numrows = $dbh->ct_res_info(CS_ROW_COUNT);
                              }
                              # do any other processing here
                              # for example, process any fetchable rows:
                              if($dbh->ct_fetchable($restype)) {
                                 while(@dat = $dbh->ct_fetch) {
                                    # do whatever you need to do with the data...
                                 }
                               }
                          }


                          Hope this helps a little bit...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Tonyboy
ID: 6265748
Oh... That's dissapointing..

Is there any way of finding out how many rows are returned before fetching all of the results?
0
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 6265754
listening ..
0
 

Author Comment

by:Tonyboy
ID: 6265798
???
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now