# update the bioRecordList, insert updated and remove vanished records
delete from bioRecordList
using removedRecords
join bioRecordList on removedRecords.brKey = bioRecordList.brKey;

insert ignore into bioRecordList
select brKey from updatedRecords;

# save the latest bioRecordModifiedDate
select @m := max(bioRecordModifiedDate)
from updatedRecords;

select @m_old := max_bioRecordModified
from lastUpdated;

# if no updateRecords in the to do list, preserve the old date
set @m = IfNull(@m, @m_old);

update lastUpdated
set lastRun = now(),
    max_bioRecordModified = If(@m > @m_old, @m, @m_old);

# Last, remove from and replace records in cormList
delete cormList
from cormList
left join corm_datasource using (serviceKey)
where corm_datasource.servicekey is null;

replace into cormList
select * from corm_datasource;