Solved

ODBC in C++: Need row count on SELECT query

Posted on 2007-11-21
3
2,611 Views
Last Modified: 2008-02-01
When you prepare and execute a SQL statement, but before you fetch the results, is there any way to know how many rows there are in the result set?  I need to create an array with the same number of elements.

Here's the application flow:
Execute SQL statement.
Bind columns to vars.
                         <-- I need the row count here so I can make an array of n elements.
Fetch rows.

The ODBC API has a function called SQLRowCount(), but MSDN says it only returns the number of rows affected by insert, update, or delete queries.  It mentions nothing about select queries.  I know I can read the entire result set to get the count, reset the cursor, and then read it again, but that is just inefficient.

Any ideas?
0
Comment
Question by:cuziyq
[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 Comments
 
LVL 16

Accepted Solution

by:
SQL_SERVER_DBA earned 250 total points
ID: 20328624
select count(*) from table
0
 
LVL 14

Author Comment

by:cuziyq
ID: 20328758
DUH!  I am such an idiot!  Maybe I should think before I post next time ;-)
0
 
LVL 30

Expert Comment

by:Axter
ID: 20328904
>>I need to create an array with the same number of elements.

I recommend using a vector container, so that you don't need to know the number of elements needed for the array.

A std::vector container will grow to the size you need it, as you add elements to it.

It's also a safer approach over using new or malloc, since the vector will automatically clean up the memory.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

735 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