Solved

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

717 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