views:

49

answers:

1

Hi,

I need to have a way to iterate through a database table without actually storing it in memory anywhere. I want to essentially read through the rows like an input iterator.

I've tried using cursors with a select statement (select * from table_name), but this retrieves the entire table and feeds it back to be one row at a time. So this solution is no good. Instead, I need it to only feed me each row as I ask for it.

Any suggestions are greatly appreciated.

Thanks!

A: 

You'll just want to use a forward only cursor. Your DB will need to support this. For detials, see MSDN's How to: Use Cursors.

If you're using SQL Server, you can use a Fast Forward-Only Cursor, which provides extra benefits.

Reed Copsey
Thank you for your help. However I have already looked into Cursors and they are not what I'm looking for. They save the data before iterating through it. Thus it takes a long time with a large database to execute a 'select' query. I want to just be able fetch the data row by row without having it load into memory first. I want to read it straight from the database to ensure it is fast. Right now it is very slow and sometimes crashes if the database is too large
bumper
Forward only cursors don't "save the data" before iterating through it - they save a small cache of a few records, but automatically delete it as you move. THere is no way (in ODBC) to directly load row-by-row. You'll need to use native API for your DB if you want to do that. Forward only cursors (and FF-Only for SQL Server) are the "lightweight" way of accessing data in ODBC.
Reed Copsey