IF OBJECT_ID('tempdb..##MHBRIDGE') IS NOT NULL
    DROP TABLE ##MHBRIDGE SELECT * 
INTO ##MHBRIDGE 
FROM (
    SELECT MOBILEKEY, TAXDIST, MFG, MODEL, WIDTH, LENGTH, YEARBUILT, LEFT(REPLACE(LASTNAME,',',''),24) LASTNAME, CASE WHEN OVRVALUE > 0 THEN OVRVALUE WHEN GUIDE=1 THEN GUIDEVALUE ELSE VALUE END + ADDONVAL AS FMV 
    FROM       [wingap].AY2014.DBO.M_MOBILE M
    INNER JOIN [wingap].AY2014.DBO.OWNER O ON O.OWNKEY=M.OWNKEY 
    UNION
    SELECT MOBILEKEY, TAXDISTRIC, MFG, MODEL, WIDTH, LENGTH, YEARBUILT, LEFT(REPLACE(LASTNAME,',',''),24) LASTNAME, CASE WHEN OVRVALUE > 0 THEN OVRVALUE WHEN GUIDE=1 THEN GUIDEVALUE ELSE VALUE END + ADDONVAL AS FMV 
    FROM       [wingap].AY2014.DBO.M_MOBILE M
    INNER JOIN [wingap].AY2014.DBO.REPROP RP ON RP.REPROPKEY=M.REPROPKEY
    INNER JOIN [wingap].AY2014.DBO.REALPROP R ON R.REALKEY=RP.REALKEY
    INNER JOIN [wingap].AY2014.DBO.OWNER O ON O.OWNKEY=R.OWNKEY 
    ) X 

update mm set mm.MOBILEKEY=wg.mobilekey 
from dy2013.dbo.property p 
inner join dy2013.dbo.M_MOBILE mm on mm.MOBILEKEY=p.MOBILEKEY
inner join MCDUFFIE_GSI.dbo.MOBIL m on m.ACCOUNT_NO+m.LINE_NO=p.LEGACY_ACCT and m.TAX_YEAR=2013
inner join ##MHBRIDGE WG ON LEFT(WG.MFG,25)=m.MFG AND LEFT(WG.MODEL,25)=m.MODEL AND WG.YEARBUILT = m.YR_BUILT AND WG.TAXDIST=m.TAXDISTRIC AND m.FMV=WG.FMV AND WG.WIDTH=m.WIDTH 
    AND WG.LENGTH=m.LENGTH AND m.OWNER_NAME=LEFT(WG.LASTNAME ,35) AND m.TAX_YEAR=2013
where p.proptype='m'

update p set p.MOBILEKEY=wg.mobilekey 
from dy2013.dbo.property p 
inner join MCDUFFIE_GSI.dbo.MOBIL m on m.ACCOUNT_NO+m.LINE_NO=p.LEGACY_ACCT and m.TAX_YEAR=2013
inner join ##MHBRIDGE WG ON LEFT(WG.MFG,25)=m.MFG AND LEFT(WG.MODEL,25)=m.MODEL AND WG.YEARBUILT = m.YR_BUILT AND WG.TAXDIST=m.TAXDISTRIC AND m.FMV=WG.FMV AND WG.WIDTH=m.WIDTH 
    AND WG.LENGTH=m.LENGTH AND m.OWNER_NAME=LEFT(WG.LASTNAME ,35) AND m.TAX_YEAR=2013
where p.proptype='m' 

