insert into Storage_TousseInstance (tousseInstanceId, storageLocationId) select tousseInstanceId, storageLocationId from StorageRecord where status = '存入' and not exists (select st.storageLocationId from Storage_TousseInstance st where st.storageLocationId = StorageRecord.storageLocationId);
select tousseInstanceId, max(id) storageRecordId into MaxTousseInStorageLocationId from StorageRecord group by tousseInstanceId;
update TousseInstance set lastStorageLocationId = (select sr.storageLocationId from MaxTousseInStorageLocationId mis join StorageRecord sr on sr.id = mis.storageRecordId where mis.tousseInstanceId = TousseInstance.id) where exists (select 1 from StorageRecord sr where sr.tousseInstanceId = TousseInstance.id) and lastStorageLocationId is null;
drop table MaxTousseInStorageLocationId;