update PackingRecord set tousseDefinitionId=( select min(ti.tousseDefinition_id) from TousseInstance ti where ti.packingRecord_id=PackingRecord.id group by ti.packingRecord_id ) where tousseDefinitionId is null and tousseType<>'外来器械包'; update PackingRecord set tousseDefinitionId=( select case when min(td.tousseType)='外来器械拆分小包' then min(td.parentID) else min(td.id) end from TousseInstance ti join TousseDefinition td on td.id=ti.tousseDefinition_id where ti.packingRecord_id=PackingRecord.id group by ti.packingRecord_id ) where tousseDefinitionId is null and tousseType='外来器械包';