I am new to MS SQL. I have 2 tables, one has Data and other keeps track of records that were processed (log Table). I want to fetch records that are new i.e not present in log Table and those records that were modified after the last fetch.
I store fetched Date, update Date and Primary Key of the Record from Data Table in my Log Table.
Currently I am doing this by using UNION. 1st query fetches new records a开发者_开发技巧nd second fetched modified record. Is this the right way to do it or else is there a better way for it.
Thanks in advance.
I think a UNION
is actually the best way to get this data, specially if you can use indexes on each select statement
Try something like this:
select id, updatedDate
from YourDataTable
except
select id, updatedDate
from YourLogTable
What EXCEPT does it return the rows that aren't in both queries. INTERSECT does the opposite.
You could also utilize MERGE, but in my opinion your easiest bet is through EXCEPT
.
精彩评论