﻿UPDATE g
SET g.C_TANEVID = t.C_ALTANEVID
FROM T_GONDVISELO g
INNER JOIN T_TANULO t ON t.ID = g.C_TANULOID AND t.C_ALINTEZMENYID = g.C_INTEZMENYID
WHERE g.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2016/2017')
  AND t.C_ALTANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2017/2018')
  AND NOT EXISTS (SELECT 1 FROM T_GONDVISELO gUj WHERE gUj.C_NEV = g.C_NEV AND gUj.C_TANEVID = t.C_ALTANEVID)

UPDATE g
SET g.C_TANEVID = t.C_ALTANEVID, g.TOROLT = 'T'
FROM T_GONDVISELO g
INNER JOIN T_TANULO t ON t.ID = g.C_TANULOID AND t.C_ALINTEZMENYID = g.C_INTEZMENYID
WHERE g.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2016/2017')
  AND t.C_ALTANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2017/2018')

UPDATE c
SET c.C_TANEVID = g.C_TANEVID, c.TOROLT = IIF(g.TOROLT = 'F', c.TOROLT, 'T')
FROM T_CIM c
INNER JOIN T_GONDVISELO g ON g.ID = c.C_GONDVISELOID AND c.C_INTEZMENYID = g.C_INTEZMENYID
WHERE c.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2016/2017')
  AND g.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2017/2018')


UPDATE e
SET e.C_TANEVID = g.C_TANEVID, e.TOROLT = IIF(g.TOROLT = 'F', e.TOROLT, 'T')
FROM T_EMAIL e
INNER JOIN T_GONDVISELO g ON g.ID = e.C_GONDVISELOID AND e.C_INTEZMENYID = g.C_INTEZMENYID
WHERE e.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2016/2017')
  AND g.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2017/2018')


UPDATE t
SET t.C_TANEVID = g.C_TANEVID, t.TOROLT = IIF(g.TOROLT = 'F', t.TOROLT, 'T')
FROM T_TELEFON t
INNER JOIN T_GONDVISELO g ON g.ID = t.C_GONDVISELOID AND t.C_INTEZMENYID = g.C_INTEZMENYID
WHERE t.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2016/2017')
  AND g.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2017/2018')


UPDATE f
SET f.C_TANEVID = g.C_TANEVID, f.TOROLT = IIF(g.TOROLT = 'F', f.TOROLT, 'T')
FROM T_FELHASZNALOBELEPES f
INNER JOIN T_GONDVISELO g ON g.ID = f.C_GONDVISELOID AND f.C_INTEZMENYID = g.C_INTEZMENYID
WHERE f.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2016/2017')
  AND g.C_TANEVID IN (SELECT ID FROM T_TANEV WHERE T_TANEV.C_NEV = '2017/2018')

