Solved

Sybase returning row numbers

Posted on 2001-07-08
6
3,007 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
The recent Petya-like ransomware attack served a big blow to hundreds of banks, corporations and government offices The Acronis blog takes a closer look at this damaging worm to see what’s behind it – and offers up tips on how you can safeguard your…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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