﻿DECLARE @intezmenyId int, @aktTanevNev nvarchar(10)

DECLARE intCursor CURSOR FAST_FORWARD READ_ONLY FOR 
SELECT i.ID, tv.C_NEV
FROM T_INTEZMENY i
  INNER JOIN T_TANEV tv ON tv.C_INTEZMENYID = i.ID AND tv.C_AKTIV = 'T'
WHERE i.TOROLT = 'F'
  AND tv.TOROLT = 'F'
  AND EXISTS (SELECT 1 FROM T_TANEV tv2 WHERE tv2.C_INTEZMENYID = i.ID AND tv2.C_KOVETKEZO = 'T' AND tv2.C_ISKIVALASZTHATO = 'F')

OPEN intCursor

FETCH NEXT FROM intCursor 
INTO @intezmenyId, @aktTanevNev

WHILE @@FETCH_STATUS = 0 BEGIN
      EXEC [dev].[uspGenerateTanevAdatokFromExistingTanev]
	       @intezmenyId = @intezmenyId
	      ,@tanevNeveNew = '2021/2022'
	      ,@tanevNeveSource = @aktTanevNev
	      ,@kovetkezoTanev = 1   
  FETCH NEXT FROM intCursor 
  INTO @intezmenyId, @aktTanevNev
END

CLOSE intCursor
DEALLOCATE intCursor
GO 