﻿UPDATE tcs SET 
  tcs.C_KILEPESDATUM = '20210831'
 ,tcs.C_KILEPESROGZITESDATUM = GETDATE()
 ,tcs.MODIFIER = 0
 ,tcs.LASTCHANGED = GETDATE()
FROM T_TANULOCSOPORT tcs
  INNER JOIN T_TANEV tv ON tv.ID = tcs.C_TANEVID AND tv.TOROLT = 'F'
  INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tcs.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
  INNER JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
  INNER JOIN T_TANULO dk ON dk.ID = tcs.C_TANULOID
WHERE tv.C_AKTIV = 'T' 
  AND tv.C_NEV = '2020/2021' 
  AND tcs.TOROLT = 'F'
  AND tcs.C_KILEPESDATUM IS NULL
  AND EXISTS (
    SELECT 1 
    FROM T_TANULOCSOPORT tcsKov
      INNER JOIN T_TANEV tvKov ON tvKov.ID = tcsKov.C_TANEVID AND tvKov.TOROLT = 'F'
      INNER JOIN T_OSZTALYCSOPORT ocsKov ON ocsKov.ID = tcsKov.C_OSZTALYCSOPORTID AND ocsKov.TOROLT = 'F'
      INNER JOIN T_OSZTALY oKov ON oKov.ID = ocsKov.ID AND oKov.TOROLT = 'F'
      INNER JOIN T_TANULO dkKov ON dkKov.ID = tcsKov.C_TANULOID
    WHERE tvKov.C_KOVETKEZO = 'T' 
      AND tvKov.C_NEV = '2021/2022' 
      AND tcsKov.TOROLT = 'F'
      AND dkKov.ELOZOTANEVIREKORDID = dk.ID
      AND ocsKov.C_FELADATKATEGORIAID = ocs.C_FELADATKATEGORIAID
  )
GO