SQLTeam.com | Weblogs | Forums

How to check if ID is in a Table


I have a stored procedure that runs a number of SQL statements. Each statement is design to find records based on IDs. So, for example after it goes through the first SQL statement it writes the records to a table. When it goes through the next SQL statement I need it to look at the table, that the first statement wrote, and only return records from this SQL statement that are not in the table based on an ID field.
My thought was to join to the table and say were the ID in SQl table is not in the Table.
But even if this works as the table grows it will take too long, as it goes through the SQL statement..
Any ideas I would appreciate it.


You can use JOINs or you can use NOT EXISTS construct. If the queries are taking longer than you like, appropriate indexes would usually help. In this case, an index on the ID might be useful. However, that is a VERY wild guess on my part, because without looking at the structure of your tables and the nature of your queries, it is hard to tell.


Thank you
That is what I thought I just want to see if i was missing something.
Thank you