Basically the way our app has been designed is that rather than allowing the db to decide the next primary key for each table these are stored in a seperate table containing the table name and key (I know...not my idea but something I'm stuck with!!

).
Sometimes these get out of sync so when the application tries to insert a new record it does so with an invalid primary key. What I need to do is loop through each of the records on the table and check the actual primary key value (using dynamic sql) to see where the 2 are out of sync. Unfortunately I don't have the option of changing how the application does this but what I just need to run this on an adhoc basis to see where there might be some problems (hope this makes sense)