﻿  DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT C_INTEZMENYID
		FROM T_INTEZMENYADATOK

  DECLARE @intezmenyId int

  OPEN intezmenytanevekCursor  
  FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId

  WHILE @@FETCH_STATUS = 0 
  BEGIN  

  DECLARE
     @oldTanevId int
    ,@newTanevId int

  SET @oldTanevId = (SELECT ID FROM T_TANEV WHERE C_NEV = '2016/2017' AND C_INTEZMENYID = @intezmenyId AND TOROLT = 'F');
  SET @newTanevId = (SELECT ID FROM T_TANEV WHERE C_NEV = '2017/2018' AND C_INTEZMENYID = @intezmenyId AND TOROLT = 'F');

  UPDATE fbOld
  SET fbOld.C_GUID = fbNew.C_GUID
  FROM T_FELHASZNALOBELEPES fbOld
    INNER JOIN T_FELHASZNALOBELEPES fbNew ON fbOld.C_BEJELENTKEZESINEV = fbNew.C_BEJELENTKEZESINEV
  WHERE fbOld.C_INTEZMENYID = @intezmenyId
    AND fbOld.C_TANEVID = @oldTanevId
    AND fbNew.C_INTEZMENYID = @intezmenyId
    AND fbNew.C_TANEVID = @newTanevId
    AND fbOld.C_BEJELENTKEZESINEV IN ('admin', 'fenntarto')

	  FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId
  END  

  CLOSE intezmenytanevekCursor  
  DEALLOCATE intezmenytanevekCursor