Solved

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

Posted on 2007-11-21
3
2,550 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
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

16 Experts available now in Live!

Get 1:1 Help Now