﻿DECLARE @tmp TABLE (Id_OLD INT, Id_NEW INT, Nev NVARCHAR(MAX))
DECLARE @tmp0 TABLE (Id_NEW INT, SKey NVARCHAR(MAX))

INSERT INTO @tmp (Id_OLD, Nev)
SELECT ID, CONVERT(VARCHAR(MAX), LOWER(C_NAME) COLLATE SQL_Latin1_General_Cp1251_CS_AS) AS Nev
FROM T_DICTIONARYITEMBASE
WHERE CONVERT(VARCHAR(MAX), LOWER(C_NAME) COLLATE SQL_Latin1_General_Cp1251_CS_AS) IN (N'9/ajkp', N'projektor')
  AND ID > 100000
  AND (C_TYPE = N'EvfolyamTipus' OR C_TYPE = N'EszkozTipus')

INSERT INTO @tmp0 VALUES 
	(6468, N'9/ajkp'),
	(6493, N'projektor')

UPDATE @tmp
 SET Id_NEW = tmp0.Id_NEW
FROM @tmp
INNER JOIN @tmp0 tmp0 ON tmp0.SKey = Nev

UPDATE T_DICTIONARYITEMBASE
  SET TOROLT = 'T', C_NAME += N'_OLD', C_NAME_1 += N'_OLD'
WHERE ID IN (SELECT Id_OLD FROM @tmp)

UPDATE T_EVFOLYAMTIPUS 
	SET TOROLT = 'T'
WHERE ID IN (SELECT Id_OLD FROM @tmp)

UPDATE T_DICTIONARYITEMBASE
	SET C_NAME = '9/AJKP',
		C_NAME_1 = '9/AJKP'
	WHERE ID = 3019

DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT C_INTEZMENYID, C_TANEVID
		FROM T_INTEZMENYADATOK

DECLARE @intezmenyId int, @tanevId int

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

WHILE @@FETCH_STATUS = 0 
BEGIN  
	PRINT N'INTÉZMÉNY ID: ' + CAST(@intezmenyId AS NVARCHAR(10)) + N', TANÉV ID: ' + CAST(@tanevId AS NVARCHAR(10))
    EXEC sp_Global_CreateDictionaryItems @IntezmenyId = @intezmenyId, @TanevId = @tanevId

	FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId
END  

CLOSE intezmenytanevekCursor  
DEALLOCATE intezmenytanevekCursor

UPDATE T_ESZKOZ SET C_TIPUS = tmp.Id_NEW
FROM T_ESZKOZ te 
INNER JOIN @tmp tmp ON te.C_TIPUS = tmp.Id_OLD

UPDATE T_KIZAROCSOPORT SET C_EVFOLYAM = tmp.Id_NEW
FROM T_KIZAROCSOPORT te 
INNER JOIN @tmp tmp ON te.C_EVFOLYAM = tmp.Id_OLD

UPDATE T_ORATERV SET C_EVFOLYAM = tmp.Id_NEW
FROM T_ORATERV te 
INNER JOIN @tmp tmp ON te.C_EVFOLYAM = tmp.Id_OLD

UPDATE T_TANTERV SET C_EVFOLYAMIG = tmp.Id_NEW
FROM T_TANTERV te 
INNER JOIN @tmp tmp ON te.C_EVFOLYAMIG = tmp.Id_OLD

UPDATE T_TANTERV SET C_EVFOLYAMTOL = tmp.Id_NEW
FROM T_TANTERV te 
INNER JOIN @tmp tmp ON te.C_EVFOLYAMTOL = tmp.Id_OLD

UPDATE T_OSZTALYCSOPORT SET C_EVFOLYAMTIPUSA = tmp.Id_NEW
FROM T_OSZTALYCSOPORT te 
INNER JOIN @tmp tmp ON te.C_EVFOLYAMTIPUSA = tmp.Id_OLD

UPDATE T_EVFOLYAMTIPUS_OKTATASINEVELE SET C_EVFOLYAMTIPUSID = tmp.Id_NEW
FROM T_EVFOLYAMTIPUS_OKTATASINEVELE te 
INNER JOIN @tmp tmp ON te.C_EVFOLYAMTIPUSID = tmp.Id_OLD

GO
