Skip to content

How to track already read records from DB?

I am working on writing a service, which will retrieve records (account details) from DB, get account ids from those, and with account ids call a rest API.
This rest API accepts only 100 account ids.

Say I read the first 100 records from DB, call the rest API and did the processing. Now when getting the next 100 from DB how can I make sure I am not reading from one of the last 100 I already used. There are 1 million records in the DB.

Thank you

Answer

Sort your database query on a key field and use the TOP 100 statement, like:
SELECT TOP 100 * FROM …. ORDER BY [keyfield]
remember the last key field value, an use this for your next database query, like:
SELECT TOP 100 * FROM…. ORDER BY [keyfield] WHERE [keyfield] > last key field value.
an so on..