﻿IF DB_NAME() LIKE 'KRETA_KLIK%' BEGIN -- csak KK

  INSERT INTO T_KKTANITOVEZGETTSEG (
     C_KKTANIOTOMUVTERTIPUSID
    ,C_ALKALMAZOTTID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,NNID
  ) SELECT 
     tv.C_KKTANIOTOMUVTERTIPUSID AS C_KKTANIOTOMUVTERTIPUSID -- C_KKTANIOTOMUVTERTIPUSID - int
    ,fh.ID   AS C_ALKALMAZOTTID         -- C_ALKALMAZOTTID - int
    ,tv.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,teNext.ID   AS C_TANEVID               -- C_TANEVID - int
    ,tv.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
    ,tv.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
    ,NULL AS NNID                   -- NNID - int
  FROM T_KKTANITOVEZGETTSEG tv
    INNER JOIN T_FELHASZNALO fh ON fh.ELOZOTANEVIREKORDID = tv.C_ALKALMAZOTTID
    INNER JOIN T_TANEV te ON te.ID = tv.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 tv.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKTANITOVEZGETTSEG WHERE ELOZOTANEVIREKORDID = tv.ID)
  
  INSERT INTO T_KKGYOGYPEDVEGZETTSEG (
     C_KKGYOGYPEDTERTIPUSID
    ,C_ALKALMAZOTTID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,NNID
  ) SELECT 
     gyp.C_KKGYOGYPEDTERTIPUSID AS C_KKGYOGYPEDTERTIPUSID -- C_KKGYOGYPEDTERTIPUSID - int
    ,fh.ID   AS C_ALKALMAZOTTID         -- C_ALKALMAZOTTID - int
    ,gyp.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,teNext.ID   AS C_TANEVID               -- C_TANEVID - int
    ,gyp.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
    ,gyp.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
    ,NULL AS NNID                   -- NNID - int
  FROM T_KKGYOGYPEDVEGZETTSEG gyp
    INNER JOIN T_FELHASZNALO fh ON fh.ELOZOTANEVIREKORDID = gyp.C_ALKALMAZOTTID
    INNER JOIN T_TANEV te ON te.ID = gyp.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 gyp.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKGYOGYPEDVEGZETTSEG WHERE ELOZOTANEVIREKORDID = gyp.ID)
    
  INSERT INTO T_KKTANARVEGZETTSEG (
     C_EGYEB
    ,C_KKTANARVEGZETTSEGID
    ,C_ALKALMAZOTTID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,NNID
  ) SELECT 
     tv.C_EGYEB AS C_EGYEB                -- C_EGYEB - nvarchar(MAX)
    ,tv.C_KKTANARVEGZETTSEGID AS C_KKTANARVEGZETTSEGID  -- C_KKTANARVEGZETTSEGID - int
    ,fh.ID   AS C_ALKALMAZOTTID         -- C_ALKALMAZOTTID - int
    ,tv.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,teNext.ID   AS C_TANEVID               -- C_TANEVID - int
    ,tv.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
    ,tv.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
    ,NULL AS NNID                   -- NNID - int
  FROM T_KKTANARVEGZETTSEG tv
    INNER JOIN T_FELHASZNALO fh ON fh.ELOZOTANEVIREKORDID = tv.C_ALKALMAZOTTID
    INNER JOIN T_TANEV te ON te.ID = tv.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 tv.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKTANARVEGZETTSEG WHERE ELOZOTANEVIREKORDID = tv.ID)

  INSERT INTO T_KKTANTARGYKATEGORIA (
     C_KKSZAKTIPUSID
    ,C_KKTANARVEGZETTSEGID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,NNID
  ) SELECT 
     tk.C_KKSZAKTIPUSID  AS C_KKSZAKTIPUSID        -- C_KKSZAKTIPUSID - int
    ,tv.ID   AS C_KKTANARVEGZETTSEGID   -- C_KKTANARVEGZETTSEGID - int
    ,tk.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,teNext.ID   AS C_TANEVID               -- C_TANEVID - int
    ,tk.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
    ,tk.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
    ,NULL AS NNID                   -- NNID - int
  FROM T_KKTANTARGYKATEGORIA tk
    INNER JOIN T_KKTANARVEGZETTSEG tv ON tk.C_KKTANARVEGZETTSEGID = tv.ELOZOTANEVIREKORDID
    INNER JOIN T_TANEV te ON te.ID = tk.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 tk.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKTANTARGYKATEGORIA WHERE ELOZOTANEVIREKORDID = tk.ID)
END