﻿INSERT INTO T_LICENCE (
   C_DATE
  ,C_FULL
  ,C_FUNCTION
  ,C_INTEZMENYID
  ,C_TANEVID
  ,TOROLT
  ,SERIAL
  ,LASTCHANGED
  ,CREATED
  ,MODIFIER
  ,CREATOR
  ,ELOZOTANEVIREKORDID
  ,NNID
) SELECT 
   lc.C_DATE AS C_DATE                 -- C_DATE - datetime
  ,lc.C_FULL  AS C_FULL                  -- C_FULL - char(1)
  ,lc.C_FUNCTION AS C_FUNCTION             -- C_FUNCTION - int
  ,lc.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
  ,teNext.ID   AS C_TANEVID               -- C_TANEVID - int
  ,lc.TOROLT  AS TOROLT                  -- TOROLT - char(1)
  ,0   AS SERIAL                  -- SERIAL - int
  ,GETDATE() AS LASTCHANGED       -- LASTCHANGED - datetime
  ,GETDATE() AS CREATED           -- CREATED - datetime
  ,NULL AS MODIFIER               -- MODIFIER - int
  ,NULL AS CREATOR                -- CREATOR - int
  ,NULL AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  ,NULL AS NNID                   -- NNID - int
FROM T_LICENCE lc
  INNER JOIN T_TANEV te ON te.ID = lc.C_TANEVID AND te.C_NEV = '2017/2018'  AND te.TOROLT = 'F'
  INNER JOIN T_TANEV teNext ON te.C_INTEZMENYID = teNext.C_INTEZMENYID AND teNext.C_NEV = '2018/2019'  AND te.TOROLT = 'F'
WHERE NOT EXISTS (SELECT 1 FROM T_LICENCE lcs WHERE lcs.C_TANEVID = teNext.ID AND lcs.C_INTEZMENYID = lc.C_INTEZMENYID) 
