﻿--Migration
DECLARE @IntezmenyId varchar(100)
       ,@pAktTanevId varchar(100)
       ,@pKovTanevId varchar(100)
DECLARE keys_cursor CURSOR FOR
SELECT i.ID as IntezmenyId, t.ID as pAktTanevId  from T_INTEZMENY i
INNER JOIN T_TANEV t on t.C_INTEZMENYID = i.ID
WHERE i.C_ISARCHIV = 'F' AND i.TOROLT = 'F' and t.C_NEV = '2021/2022' AND t.TOROLT = 'F'

OPEN keys_cursor

FETCH NEXT FROM keys_cursor
INTO @IntezmenyId, @pAktTanevId

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @pKovTanevId = t.ID from T_INTEZMENY i
INNER JOIN T_TANEV t on t.C_INTEZMENYID = i.ID
WHERE i.ID = @IntezmenyId and i.C_ISARCHIV = 'F' AND i.TOROLT = 'F' and t.C_NEV = '2022/2023' AND t.TOROLT = 'F'

    INSERT INTO T_TanulasiEgyseg(
	 C_SzakmaTipusID
	,C_SzakmaIranyTipusID
	,C_SorSzam
	,C_Szoveg
    ,C_INTEZMENYID
    ,C_TANEVID
	,TOROLT
	,SERIAL
	,LASTCHANGED
	,CREATED
	,MODIFIER
	,CREATOR
	,ELOZOTANEVIREKORDID
) SELECT
 te.C_SzakmaTipusID
  ,te.C_SzakmaIranyTipusID
  ,te.C_SorSzam
  ,te.C_Szoveg
  ,te.C_INTEZMENYID
  ,@pKovTanevId
  ,'F' AS TOROLT
  ,0 AS SERIAL
  ,GETDATE() AS LASTCHANGED
  ,GETDATE() AS CREATED
  ,0 AS MODIFIER
  ,0 AS CREATOR
  ,te.ID AS ELOZOTANEVIREKORDID
  FROM T_TanulasiEgyseg te
  WHERE te.TOROLT = 'F' 
    AND te.C_TANEVID = @pAktTanevId
    AND NOT EXISTS (SELECT 1 FROM T_TanulasiEgyseg_OSSZES u WHERE u.C_TANEVID = @pKovTanevId AND u.TOROLT = 'F' AND u.ELOZOTANEVIREKORDID = te.ID)

FETCH NEXT FROM keys_cursor
INTO @IntezmenyId, @pAktTanevId

END

CLOSE keys_cursor;
DEALLOCATE keys_cursor;