Our CRM has an API that allows us to run SQL queries but limits the results to 500 records. We need to get every row from certain tables that have more than 500 records.
My workaround so far for some tables has been to query the lowest primary key, the highest primary key, then run a query for 500 records at a time by primary key, and to keep repeating and adding each result set until we've reached the highest primary key (and then add all of those tables together). But some tables have no primary key. Is there a way to pull it 500 records at a time until the whole table has been pulled? This is SQL Server.
lowestPKQuery = "SELECT TOP 1 " + pkColumn + " FROM " + tableToDump + "ORDER BY " + pkColumn + " ASC";
highestPKQuery = "SELECT TOP 1 " + pkColumn + " FROM " + tableToDump + " ORDER BY " + pkColumn + " DESC";
initialRecordCountQuery = "SELECT count(" + pkColumn + ") FROM " + tableToDump;
// then, populate a dictionary that covers every range by intervals of 500
query = "SELECT " + columnsToSelect + " FROM " + tableToDump + " WHERE " + pkColumn + " BETWEEN " + range.Key + " AND " + range.Value + " ORDER BY " + pkColumn + " ASC";
// after all the tables have been pulled, then add all of those tables together
Is there a way just using SQL to eventually pull the entire table, 500 records at a time, when there is no primary key? It's basically a rest API that allows us to run almost any SQL statement (besides record deletion or table creation) but no matter what query, it will only return 500 records (in an XML document).