UPDATE I
SET Business_Migration_Ref = T.rn
FROM
InitialData I INNER JOIN
(SELECT DISTINCT
OrgNm, AddrLn1, AddrLn2, AddrLn3, AddrLn4, AddrPostCd
,rn = dense_rank() over(order by OrgNm, AddrLn1, AddrLn2, AddrLn3, AddrLn4, AddrPostCd)
FROM InitialData
WHERE OrgNm IS NOT NULL AND LEN(OrgNm) > 0 AND (Business_Migration_Ref IS NULL)
) T ON (I.OrgNm=T.OrgNm OR (I.OrgNm IS NULL AND T.OrgNm IS NULL)) AND
(I.AddrLn1=T.AddrLn1 OR (I.AddrLn1 IS NULL AND T.AddrLn1 IS NULL)) AND
(I.AddrLn2=T.AddrLn2 OR (I.AddrLn2 IS NULL AND T.AddrLn2 IS NULL)) AND
(I.AddrLn3=T.AddrLn3 OR (I.AddrLn3 IS NULL AND T.AddrLn3 IS NULL)) AND
(I.AddrLn4=T.AddrLn4 OR (I.AddrLn4 IS NULL AND T.AddrLn4 IS NULL)) AND
(I.AddrPostCd=T.AddrPostCd OR (I.AddrPostCd IS NULL AND T.AddrPostCd IS NULL))
|