﻿DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT i.ID, t.ID
		FROM T_INTEZMENY i
    JOIN T_TANEV t ON i.ID = t.C_INTEZMENYID AND t.Torolt = 'F' AND t.C_AKTIV = 'T' AND t.C_NEV = '2018/2019'
    WHERE i.TOROLT = 'F'

DECLARE @intezmenyId int, @tanevId int
DECLARE @ErtekelesModOptionList nVARCHAR(MAX) 
DROP TABLE IF EXISTS #ErtekelesModoktmp;
	CREATE TABLE #ErtekelesModoktmp (options nvarchar(MAX))
OPEN intezmenytanevekCursor  
FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId


WHILE @@FETCH_STATUS = 0 
BEGIN  
  TRUNCATE TABLE #ErtekelesModoktmp
	INSERT INTO #ErtekelesModoktmp
		SELECT '{"Selected":'+IIF(ISNULL(C_NAME,'') = 'Írásbeli témazáró dolgozat','true','false')+',"Text":"' + ISNULL(C_NAME,'') + '","Value":"' + CAST(ISNULL(ID,'') as nvarchar(10)) + '"}' 
    from T_DICTIONARYITEMBASE 
    where C_TYPE like 'ErtekelesMod' and C_INTEZMENYID = @intezmenyId 
    and C_TANEVID = @tanevId and TOROLT = 'F' order by C_VALUE
	
	set @ErtekelesModOptionList = null
	SELECT @ErtekelesModOptionList = COALESCE(@ErtekelesModOptionList + ', ', '') + options FROM #ErtekelesModoktmp
	SET @ErtekelesModOptionList = ISNULL(@ErtekelesModOptionList, '')
  SELECT  * FROM #ErtekelesModoktmp
  SELECT @ErtekelesModOptionList
	UPDATE T_RENDSZERBEALLITAS SET C_ALAPERTEK = '{"Id":"Ertekeles_Modok_Beallitasa_Napi_Limithez","Options":[' + @ErtekelesModOptionList + ']}', 
	C_ERTEK = '{"Id":"Ertekeles_Modok_Beallitasa_Napi_Limithez","Options":[' + @ErtekelesModOptionList + ']}' WHERE C_BEALLITASTIPUS = 7662 and C_INTEZMENYID = @intezmenyId and C_TANEVID = @tanevId
  
	FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId
  
END  

CLOSE intezmenytanevekCursor  
DROP TABLE IF EXISTS #ErtekelesModoktmp;
DEALLOCATE intezmenytanevekCursor 
