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
Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Update query with connected table data 3 38
SSIS On fail action 5 38
Nested Case statement 4 37
SQL - Curser to do an insert based on a select 2 10
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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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