﻿
DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT IntezmenyAdatok.C_INTEZMENYID, IntezmenyAdatok.C_TANEVID
		FROM T_INTEZMENYADATOK IntezmenyAdatok
    INNER JOIN T_TANEV Tanev ON Tanev.ID = C_TANEVID
      AND Tanev.TOROLT = 'F'
      AND (Tanev.C_AKTIV = 'T' OR Tanev.C_KOVETKEZO = 'T')

DECLARE @intezmenyId int, @tanevId int

OPEN intezmenytanevekCursor  
FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId

WHILE @@FETCH_STATUS = 0 
BEGIN  

  DECLARE @FELTAREsetKezeloId INT = NULL
  SELECT TOP 1 @FELTAREsetKezeloId = ID FROM dbo.T_SZEREPKOR WHERE C_SZEREPKORTIPUS = 8828 AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@FELTAREsetKezeloId IS NULL)  BEGIN
  	INSERT INTO [dbo].T_SZEREPKOR ([C_SZEREPKORTIPUS],[C_INTEZMENYID],[C_TANEVID],[SERIAL],[LASTCHANGED],[CREATED],[MODIFIER],[CREATOR])
  		VALUES (8828,@intezmenyId,@tanevId,0,GETDATE(),GETDATE(),NULL,1)
  	SET @FELTAREsetKezeloId = SCOPE_IDENTITY()	
  END
  DECLARE @FELTAREsetKezeloJogId INT = NULL
  SELECT TOP 1 @FELTAREsetKezeloJogId = ID FROM dbo.T_JOGOSULTSAG WHERE C_JOG = N'FELTAR_EsetKezelo' AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@FELTAREsetKezeloJogId IS NULL)
  BEGIN
  	INSERT INTO dbo.T_JOGOSULTSAG (C_JOG,C_SZEREPKORTIPUS,[C_INTEZMENYID],[C_TANEVID],TOROLT,SERIAL,CREATED,CREATOR)
  	  VALUES (N'FELTAR_EsetKezelo',8828,@intezmenyId , @tanevId,'F',0,GETDATE(),0)
  	SET @FELTAREsetKezeloJogId = SCOPE_IDENTITY()
  END

  DECLARE @FELTAREszkozMenedzserId INT = NULL
  SELECT TOP 1 @FELTAREszkozMenedzserId = ID FROM dbo.T_SZEREPKOR WHERE C_SZEREPKORTIPUS = 8829 AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@FELTAREszkozMenedzserId IS NULL)  BEGIN
  	INSERT INTO [dbo].T_SZEREPKOR ([C_SZEREPKORTIPUS],[C_INTEZMENYID],[C_TANEVID],[SERIAL],[LASTCHANGED],[CREATED],[MODIFIER],[CREATOR])
  		VALUES (8829,@intezmenyId,@tanevId,0,GETDATE(),GETDATE(),NULL,1)
  	SET @FELTAREszkozMenedzserId = SCOPE_IDENTITY()	
  END
  DECLARE @FELTAREszkozMenedzserJogId INT = NULL
  SELECT TOP 1 @FELTAREszkozMenedzserJogId = ID FROM dbo.T_JOGOSULTSAG WHERE C_JOG = N'FELTAR_EszkozMenedzser' AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@FELTAREszkozMenedzserJogId IS NULL)
  BEGIN
  	INSERT INTO dbo.T_JOGOSULTSAG (C_JOG,C_SZEREPKORTIPUS,[C_INTEZMENYID],[C_TANEVID],TOROLT,SERIAL,CREATED,CREATOR)
  	  VALUES (N'FELTAR_EszkozMenedzser',8829,@intezmenyId , @tanevId,'F',0,GETDATE(),0)
  	SET @FELTAREszkozMenedzserJogId = SCOPE_IDENTITY()
  END

  FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId
END  

CLOSE intezmenytanevekCursor  
DEALLOCATE intezmenytanevekCursor

GO
