sqlserver Show only new IDs and then update table so that next time query is run they won't show

sqlserver Show only new IDs and then update table so that next time query is run they won't show

Hi as the title suggest I would like to run a query that only shows New ID's.

Here is some test data

create table #tmpmerchant (Com_id int not null)

insert #tmpmerchant values ('1234')
insert #tmpmerchant values ('23456')
insert #tmpmerchant values ('74895')
insert #tmpmerchant values ('9578')
insert #tmpmerchant values ('9579')
insert #tmpmerchant values ('444889')

So the first time this runs it will return all records.

I then want to store these IDs in a permanent table.

When the query is next run it will only show new IDs that are not in the permanent table.

insert #tmpmerchant values ('1234')
insert #tmpmerchant values ('23456')
insert #tmpmerchant values ('74895')
insert #tmpmerchant values ('9578')
insert #tmpmerchant values ('9579')
insert #tmpmerchant values ('444889')
insert #tmpmerchant values ('54789')

So in this example '54789' will only show.

The permanent table is then updated with the '54789'

What would be the best way to achieve this?
select * from PermanentTable P
where not exists (select 1 from #tmpMerchant M where M.Com_Id = P.Com_ID);

In SQL Server 2008+ to add new rows it will be

[1] [2] Next

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright