
                        with tmpPerson as (
                            select
                                l.frlPerson_id,
                                ps.Person_id
                            from
                                stg.frlPerson l
								inner join v_PersonState ps on upper(rtrim(REPLACE(ps.Person_SurName,'ё','е'))) = upper(rtrim(ltrim(REPLACE(l.fam,'ё','е'))))
								    and upper(rtrim(REPLACE(ps.Person_FirName,'ё','е'))) = upper(rtrim(ltrim(REPLACE(l.im,'ё','е'))))
								    and (upper(rtrim(REPLACE(ps.Person_SecName,'ё','е'))) = upper(rtrim(ltrim(REPLACE(l.ot,'ё','е'))))
								        or (nullif(replace(ps.Person_SecName,' ',''),'---') is null and nullif(rtrim(ltrim(l.ot)),'') is null))
								    and ps.Person_BirthDay = l.Person_BirthDay and ps.Person_Snils = l.Person_Snils
							where
								l.person_id is null
								and l.identCount = 1
								and l.identVariant = 1
								and l.RegisterListLog_id = '520101000000003'
						)

						update
							stg.frlPerson
						set
							Person_id = ps.Person_id
						from
							tmpPerson ps
						where
							frlPerson.frlPerson_id = ps.frlPerson_id
							and frlPerson.RegisterListLog_id = '520101000000003';
					
