DECLARE @tanevId int,
        @intezmenyId int

DECLARE TanevIntezmeny CURSOR FOR 
  SELECT tt.ID, tt.C_INTEZMENYID 
  FROM dbo.T_TANEV tt 
  INNER JOIN T_INTEZMENY i ON i.ID = tt.C_INTEZMENYID
  WHERE tt.C_NEV='2017/2018' AND tt.TOROLT = 'F' AND i.TOROLT = 'F'

OPEN TanevIntezmeny
FETCH NEXT FROM TanevIntezmeny INTO @tanevId,@intezmenyId
WHILE @@FETCH_STATUS=0
  BEGIN

  IF((SELECT COUNT(nh.ID) 
		FROM T_TANEV t
		INNER JOIN T_INTEZMENY i ON i.ID = t.C_INTEZMENYID
		LEFT JOIN T_NAPTARIHET nh ON nh.C_TANEVID = t.ID
		WHERE t.Id = @tanevId AND i.ID = @intezmenyId) = 0)
	BEGIN
		EXEC [sp_GenerateNaptariHetek] @intezmenyId,@tanevId
	END

    EXEC [sp_GenerateNaptariNapok] @intezmenyId,@tanevId,DEFAULT
    FETCH NEXT FROM TanevIntezmeny INTO @tanevId,@intezmenyId
  END
CLOSE TanevIntezmeny
DEALLOCATE TanevIntezmeny
