insert into TousseDefinitionNote (note, tousseDefinitionId) select note, id from TousseDefinition where forDisplay = 1 and len(note)>0 and id not in (select tousseDefinitionId from TousseDefinitionNote); update TousseDefinition set note_id = (select id from TousseDefinitionNote where tousseDefinitionId = TousseDefinition.id) where note_id is null; update UseRecord set departCodeOfpatient = (select top 1 orgUnitCoding from OrgUnit where name = UseRecord.ascriptionDepartment) where (departCodeOfpatient is null or departCodeOfpatient = '') and ascriptionDepartment is not null and ascriptionDepartment <> ''; update UseRecord set departCodeOfpatient = (select top 1 orgUnitCoding from OrgUnit where orgUnitCoding = UseRecord.ascriptionDepartment) where (departCodeOfpatient is null or departCodeOfpatient = '') and ascriptionDepartment is not null and ascriptionDepartment <> '';