﻿DECLARE tanev_cur CURSOR FOR 
SELECT ID, C_INTEZMENYID FROM T_TANEV 
WHERE CAST(C_UTOLSONAP as date) =  '2018-08-31' and C_NEV = '2018/2019' 

DECLARE 
   @intezmenyId int
  ,@tanevid int

OPEN tanev_cur
FETCH NEXT FROM tanev_cur INTO @tanevid,  @intezmenyId

WHILE @@FETCH_STATUS = 0 BEGIN
  DELETE FROM T_NAPTARINAP WHERE C_TANEVID = @tanevid
  DELETE FROM T_NAPTARIHET WHERE C_TANEVID = @tanevid

  UPDATE T_TANEV SET C_UTOLSONAP = '20190831' WHERE ID = @tanevid

  EXEC dbo.sp_GenerateNaptariHetek  
    @intezmenyId = @intezmenyId,
    @tanevId = @tanevid


  EXEC dbo.sp_GenerateNaptariNapok
    @intezmenyId = @intezmenyId,
    @tanevId = @tanevid

  FETCH NEXT FROM tanev_cur INTO @tanevid,  @intezmenyId
END

CLOSE tanev_cur
DEALLOCATE tanev_cur
