﻿--Migration
INSERT INTO T_TANEV (
   C_AKTIV
  ,C_ELSOTANITASINAP
  ,C_KEZDONAP
  ,C_KOVETKEZO
  ,C_NEV
  ,C_SORSZAM
  ,C_UTOLSONAP
  ,C_UTOLSOTANITASINAP
  ,C_AKTIVALVA
  ,C_INTEZMENYID
  ,TOROLT
  ,SERIAL
  ,LASTCHANGED
  ,CREATED
  ,MODIFIER
  ,CREATOR
  ,ELOZOTANEVIREKORDID
  ,C_ISKIVALASZTHATO
) SELECT
   'F' AS C_AKTIV -- char(1)
  ,CAST(SUBSTRING(x.Nev, 1, 4) + '0901' AS datetime) AS C_ELSOTANITASINAP -- datetime
  ,CAST(SUBSTRING(x.Nev, 1, 4) + '0901' AS datetime) AS C_KEZDONAP -- datetime
  ,'F' AS C_KOVETKEZO -- char(1)
  ,x.Nev AS C_NEV -- nvarchar(20)
  ,SUBSTRING(x.Nev,3,2) * 2 AS C_SORSZAM -- int
  ,CAST(SUBSTRING(x.Nev, 6, 4) + '0831' AS datetime) AS C_UTOLSONAP -- datetime
  ,CAST(SUBSTRING(x.Nev, 6, 4) + '0615' AS datetime) AS C_UTOLSOTANITASINAP -- datetime
  ,NULL AS C_AKTIVALVA -- datetime
  ,i.ID AS C_INTEZMENYID -- * int
  ,'F' AS TOROLT -- * char(1)
  ,0 AS SERIAL -- * int
  ,GETDATE() AS LASTCHANGED -- * datetime
  ,GETDATE() AS CREATED -- * datetime
  ,NULL AS MODIFIER -- int
  ,0 AS CREATOR -- int
  ,NULL AS ELOZOTANEVIREKORDID -- int
  ,'F' AS C_ISKIVALASZTHATO -- * char(1)
FROM T_INTEZMENY i
  CROSS JOIN (VALUES 
    ('2021/2022'), ('2022/2023'), ('2023/2024'), ('2024/2025'), ('2025/2026'),
    ('2026/2027'), ('2027/2028'), ('2028/2029'), ('2029/2030'), ('2030/2031')
  ) x (Nev)
WHERE i.TOROLT = 'F'
  AND NOT EXISTS (SELECT 1 FROM T_TANEV tv WHERE i.ID = tv.C_INTEZMENYID AND tv.TOROLT = 'F' AND tv.C_NEV = x.Nev)
ORDER BY i.ID, x.Nev


