﻿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'4/16'
	,N'5/13'
	,N'nagy kozepso'
	,N'kh/1/1'
	,N'kh/2/1/c'
	,N'kh/2/1/a'
	,N'kh/2/2/a'
	,N'kh/2/1/b'
	,N'kh/2/2/b'
	,N'szh/1'
	,N'szh/2'
	,N'szh/1/a'
	,N'szh/2/a'
	,N'szh/1/b'
	,N'szh/2/b')
  AND ID > 100000
  AND C_TYPE = N'EvfolyamTipus'

INSERT INTO @tmp0 VALUES 
	(6468, N'4/16')
	,(6469, N'5/13')
	,(6470, N'nagy kozepso')
	,(6471, N'kh/1/1')
	,(6472, N'kh/2/1/c')
	,(6473, N'kh/2/1/a')
	,(6474, N'kh/2/2/a')
	,(6475, N'kh/2/1/b')
	,(6476, N'kh/2/2/b')
	,(6477, N'szh/1')
	,(6478, N'szh/2')
	,(6479, N'szh/1/a')
	,(6480, N'szh/2/a')
	,(6481, N'szh/1/b')
	,(6482, N'szh/2/b')

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)


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_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
