﻿    UPDATE dib
    SET dib.C_TANEVID = a.C_TANEVID 
       ,dib.MODIFIER = 0
       ,dib.LASTCHANGED = GETDATE()
       ,dib.SERIAL += 1
    FROM T_FELHASZNALOBELEPESTORTENET dib WITH (NOLOCK)
    INNER JOIN T_FELHASZNALO a WITH (NOLOCK) ON a.ID = dib.C_FELHASZNALOID
    WHERE dib.C_TANEVID <> a.C_TANEVID

    UPDATE dib
    SET dib.C_TANEVID = a.C_TANEVID 
       ,dib.MODIFIER = 0
       ,dib.LASTCHANGED = GETDATE()
       ,dib.SERIAL += 1
    FROM T_FELHASZNALOBELEPESTORTENET dib WITH (NOLOCK)
    INNER JOIN T_GONDVISELO a WITH (NOLOCK) ON a.ID = dib.C_GONDVISELOID
    WHERE dib.C_TANEVID <> a.C_TANEVID
    
    UPDATE x
    SET x.C_SZEREPKORID = z2.ID
    FROM dbo.T_FELHASZNALO_SZEREPKOR x
    INNER JOIN dbo.T_FELHASZNALO s ON s.ID  = x.C_FELHASZNALOID
    INNER JOIN dbo.T_SZEREPKOR z ON z.ID  = x.C_SZEREPKORID 
    INNER JOIN dbo.T_SZEREPKOR z2 ON z2.C_SZEREPKORTIPUS  = z.C_SZEREPKORTIPUS AND z2.C_TANEVID = s.C_TANEVID
    WHERE s.C_TANEVID <> z.C_TANEVID AND z.C_INTEZMENYID = s.C_INTEZMENYID
      AND NOT EXISTS (SELECT 1 FROM dbo.T_FELHASZNALO_SZEREPKOR fsz WHERE fsz.C_SZEREPKORID = z2.ID AND x.C_FELHASZNALOID = fsz.C_FELHASZNALOID)

    DELETE x
    FROM dbo.T_FELHASZNALO_SZEREPKOR x
    INNER JOIN dbo.T_FELHASZNALO s ON s.ID  = x.C_FELHASZNALOID
    INNER JOIN dbo.T_SZEREPKOR z ON z.ID  = x.C_SZEREPKORID
    WHERE s.C_TANEVID <> z.C_TANEVID

    UPDATE dib
    SET dib.C_TANEVID = a.C_TANEVID 
       ,dib.MODIFIER = 0
       ,dib.LASTCHANGED = GETDATE()
       ,dib.SERIAL += 1
    FROM T_JELSZOMODOSITASLINK dib WITH (NOLOCK)
    INNER JOIN T_FELHASZNALO a WITH (NOLOCK) ON a.ID = dib.C_FELHASZNALOID
    WHERE dib.C_TANEVID <> a.C_TANEVID        
    
    UPDATE dib
    SET dib.C_TANEVID = a.C_TANEVID 
       ,dib.MODIFIER = 0
       ,dib.LASTCHANGED = GETDATE()
       ,dib.SERIAL += 1
    FROM T_JELSZOMODOSITASLINK dib WITH (NOLOCK)
    INNER JOIN T_GONDVISELO a WITH (NOLOCK) ON a.ID = dib.C_GONDVISELOID
    WHERE dib.C_TANEVID <> a.C_TANEVID 

    UPDATE dib
    SET dib.C_TANEVID = a.C_TANEVID 
       ,dib.MODIFIER = 0
       ,dib.LASTCHANGED = GETDATE()
       ,dib.SERIAL += 1
    FROM T_OLDALLATOGATOTTSAG dib WITH (NOLOCK)
    INNER JOIN T_FELHASZNALO a WITH (NOLOCK) ON a.ID = dib.C_FELHASZNALOID
    WHERE dib.C_TANEVID <> a.C_TANEVID
 
    UPDATE dib
    SET dib.C_TANEVID = a.C_TANEVID 
       ,dib.MODIFIER = 0
       ,dib.LASTCHANGED = GETDATE()
       ,dib.SERIAL += 1
    FROM T_OLDALLATOGATOTTSAG dib WITH (NOLOCK)
    INNER JOIN T_GONDVISELO a WITH (NOLOCK) ON a.ID = dib.C_GONDVISELOID
    WHERE dib.C_TANEVID <> a.C_TANEVID
