﻿INSERT INTO T_SZEREPKOR (C_SZEREPKORTIPUS, C_INTEZMENYID, C_TANEVID, SERIAL)
SELECT 6502, C_INTEZMENYID, t.ID, 0
FROM T_TANEV t
INNER JOIN T_INTEZMENY i ON i.ID = t.C_INTEZMENYID 
WHERE t.TOROLT = 'F'
    AND i.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_SZEREPKOR s WHERE s.C_SZEREPKORTIPUS = 6502 AND s.C_INTEZMENYID = t.C_INTEZMENYID AND s.C_TANEVID = t.ID)

INSERT INTO T_SZEREPKOR (C_SZEREPKORTIPUS, C_INTEZMENYID, C_TANEVID, SERIAL)
SELECT 6503, C_INTEZMENYID, t.ID, 0
FROM T_TANEV t 
INNER JOIN T_INTEZMENY i ON i.ID = t.C_INTEZMENYID 
WHERE t.TOROLT = 'F'
    AND i.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_SZEREPKOR s WHERE s.C_SZEREPKORTIPUS = 6503 AND s.C_INTEZMENYID = t.C_INTEZMENYID AND s.C_TANEVID = t.ID)