﻿DECLARE TulajdonsagCursor CURSOR FOR 
SELECT * FROM (VALUES 
(6837, N'C_DIFFERENCIALT'), 
(6838, N'C_DRAMA'), 
(6839, N'C_IKTTANORA'), 
(6840, N'C_IPRTANORA'), 
(6841, N'C_KOOPERATIV'), 
(6842, N'C_MERES'), 
(6843, N'C_MINDENNAPOSTESTNEVELES'), 
(6844, N'C_OSZTALYFONOKI'), 
(6845, N'C_NEMSZAKRENDSZERUORA'), 
(6846, N'C_NEMZETISEGIORA'), 
(6847, N'C_TAMOPORA'), 
(6848, N'C_TULORA'), 
(6849, N'C_BONTOTT'), 
(6850, N'C_PARHUZAMOSORA'), 
(6851, N'C_TIOP12')
) x (id, col) 


DECLARE @id int
,@col nvarchar(35)
,@sql nvarchar(max)

OPEN TulajdonsagCursor
FETCH NEXT FROM TulajdonsagCursor INTO @id, @col 

WHILE @@FETCH_STATUS = 0 BEGIN
SET @sql = N'
INSERT INTO T_TANITASIORATULAJDONSAG (
	 C_ORATULAJDONSAGID
	,C_TANITASIORAID
	,C_INTEZMENYID
	,C_TANEVID
	,TOROLT
	,SERIAL
	,LASTCHANGED
	,CREATED
	,MODIFIER
	,CREATOR
	,ELOZOTANEVIREKORDID
	,NNID
) SELECT 
	@id AS C_ORATULAJDONSAGID -- C_ORATULAJDONSAGID - int
	,tn.ID AS C_TANITASIORAID -- C_TANITASIORAID - int
	,tn.C_INTEZMENYID AS C_INTEZMENYID -- C_INTEZMENYID - int
	,tn.C_TANEVID AS C_TANEVID -- C_TANEVID - int
	,tn.TOROLT AS TOROLT -- TOROLT - char(1)
	,0 AS SERIAL -- SERIAL - int
	,tn.CREATED AS LASTCHANGED -- LASTCHANGED - datetime
	,tn.CREATED AS CREATED -- CREATED - datetime
	,tn.CREATOR AS MODIFIER -- MODIFIER - int
	,tn.CREATOR AS CREATOR -- CREATOR - int
	,NULL AS ELOZOTANEVIREKORDID -- ELOZOTANEVIREKORDID - int
	,NULL AS NNID -- NNID - int
FROM T_TANITASIORA tn
WHERE tn.'+ @col + ' = ''T''
AND NOT EXISTS (
	SELECT 1
	FROM T_TANITASIORATULAJDONSAG tt
	WHERE tt.C_ORATULAJDONSAGID = @id AND tt.C_TANITASIORAID = tn.ID
)
'

EXEC sys.sp_executesql @sql, N'@id int', @id

FETCH NEXT FROM TulajdonsagCursor INTO @id, @col
END

CLOSE TulajdonsagCursor
DEALLOCATE TulajdonsagCursor