﻿--Migration
INSERT INTO T_TANULOSNI (
   C_FELULVIZSGALATTANEVE
  ,C_KIALLITASDATUMA
  ,C_KIALLITOSZAKBIZOTTSAGCIME
  ,C_KIALLITOSZAKBIZOTTSAGNEVE
  ,C_SZAKVELEMENYSZAMA
  ,C_TANULOID
  ,C_INTEZMENYID
  ,C_TANEVID
  ,TOROLT
  ,SERIAL
  ,LASTCHANGED
  ,CREATED
  ,MODIFIER
  ,CREATOR
  ,ELOZOTANEVIREKORDID
) SELECT
   sniOld.C_FELULVIZSGALATTANEVE AS C_FELULVIZSGALATTANEVE -- nvarchar(50)
  ,sniOld.C_KIALLITASDATUMA AS C_KIALLITASDATUMA -- datetime
  ,sniOld.C_KIALLITOSZAKBIZOTTSAGCIME AS C_KIALLITOSZAKBIZOTTSAGCIME -- nvarchar(500)
  ,sniOld.C_KIALLITOSZAKBIZOTTSAGNEVE AS C_KIALLITOSZAKBIZOTTSAGNEVE -- nvarchar(500)
  ,sniOld.C_SZAKVELEMENYSZAMA AS C_SZAKVELEMENYSZAMA -- nvarchar(500)
  ,dkNew.ID AS C_TANULOID -- * int
  ,sniOld.C_INTEZMENYID AS C_INTEZMENYID -- * int
  ,dkNew.C_ALTANEVID AS C_TANEVID -- * int
  ,sniOld.TOROLT AS TOROLT -- * char(1)
  ,0 AS SERIAL -- * int
  ,GETDATE() AS LASTCHANGED -- * datetime
  ,GETDATE() AS CREATED -- * datetime
  ,0 AS MODIFIER -- int
  ,0 AS CREATOR -- int
  ,sniOld.ID AS ELOZOTANEVIREKORDID -- int
FROM T_TANULOSNI sniOld
  INNER JOIN T_TANULO dkNew ON dkNew.ELOZOTANEVIREKORDID = sniOld.C_TANULOID AND dkNew.TOROLT = 'F'
  INNER JOIN T_TANEV tvOld ON tvOld.ID = sniOld.C_TANEVID AND tvOld.C_NEV = '2020/2021' AND tvOld.C_AKTIV = 'T' AND tvOld.TOROLT = 'F'
  INNER JOIN T_INTEZMENY i ON i.ID = tvOld.C_INTEZMENYID AND i.TOROLT = 'F'
WHERE sniOld.TOROLT = 'F'
  AND NOT EXISTS (SELECT 1 FROM T_TANULOSNI sniNew WHERE sniNew.C_TANULOID = dkNew.ID AND sniNew.TOROLT = 'F' AND sniNew.C_TANEVID = dkNew.C_ALTANEVID)
GO

