开发者

fetch new and modified record from a Table

开发者 https://www.devze.com 2023-04-10 18:54 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消