﻿DECLARE @TypeId int
DECLARE @TypeId2 int

DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT DISTINCT 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))

	IF (@TypeId IS NULL) BEGIN
		SELECT @TypeId = ID
		FROM T_DICTIONARYTYPE
		WHERE C_CODENAME = N'KKTanitoMuvTerTipus'
	END

	IF (@TypeId2 IS NULL) BEGIN
		SELECT @TypeId2 = ID
		FROM T_DICTIONARYTYPE
		WHERE C_CODENAME = N'KKGyogypedTerTipus'
	END

	SET IDENTITY_INSERT [dbo].[T_DICTIONARYITEMBASE] ON

		INSERT INTO dbo.T_DICTIONARYITEMBASE (ID, C_NAME, C_NAME_1, C_NAME_2, C_NAME_3, C_NAME_4, C_TYPE, C_PROTECTED, C_VISIBLE, C_ORDER, C_CODE, C_VALUE, C_COLOR, C_DICTIONARYTYPEID, C_INTEZMENYID, C_TANEVID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR)
		VALUES
			(7352, N'Na', N'Na', NULL, NULL, NULL, N'KKTanitoMuvTerTipus', N'T', N'T', 1, NULL, 1, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
			(7353, N'Na', N'Na', NULL, NULL, NULL, N'KKGyogypedTerTipus', N'T', N'T', 1, NULL, 1, NULL, @TypeId2, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)

    SET IDENTITY_INSERT [dbo].[T_DICTIONARYITEMBASE] OFF

		INSERT INTO [dbo].[T_KKTANITOMUVTERTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) VALUES
			(7352, @IntezmenyId, @TanevId)

		INSERT INTO [dbo].[T_KKGYOGYPEDTERTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) VALUES
			(7353, @IntezmenyId, @TanevId)

	FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId
END  

CLOSE intezmenytanevekCursor  
DEALLOCATE intezmenytanevekCursor

GO


UPDATE T_DICTIONARYITEMBASE
SET C_ORDER = C_ORDER + 1,
	C_VALUE = C_VALUE + 1
WHERE ID IN (7330,7331,7332,7333,7334,7335,7336,7337,7338,7339,7340,7341,7342,7343,7344,7345,7346,7347)
GO
