IF OBJECT_ID('sp_FollowUpAlkalmazott') IS NOT NULL BEGIN
  DROP PROCEDURE sp_FollowUpAlkalmazott
END;
GO

CREATE PROCEDURE sp_FollowUpAlkalmazott
  @intezmenyId int,
  @aktTanevId int,
  @kovetkezoTanevId int,
  @alkalmazottId int
AS
BEGIN

IF (@alkalmazottId = 0)
	BEGIN
		INSERT INTO T_FELHASZNALO_OSSZES (
			 C_ADOSZAM
			,C_ALAIRAS
			,C_ALAPERTELMEZETTLCID
			,C_ALLAMPOLGARSAGA
			,C_ALLAMPOLGARSAGA2
			,C_ANYANYELVE
			,C_ANYJANEVE
			,C_ANYJANEVEELVALASZTO
			,C_ANYJANEVESORREND
			,C_DMSTARTTIME
			,C_ELOTAG
			,C_FENNTARTOAZONOSITO
			,C_IGAZOLVANYSZAM
			,C_IGAZOLVANYTIPUSA
			,C_INTEZMENYID
			,C_KELLADSZINKRONIZALAS
			,C_KERESESINEV
			,C_KIRAZONOSITO
			,C_KRETAKOD
			,C_LATASSERULTWEBHASZNALATA
			,C_LEIRAS
			,C_NEME
			,C_NEVSORREND
			,C_NYILVANTARTASISZAM
			,C_NYILVANTARTASKEZDETE
			,C_NYOMTATASINEV
			,C_OKTATASIAZONOSITO
			,C_OMKOD
			,C_PROFILKEP
			,C_SMSSZAM
			,C_SMSSZINKRONIZALT
			,C_SZULETESIDATUM
			,C_SZULETESIHELY
			,C_SZULETESINEV
			,C_SZULETESINEVELVALASZTO
			,C_SZULETESINEVSORREND
			,C_SZULETESIORSZAG
			,C_TAJSZAM
			,C_TANEVID
			,C_UTONEV
			,C_VEZETEKNEV
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  C_ADOSZAM AS C_ADOSZAM
			  ,C_ALAIRAS AS C_ALAIRAS
			  ,C_ALAPERTELMEZETTLCID AS C_ALAPERTELMEZETTLCID
			  ,IIF((SELECT COUNT(1) FROM T_ALLAMPOLGARSAG_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_ALLAMPOLGARSAGA) = 0,216,t.C_ALLAMPOLGARSAGA) AS C_ALLAMPOLGARSAGA
			  ,IIF((SELECT COUNT(1) FROM T_ALLAMPOLGARSAG_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_ALLAMPOLGARSAGA2) = 0,216,t.C_ALLAMPOLGARSAGA2) AS C_ALLAMPOLGARSAGA2
			  ,IIF((SELECT COUNT(1) FROM T_ANYANYELV_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_ANYANYELVE) = 0,493,t.C_ANYANYELVE) AS C_ANYANYELVE
			  ,C_ANYJANEVE AS C_ANYJANEVE
			  ,C_ANYJANEVEELVALASZTO AS C_ANYJANEVEELVALASZTO
			  ,C_ANYJANEVESORREND AS C_ANYJANEVESORREND
			  ,C_DMSTARTTIME AS C_DMSTARTTIME
			  ,C_ELOTAG AS C_ELOTAG
			  ,C_FENNTARTOAZONOSITO AS C_FENNTARTOAZONOSITO
			  ,C_IGAZOLVANYSZAM AS C_IGAZOLVANYSZAM
			  ,IIF((SELECT COUNT(1) FROM T_IGAZOLVANYTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_IGAZOLVANYTIPUSA) = 0,543,t.C_IGAZOLVANYTIPUSA) AS C_IGAZOLVANYTIPUSA
			  ,@intezmenyID AS C_INTEZMENYID
			  ,C_KELLADSZINKRONIZALAS AS C_KELLADSZINKRONIZALAS
			  ,C_KERESESINEV AS C_KERESESINEV
			  ,C_KIRAZONOSITO AS C_KIRAZONOSITO
			  ,C_KRETAKOD AS C_KRETAKOD
			  ,C_LATASSERULTWEBHASZNALATA AS C_LATASSERULTWEBHASZNALATA
			  ,C_LEIRAS AS C_LEIRAS
			  ,C_NEME AS C_NEME
			  ,C_NEVSORREND AS C_NEVSORREND
			  ,C_NYILVANTARTASISZAM AS C_NYILVANTARTASISZAM
			  ,C_NYILVANTARTASKEZDETE AS C_NYILVANTARTASKEZDETE
			  ,C_NYOMTATASINEV AS C_NYOMTATASINEV
			  ,C_OKTATASIAZONOSITO AS C_OKTATASIAZONOSITO
			  ,C_OMKOD AS C_OMKOD
			  ,C_PROFILKEP AS C_PROFILKEP
			  ,C_SMSSZAM AS C_SMSSZAM
			  ,C_SMSSZINKRONIZALT AS C_SMSSZINKRONIZALT
			  ,C_SZULETESIDATUM AS C_SZULETESIDATUM
			  ,C_SZULETESIHELY AS C_SZULETESIHELY
			  ,C_SZULETESINEV AS C_SZULETESINEV
			  ,C_SZULETESINEVELVALASZTO AS C_SZULETESINEVELVALASZTO
			  ,C_SZULETESINEVSORREND AS C_SZULETESINEVSORREND
			  ,C_SZULETESIORSZAG AS C_SZULETESIORSZAG
			  ,C_TAJSZAM AS C_TAJSZAM
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,C_UTONEV AS C_UTONEV
			  ,C_VEZETEKNEV AS C_VEZETEKNEV
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_FELHASZNALO_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_FELHASZNALO_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
				AND 0 <> (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE T_ALKALMAZOTT_OSSZES.ID = t.ID)

		INSERT INTO T_ALKALMAZOTT_OSSZES (
			 C_ALINTEZMENYID
			,C_ALLANDOHELYETTESITESRE
			,C_ALTANEVID
			,C_FOALLAS
			,C_TANARIAUTOMATAMEGAJANLASENGE
			,C_TANARIERTEKELESHATARERTEK
			,C_DIAKOLIMPIA
			,C_SZAKERTOIVIZSGAELNOKI
			,C_SZAKVIZSGA
			,C_TOVABBKEPZES
			,C_ORAKEGYHUZAMBAN
			,C_KOZOSSEGISZOLGALATKEZELO
			,C_ELFOGADOTTSZANDEKNYILATKOZAT
			,ID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDIDA
			) SELECT 
			  @intezmenyID AS C_ALINTEZMENYID
			  ,C_ALLANDOHELYETTESITESRE AS C_ALLANDOHELYETTESITESRE
			  ,@kovetkezoTanevId AS C_ALTANEVID
			  ,C_FOALLAS AS C_FOALLAS
			  ,C_TANARIAUTOMATAMEGAJANLASENGE AS C_TANARIAUTOMATAMEGAJANLASENGE
			  ,C_TANARIERTEKELESHATARERTEK AS C_TANARIERTEKELESHATARERTEK
			  ,C_DIAKOLIMPIA AS C_DIAKOLIMPIA
 			  ,C_SZAKERTOIVIZSGAELNOKI AS C_SZAKERTOIVIZSGAELNOKI
			  ,C_SZAKVIZSGA AS C_SZAKVIZSGA
			  ,C_TOVABBKEPZES AS C_TOVABBKEPZES
			  ,C_ORAKEGYHUZAMBAN AS C_ORAKEGYHUZAMBAN
			  ,C_KOZOSSEGISZOLGALATKEZELO AS C_KOZOSSEGISZOLGALATKEZELO
			  ,C_ELFOGADOTTSZANDEKNYILATKOZAT AS C_ELFOGADOTTSZANDEKNYILATKOZAT
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.ID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS ID
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDIDA
			FROM T_ALKALMAZOTT_OSSZES t WHERE C_ALTANEVID = @aktTanevId AND C_ALINTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND C_ALINTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDIDA = t.ID)

		INSERT INTO T_FELHASZNALOBELEPES_OSSZES (
			 C_BEJELENTKEZESINEV
			,C_FELHASZNALOID
			,C_INTEZMENYID
			,C_JELSZO
			,C_JELSZOVALTOZTATASIDEJE
			,C_KOTELEZOVALTOZTATNI
			,C_MEGHIUSULTBELEPESEK
			,C_NEPTUNNAPLOJELSZO
			,C_SO
			,C_TANEVID
			,C_UTOLSOBELEPES
			,C_GUID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  C_BEJELENTKEZESINEV AS C_BEJELENTKEZESINEV
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELHASZNALOID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELHASZNALOID
			  ,@intezmenyID AS C_INTEZMENYID
			  ,C_JELSZO AS C_JELSZO
			  ,C_JELSZOVALTOZTATASIDEJE AS C_JELSZOVALTOZTATASIDEJE
			  ,C_KOTELEZOVALTOZTATNI AS C_KOTELEZOVALTOZTATNI
			  ,C_MEGHIUSULTBELEPESEK AS C_MEGHIUSULTBELEPESEK
			  ,C_NEPTUNNAPLOJELSZO AS C_NEPTUNNAPLOJELSZO
			  ,C_SO AS C_SO
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,C_UTOLSOBELEPES AS C_UTOLSOBELEPES
			  ,C_GUID
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_FELHASZNALOBELEPES_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_FELHASZNALOBELEPES_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
				 AND 0 <> (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE T_ALKALMAZOTT_OSSZES.ID = t.C_FELHASZNALOID)

		INSERT INTO T_MUNKAUGYIADATOK_OSSZES (
			 C_ALKALMAZASKEZDETE
			,C_ALKALMAZASMEGSZUNESE
			,C_ALKALMAZOTTID
			,C_BESOROLASIFOKOZAT
			,C_BETOLTETLENALLASHELY
			,C_FELADATELLATASIHELYID
			,C_FOGLALKOZTATASTIPUS
			,C_INTEZMENYID
			,C_KOTELEZOORASZAM
			,C_MUNKAIDOKEDVEZMENYOKA
			,C_MUNKAIDOKEDVEZMENYOKASZOVEG
			,C_MUNKAIDOKEDVEZMENYORASZAM
			,C_MUNKAKORTIPUSA
			,C_MUNKAVISZONYTIPUSA
			,C_NYUGDIJAS
			,C_PEDAGOGUSSTATUSZA
			,C_RESZMUNKAIDOSZAZALEK
			,C_TANEVID
			,C_TARTOSHELYETTESITES
			,C_UTAZOGYOGYPEDAGOGUS
			,C_CSOKKENTETTMUNKAIDOS
			,C_SZAKERTOMESTERPEDAGOGUS
			,C_SZABADALLASHELY
			,C_VEZETOIORASZAMOK
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,ELOZOTANEVIREKORDID
			) SELECT 
			  C_ALKALMAZASKEZDETE AS C_ALKALMAZASKEZDETE
			  ,C_ALKALMAZASMEGSZUNESE AS C_ALKALMAZASMEGSZUNESE
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_ALKALMAZOTTID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_ALKALMAZOTTID
			  ,IIF((SELECT COUNT(1) FROM T_BESOROLASIFOKOZATTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_BESOROLASIFOKOZAT) = 0,3040,t.C_BESOROLASIFOKOZAT) AS C_BESOROLASIFOKOZAT
			  ,C_BETOLTETLENALLASHELY AS C_BETOLTETLENALLASHELY
			  ,(SELECT ID FROM T_FELADATELLATASIHELY_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELADATELLATASIHELYID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELADATELLATASIHELYID
			  ,IIF((SELECT COUNT(1) FROM T_FOGLALKOZTATASTIPUSA_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_FOGLALKOZTATASTIPUS) = 0,6465,t.C_FOGLALKOZTATASTIPUS) AS C_FOGLALKOZTATASTIPUS
			  ,@intezmenyID AS C_INTEZMENYID
			  ,C_KOTELEZOORASZAM AS C_KOTELEZOORASZAM
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAIDOKEDVEZMENYOKA_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAIDOKEDVEZMENYOKA) = 0,3073,t.C_MUNKAIDOKEDVEZMENYOKA) AS C_MUNKAIDOKEDVEZMENYOKA
			  ,C_MUNKAIDOKEDVEZMENYOKASZOVEG AS C_MUNKAIDOKEDVEZMENYOKASZOVEG
			  ,C_MUNKAIDOKEDVEZMENYORASZAM AS C_MUNKAIDOKEDVEZMENYORASZAM
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAKORTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAKORTIPUSA) = 0,548,t.C_MUNKAKORTIPUSA) AS C_MUNKAKORTIPUSA
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAVISZONYTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAVISZONYTIPUSA) = 0,643,t.C_MUNKAVISZONYTIPUSA) AS C_MUNKAVISZONYTIPUSA
			  ,C_NYUGDIJAS AS C_NYUGDIJAS
			  ,IIF((SELECT COUNT(1) FROM T_PEDAGOGUSSTATUSZ WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_PEDAGOGUSSTATUSZA) = 0,3032,t.C_PEDAGOGUSSTATUSZA) AS C_PEDAGOGUSSTATUSZA
			  ,C_RESZMUNKAIDOSZAZALEK AS C_RESZMUNKAIDOSZAZALEK
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,C_TARTOSHELYETTESITES AS C_TARTOSHELYETTESITES
			  ,C_UTAZOGYOGYPEDAGOGUS AS C_UTAZOGYOGYPEDAGOGUS
			  ,C_CSOKKENTETTMUNKAIDOS AS C_CSOKKENTETTMUNKAIDOS
			  ,C_SZAKERTOMESTERPEDAGOGUS AS C_SZAKERTOMESTERPEDAGOGUS
			  ,C_SZABADALLASHELY AS C_SZABADALLASHELY
			  ,C_VEZETOIORASZAMOK AS C_VEZETOIORASZAMOK
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_MUNKAUGYIADATOK_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_MUNKAUGYIADATOK_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)

		INSERT INTO T_CIM_OSSZES (
			 C_AJTO
			,C_ALAPERTELMEZETT
			,C_CIMTIPUSA
			,C_EMELET
			,C_EPULET
			,C_FELHASZNALOID
			,C_HAZSZAM
			,C_INTEZMENYID
			,C_IRANYITOSZAM
			,C_KOZTERULET
			,C_KOZTERULETJELLEGE
			,C_LEPCSOHAZ
			,C_MEGYE
			,C_ORSZAG
			,C_OSSZETETTCIM
			,C_TANEVID
			,C_VAROS
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  C_AJTO AS C_AJTO
			  ,C_ALAPERTELMEZETT AS C_ALAPERTELMEZETT
			  ,IIF((SELECT COUNT(1) FROM T_CIMTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_CIMTIPUSA) = 0,906,t.C_CIMTIPUSA) AS C_CIMTIPUSA
			  ,C_EMELET AS C_EMELET
			  ,C_EPULET AS C_EPULET
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELHASZNALOID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELHASZNALOID
			  ,C_HAZSZAM AS C_HAZSZAM
			  ,@intezmenyID AS C_INTEZMENYID
			  ,C_IRANYITOSZAM AS C_IRANYITOSZAM
			  ,C_KOZTERULET AS C_KOZTERULET
			  ,IIF((SELECT COUNT(1) FROM T_KOZTERULETJELLEG_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_KOZTERULETJELLEGE) = 0,911,t.C_KOZTERULETJELLEGE) AS C_KOZTERULETJELLEGE
			  ,C_LEPCSOHAZ AS C_LEPCSOHAZ
			  ,C_MEGYE AS C_MEGYE
			  ,C_ORSZAG AS C_ORSZAG
			  ,C_OSSZETETTCIM AS C_OSSZETETTCIM
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,C_VAROS AS C_VAROS
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_CIM_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_CIM_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
				 AND 0 <> (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE T_ALKALMAZOTT_OSSZES.ID = t.C_FELHASZNALOID)

		INSERT INTO T_EMAIL_OSSZES (
			C_EMAILTIPUSA
			,C_EMAILCIM
			,C_ALAPERTELMEZETT
			,C_FELHASZNALOID
			,C_ISPUBLIC
			,C_INTEZMENYID
			,C_TANEVID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  IIF((SELECT COUNT(1) FROM T_EMAILTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_EMAILTIPUSA) = 0,1024,t.C_EMAILTIPUSA) AS C_EMAILTIPUSA
			  ,C_EMAILCIM AS C_EMAILCIM
			  ,C_ALAPERTELMEZETT AS C_ALAPERTELMEZETT
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELHASZNALOID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELHASZNALOID
			  ,C_ISPUBLIC AS C_ISPUBLIC
			  ,@intezmenyID AS C_INTEZMENYID
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_EMAIL_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_EMAIL_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
				 AND 0 <> (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE T_ALKALMAZOTT_OSSZES.ID = t.C_FELHASZNALOID)

		INSERT INTO T_TELEFON_OSSZES (
			C_TELEFONTIPUSA
			,C_TELEFONSZAM
			,C_LEIRAS
			,C_ALAPERTELMEZETT
			,C_FELHASZNALOID
			,C_ISPUBLIC
			,C_INTEZMENYID
			,C_TANEVID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  IIF((SELECT COUNT(1) FROM T_TELEFONTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_TELEFONTIPUSA) = 0,1019,t.C_TELEFONTIPUSA) AS C_TELEFONTIPUSA
			  ,C_TELEFONSZAM AS C_TELEFONSZAM
			  ,C_LEIRAS AS C_LEIRAS
			  ,C_ALAPERTELMEZETT AS C_ALAPERTELMEZETT
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELHASZNALOID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELHASZNALOID
			  ,C_ISPUBLIC AS C_ISPUBLIC
			  ,@intezmenyID AS C_INTEZMENYID
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_TELEFON_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_TELEFON_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
				 AND 0 <> (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE T_ALKALMAZOTT_OSSZES.ID = t.C_FELHASZNALOID)

	END
ELSE
	BEGIN
		DECLARE @KovTanevAlkalmazottId INT;
			SET @KovTanevAlkalmazottId = (SELECT ID FROM T_ALKALMAZOTT_OSSZES WHERE ELOZOTANEVIREKORDIDA = @alkalmazottId AND C_ALTANEVID = @kovetkezoTanevId AND TOROLT = 'F')

		UPDATE kovetkezo SET
			kovetkezo.C_ADOSZAM = aktiv.C_ADOSZAM
			,kovetkezo.C_ALAIRAS = aktiv.C_ALAIRAS
			,kovetkezo.C_ALAPERTELMEZETTLCID = aktiv.C_ALAPERTELMEZETTLCID
			,kovetkezo.C_ALLAMPOLGARSAGA = IIF((SELECT COUNT(1) FROM T_ALLAMPOLGARSAG_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = aktiv.C_ALLAMPOLGARSAGA) = 0,216, aktiv.C_ALLAMPOLGARSAGA)
			,kovetkezo.C_ALLAMPOLGARSAGA2 = IIF((SELECT COUNT(1) FROM T_ALLAMPOLGARSAG_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = aktiv.C_ALLAMPOLGARSAGA2) = 0,216, aktiv.C_ALLAMPOLGARSAGA2)
			,kovetkezo.C_ANYANYELVE = IIF((SELECT COUNT(1) FROM T_ANYANYELV_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = aktiv.C_ANYANYELVE) = 0,493, aktiv.C_ANYANYELVE)
			,kovetkezo.C_ANYJANEVE = aktiv.C_ANYJANEVE
			,kovetkezo.C_ANYJANEVEELVALASZTO = aktiv.C_ANYJANEVEELVALASZTO
			,kovetkezo.C_ANYJANEVESORREND = aktiv.C_ANYJANEVESORREND
			,kovetkezo.C_DMSTARTTIME = aktiv.C_DMSTARTTIME
			,kovetkezo.C_ELOTAG = aktiv.C_ELOTAG
			,kovetkezo.C_FENNTARTOAZONOSITO = aktiv.C_FENNTARTOAZONOSITO
			,kovetkezo.C_IGAZOLVANYSZAM = aktiv.C_IGAZOLVANYSZAM
			,kovetkezo.C_IGAZOLVANYTIPUSA = IIF((SELECT COUNT(1) FROM T_IGAZOLVANYTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = aktiv.C_IGAZOLVANYTIPUSA) = 0,543, aktiv.C_IGAZOLVANYTIPUSA)
			,kovetkezo.C_KELLADSZINKRONIZALAS = aktiv.C_KELLADSZINKRONIZALAS
			,kovetkezo.C_KERESESINEV = aktiv.C_KERESESINEV
			,kovetkezo.C_KIRAZONOSITO = aktiv.C_KIRAZONOSITO
			,kovetkezo.C_KRETAKOD = aktiv.C_KRETAKOD
			,kovetkezo.C_LATASSERULTWEBHASZNALATA = aktiv.C_LATASSERULTWEBHASZNALATA
			,kovetkezo.C_LEIRAS = aktiv.C_LEIRAS
			,kovetkezo.C_NEME = aktiv.C_NEME
			,kovetkezo.C_NEVSORREND = aktiv.C_NEVSORREND
			,kovetkezo.C_NYILVANTARTASISZAM = aktiv.C_NYILVANTARTASISZAM
			,kovetkezo.C_NYILVANTARTASKEZDETE = aktiv.C_NYILVANTARTASKEZDETE
			,kovetkezo.C_NYOMTATASINEV = aktiv.C_NYOMTATASINEV
			,kovetkezo.C_OKTATASIAZONOSITO = aktiv.C_OKTATASIAZONOSITO
			,kovetkezo.C_OMKOD = aktiv.C_OMKOD
			,kovetkezo.C_PROFILKEP = aktiv.C_PROFILKEP
			,kovetkezo.C_SMSSZAM = aktiv.C_SMSSZAM
			,kovetkezo.C_SMSSZINKRONIZALT = aktiv.C_SMSSZINKRONIZALT
			,kovetkezo.C_SZULETESIDATUM = aktiv.C_SZULETESIDATUM
			,kovetkezo.C_SZULETESIHELY = aktiv.C_SZULETESIHELY
			,kovetkezo.C_SZULETESINEV = aktiv.C_SZULETESINEV
			,kovetkezo.C_SZULETESINEVELVALASZTO = aktiv.C_SZULETESINEVELVALASZTO
			,kovetkezo.C_SZULETESINEVSORREND = aktiv.C_SZULETESINEVSORREND
			,kovetkezo.C_SZULETESIORSZAG = aktiv.C_SZULETESIORSZAG
			,kovetkezo.C_TAJSZAM = aktiv.C_TAJSZAM
			,kovetkezo.C_UTONEV = aktiv.C_UTONEV
			,kovetkezo.C_VEZETEKNEV = aktiv.C_VEZETEKNEV
			,kovetkezo.TOROLT = aktiv.TOROLT
		FROM T_FELHASZNALO_OSSZES kovetkezo
		INNER JOIN T_FELHASZNALO_OSSZES aktiv ON aktiv.C_INTEZMENYID = kovetkezo.C_INTEZMENYID AND aktiv.C_TANEVID = @aktTanevId AND aktiv.ID = @alkalmazottId
		WHERE
			kovetkezo.C_INTEZMENYID = @intezmenyId AND kovetkezo.C_TANEVID = @kovetkezoTanevId AND kovetkezo.ELOZOTANEVIREKORDID = @alkalmazottId

		UPDATE kovetkezo SET
			kovetkezo.C_ALLANDOHELYETTESITESRE = aktiv.C_ALLANDOHELYETTESITESRE
			,kovetkezo.C_FOALLAS = aktiv.C_FOALLAS
			,kovetkezo.C_TANARIAUTOMATAMEGAJANLASENGE = aktiv.C_TANARIAUTOMATAMEGAJANLASENGE
			,kovetkezo.C_TANARIERTEKELESHATARERTEK = aktiv.C_TANARIERTEKELESHATARERTEK
			,kovetkezo.C_DIAKOLIMPIA = aktiv.C_DIAKOLIMPIA
			,kovetkezo.C_SZAKERTOIVIZSGAELNOKI = aktiv.C_SZAKERTOIVIZSGAELNOKI
			,kovetkezo.C_SZAKVIZSGA = aktiv.C_SZAKVIZSGA
			,kovetkezo.C_TOVABBKEPZES = aktiv.C_TOVABBKEPZES
			,kovetkezo.C_ORAKEGYHUZAMBAN = aktiv.C_ORAKEGYHUZAMBAN
			,kovetkezo.C_KOZOSSEGISZOLGALATKEZELO = aktiv.C_KOZOSSEGISZOLGALATKEZELO
			,kovetkezo.C_ELFOGADOTTSZANDEKNYILATKOZAT = aktiv.C_ELFOGADOTTSZANDEKNYILATKOZAT
			,kovetkezo.TOROLT = aktiv.TOROLT
		FROM T_ALKALMAZOTT_OSSZES kovetkezo
		INNER JOIN T_ALKALMAZOTT_OSSZES aktiv ON aktiv.C_ALINTEZMENYID = kovetkezo.C_ALINTEZMENYID AND aktiv.C_ALTANEVID = @aktTanevId AND aktiv.ID = @alkalmazottId
		WHERE
			kovetkezo.C_ALINTEZMENYID = @intezmenyId AND kovetkezo.C_ALTANEVID = @kovetkezoTanevId AND kovetkezo.ID = @KovTanevAlkalmazottId
		
		
		DELETE FROM T_CIM_OSSZES WHERE C_FELHASZNALOID = @KovTanevAlkalmazottId AND C_GONDVISELOID IS NULL
		DELETE FROM T_EMAIL_OSSZES WHERE C_FELHASZNALOID = @KovTanevAlkalmazottId AND C_GONDVISELOID IS NULL
		DELETE FROM T_TELEFON_OSSZES WHERE C_FELHASZNALOID = @KovTanevAlkalmazottId AND C_GONDVISELOID IS NULL
		DELETE FROM T_MUNKAUGYIADATOK_OSSZES WHERE C_ALKALMAZOTTID = @KovTanevAlkalmazottId
		DELETE FROM T_TOVABBIMUNKAUGYIADATOK WHERE C_ALKALMAZOTTID = @KovTanevAlkalmazottId
		DELETE FROM T_VEGZETTSEG_OSSZES WHERE C_TANARID = @KovTanevAlkalmazottId
		DELETE FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES WHERE C_TANARID = @KovTanevAlkalmazottId


		INSERT INTO T_CIM_OSSZES (
			 C_AJTO
			,C_ALAPERTELMEZETT
			,C_CIMTIPUSA
			,C_EMELET
			,C_EPULET
			,C_FELHASZNALOID
			,C_HAZSZAM
			,C_INTEZMENYID
			,C_IRANYITOSZAM
			,C_KOZTERULET
			,C_KOZTERULETJELLEGE
			,C_LEPCSOHAZ
			,C_MEGYE
			,C_ORSZAG
			,C_OSSZETETTCIM
			,C_TANEVID
			,C_VAROS
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  C_AJTO AS C_AJTO
			  ,C_ALAPERTELMEZETT AS C_ALAPERTELMEZETT
			  ,IIF((SELECT COUNT(1) FROM T_CIMTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_CIMTIPUSA) = 0,906,t.C_CIMTIPUSA) AS C_CIMTIPUSA
			  ,C_EMELET AS C_EMELET
			  ,C_EPULET AS C_EPULET
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELHASZNALOID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELHASZNALOID
			  ,C_HAZSZAM AS C_HAZSZAM
			  ,@intezmenyID AS C_INTEZMENYID
			  ,C_IRANYITOSZAM AS C_IRANYITOSZAM
			  ,C_KOZTERULET AS C_KOZTERULET
			  ,IIF((SELECT COUNT(1) FROM T_KOZTERULETJELLEG_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_KOZTERULETJELLEGE) = 0,911,t.C_KOZTERULETJELLEGE) AS C_KOZTERULETJELLEGE
			  ,C_LEPCSOHAZ AS C_LEPCSOHAZ
			  ,C_MEGYE AS C_MEGYE
			  ,C_ORSZAG AS C_ORSZAG
			  ,C_OSSZETETTCIM AS C_OSSZETETTCIM
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,C_VAROS AS C_VAROS
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_CIM_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_CIM_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
				 AND 0 <> (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE T_ALKALMAZOTT_OSSZES.ID = t.C_FELHASZNALOID)

		INSERT INTO T_EMAIL_OSSZES (
			C_EMAILTIPUSA
			,C_EMAILCIM
			,C_ALAPERTELMEZETT
			,C_FELHASZNALOID
			,C_ISPUBLIC
			,C_INTEZMENYID
			,C_TANEVID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  IIF((SELECT COUNT(1) FROM T_EMAILTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_EMAILTIPUSA) = 0,1024,t.C_EMAILTIPUSA) AS C_EMAILTIPUSA
			  ,C_EMAILCIM AS C_EMAILCIM
			  ,C_ALAPERTELMEZETT AS C_ALAPERTELMEZETT
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELHASZNALOID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELHASZNALOID
			  ,C_ISPUBLIC AS C_ISPUBLIC
			  ,@intezmenyID AS C_INTEZMENYID
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_EMAIL_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_EMAIL_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
				 AND 0 <> (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE T_ALKALMAZOTT_OSSZES.ID = t.C_FELHASZNALOID)

		INSERT INTO T_TELEFON_OSSZES (
			C_TELEFONTIPUSA
			,C_TELEFONSZAM
			,C_LEIRAS
			,C_ALAPERTELMEZETT
			,C_FELHASZNALOID
			,C_ISPUBLIC
			,C_INTEZMENYID
			,C_TANEVID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  IIF((SELECT COUNT(1) FROM T_TELEFONTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_TELEFONTIPUSA) = 0,1019,t.C_TELEFONTIPUSA) AS C_TELEFONTIPUSA
			  ,C_TELEFONSZAM AS C_TELEFONSZAM
			  ,C_LEIRAS AS C_LEIRAS
			  ,C_ALAPERTELMEZETT AS C_ALAPERTELMEZETT
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELHASZNALOID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELHASZNALOID
			  ,C_ISPUBLIC AS C_ISPUBLIC
			  ,@intezmenyID AS C_INTEZMENYID
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_TELEFON_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_TELEFON_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
				 AND 0 <> (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES WHERE T_ALKALMAZOTT_OSSZES.ID = t.C_FELHASZNALOID)

		INSERT INTO T_MUNKAUGYIADATOK_OSSZES (
			 C_ALKALMAZASKEZDETE
			,C_ALKALMAZASMEGSZUNESE
			,C_ALKALMAZOTTID
			,C_BESOROLASIFOKOZAT
			,C_BETOLTETLENALLASHELY
			,C_FELADATELLATASIHELYID
			,C_FOGLALKOZTATASTIPUS
			,C_INTEZMENYID
			,C_KOTELEZOORASZAM
			,C_MUNKAIDOKEDVEZMENYOKA
			,C_MUNKAIDOKEDVEZMENYOKASZOVEG
			,C_MUNKAIDOKEDVEZMENYORASZAM
			,C_MUNKAKORTIPUSA
			,C_MUNKAVISZONYTIPUSA
			,C_NYUGDIJAS
			,C_PEDAGOGUSSTATUSZA
			,C_RESZMUNKAIDOSZAZALEK
			,C_TANEVID
			,C_TARTOSHELYETTESITES
			,C_UTAZOGYOGYPEDAGOGUS
			,C_CSOKKENTETTMUNKAIDOS
			,C_SZAKERTOMESTERPEDAGOGUS
			,C_SZABADALLASHELY
			,C_VEZETOIORASZAMOK
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,ELOZOTANEVIREKORDID
			) SELECT 
			  C_ALKALMAZASKEZDETE AS C_ALKALMAZASKEZDETE
			  ,C_ALKALMAZASMEGSZUNESE AS C_ALKALMAZASMEGSZUNESE
			  ,@KovTanevAlkalmazottId AS C_ALKALMAZOTTID
			  ,IIF((SELECT COUNT(1) FROM T_BESOROLASIFOKOZATTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_BESOROLASIFOKOZAT) = 0,3040,t.C_BESOROLASIFOKOZAT) AS C_BESOROLASIFOKOZAT
			  ,C_BETOLTETLENALLASHELY AS C_BETOLTETLENALLASHELY
			  ,(SELECT ID FROM T_FELADATELLATASIHELY_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELADATELLATASIHELYID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELADATELLATASIHELYID
			  ,IIF((SELECT COUNT(1) FROM T_FOGLALKOZTATASTIPUSA_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_FOGLALKOZTATASTIPUS) = 0,6465,t.C_FOGLALKOZTATASTIPUS) AS C_FOGLALKOZTATASTIPUS
			  ,@intezmenyID AS C_INTEZMENYID
			  ,C_KOTELEZOORASZAM AS C_KOTELEZOORASZAM
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAIDOKEDVEZMENYOKA_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAIDOKEDVEZMENYOKA) = 0,3073,t.C_MUNKAIDOKEDVEZMENYOKA) AS C_MUNKAIDOKEDVEZMENYOKA
			  ,C_MUNKAIDOKEDVEZMENYOKASZOVEG AS C_MUNKAIDOKEDVEZMENYOKASZOVEG
			  ,C_MUNKAIDOKEDVEZMENYORASZAM AS C_MUNKAIDOKEDVEZMENYORASZAM
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAKORTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAKORTIPUSA) = 0,548,t.C_MUNKAKORTIPUSA) AS C_MUNKAKORTIPUSA
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAVISZONYTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAVISZONYTIPUSA) = 0,643,t.C_MUNKAVISZONYTIPUSA) AS C_MUNKAVISZONYTIPUSA
			  ,C_NYUGDIJAS AS C_NYUGDIJAS
			  ,IIF((SELECT COUNT(1) FROM T_PEDAGOGUSSTATUSZ WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_PEDAGOGUSSTATUSZA) = 0,3032,t.C_PEDAGOGUSSTATUSZA) AS C_PEDAGOGUSSTATUSZA
			  ,C_RESZMUNKAIDOSZAZALEK AS C_RESZMUNKAIDOSZAZALEK
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,C_TARTOSHELYETTESITES AS C_TARTOSHELYETTESITES
			  ,C_UTAZOGYOGYPEDAGOGUS AS C_UTAZOGYOGYPEDAGOGUS
			  ,C_CSOKKENTETTMUNKAIDOS AS C_CSOKKENTETTMUNKAIDOS
			  ,C_SZAKERTOMESTERPEDAGOGUS AS C_SZAKERTOMESTERPEDAGOGUS
			  ,C_SZABADALLASHELY AS C_SZABADALLASHELY
			  ,C_VEZETOIORASZAMOK AS C_VEZETOIORASZAMOK
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_MUNKAUGYIADATOK_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_MUNKAUGYIADATOK_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)

		INSERT INTO T_TOVABBIMUNKAUGYIADATOK_OSSZES (
			 C_ALKALMAZASKEZDETE
			,C_ALKALMAZASMEGSZUNESE
			,C_ALKALMAZOTTID
			,C_BESOROLASIFOKOZAT
			,C_FELADATELLATASIHELYID
			,C_FOGLALKOZTATASTIPUS
			,C_INTEZMENYID
			,C_KOTELEZOORASZAM
			,C_MUNKAIDOKEDVEZMENYOKA
			,C_MUNKAIDOKEDVEZMENYOKASZOVEG
			,C_MUNKAIDOKEDVEZMENYORASZAM
			,C_MUNKAKORTIPUSA
			,C_MUNKAVISZONYTIPUSA
			,C_NYUGDIJAS
			,C_RESZMUNKAIDOSZAZALEK
			,C_VEZETOIORASZAMOK 
			,C_TANEVID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,ELOZOTANEVIREKORDID
			) SELECT 
			  C_ALKALMAZASKEZDETE AS C_ALKALMAZASKEZDETE
			  ,C_ALKALMAZASMEGSZUNESE AS C_ALKALMAZASMEGSZUNESE
			  ,@KovTanevAlkalmazottId AS C_ALKALMAZOTTID
			  ,IIF((SELECT COUNT(1) FROM T_BESOROLASIFOKOZATTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_BESOROLASIFOKOZAT) = 0,3040,t.C_BESOROLASIFOKOZAT) AS C_BESOROLASIFOKOZAT
			  ,(SELECT ID FROM T_FELADATELLATASIHELY_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FELADATELLATASIHELYID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_FELADATELLATASIHELYID
			  ,IIF((SELECT COUNT(1) FROM T_FOGLALKOZTATASTIPUSA_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_FOGLALKOZTATASTIPUS) = 0,6465,t.C_FOGLALKOZTATASTIPUS) AS C_FOGLALKOZTATASTIPUS
			  ,@intezmenyID AS C_INTEZMENYID
			  ,C_KOTELEZOORASZAM AS C_KOTELEZOORASZAM
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAIDOKEDVEZMENYOKA_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAIDOKEDVEZMENYOKA) = 0,3073,t.C_MUNKAIDOKEDVEZMENYOKA) AS C_MUNKAIDOKEDVEZMENYOKA
			  ,C_MUNKAIDOKEDVEZMENYOKASZOVEG AS C_MUNKAIDOKEDVEZMENYOKASZOVEG
			  ,C_MUNKAIDOKEDVEZMENYORASZAM AS C_MUNKAIDOKEDVEZMENYORASZAM
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAKORTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAKORTIPUSA) = 0,548,t.C_MUNKAKORTIPUSA) AS C_MUNKAKORTIPUSA
			  ,IIF((SELECT COUNT(1) FROM T_MUNKAVISZONYTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_MUNKAVISZONYTIPUSA) = 0,643,t.C_MUNKAVISZONYTIPUSA) AS C_MUNKAVISZONYTIPUSA
			  ,C_NYUGDIJAS AS C_NYUGDIJAS
			  ,C_RESZMUNKAIDOSZAZALEK AS C_RESZMUNKAIDOSZAZALEK
			  ,C_VEZETOIORASZAMOK AS C_VEZETOIORASZAMOK 
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_TOVABBIMUNKAUGYIADATOK_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_TOVABBIMUNKAUGYIADATOK_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)

		INSERT INTO T_PEDAGOGUSELETPALYAMODELL_OSSZES (
			C_FOKOZAT
			,C_MINOSITESDATUM
			,C_PORTFOLIOSTATUSZ
			,C_TANARID
			,C_INTEZMENYID
			,C_TANEVID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  IIF((SELECT COUNT(1) FROM T_ELETPALYAMODELLFOKOZATTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_FOKOZAT) = 0,6462,t.C_FOKOZAT) AS C_FOKOZAT
			  ,C_MINOSITESDATUM AS C_MINOSITESDATUM
			  ,IIF((SELECT COUNT(1) FROM T_ELETPALYAMODELLSTATUSZOK_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_FOKOZAT) = 0,6521,t.C_PORTFOLIOSTATUSZ) AS C_PORTFOLIOSTATUSZ
			  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_TANARID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_TANARID
			  ,@intezmenyID AS C_INTEZMENYID
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,TOROLT AS TOROLT
			  ,0 AS SERIAL
			  ,GETDATE() AS LASTCHANGED
			  ,GETDATE() AS CREATED
			  ,MODIFIER AS MODIFIER
			  ,MODIFIER AS CREATOR
			  ,NULL AS NNID
			  ,t.ID AS ELOZOTANEVIREKORDID
			FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
		
		INSERT INTO T_VEGZETTSEG_OSSZES (
			C_EGYEB
			,C_VEGZETTSEGTIPUSID
			,C_TANARID
			,C_INTEZMENYID
			,C_TANEVID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
				C_EGYEB AS C_EGYEB
				,IIF((SELECT COUNT(1) FROM T_VEGZETTSEGTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_VEGZETTSEGTIPUSID) = 0,6855,t.C_VEGZETTSEGTIPUSID) AS C_VEGZETTSEGTIPUSID
				,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_TANARID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_TANARID
				,@intezmenyID AS C_INTEZMENYID
				,@kovetkezoTanevId AS C_TANEVID
				,TOROLT AS TOROLT
				,0 AS SERIAL
				,GETDATE() AS LASTCHANGED
				,GETDATE() AS CREATED
				,MODIFIER AS MODIFIER
				,MODIFIER AS CREATOR
				,NULL AS NNID
				,t.ID AS ELOZOTANEVIREKORDID
			FROM T_VEGZETTSEG_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_VEGZETTSEG_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)


	END
END
GO
