﻿DROP PROCEDURE IF EXISTS sp_FollowUpAlkalmazottVegzettsegKK
GO

CREATE PROCEDURE sp_FollowUpAlkalmazottVegzettsegKK
  @intezmenyId int,
  @aktTanevId int,
  @kovetkezoTanevId int,
  @alkalmazottId int
 
AS
BEGIN
	DECLARE @kovAlkalmazottId int
  
  SELECT @kovAlkalmazottId = ID 
  FROM T_FELHASZNALO_OSSZES fh
  WHERE fh.ELOZOTANEVIREKORDID = @alkalmazottId
  
  IF @kovAlkalmazottId IS NULL BEGIN
    RETURN
  END
  
  INSERT INTO T_KKTANITOVEZGETTSEG_OSSZES (
     C_KKTANIOTOMUVTERTIPUSID
    ,C_ALKALMAZOTTID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     x.C_KKTANIOTOMUVTERTIPUSID AS C_KKTANIOTOMUVTERTIPUSID -- C_KKTANIOTOMUVTERTIPUSID - int
    ,@kovAlkalmazottId   AS C_ALKALMAZOTTID         -- C_ALKALMAZOTTID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,0   AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKTANITOVEZGETTSEG_OSSZES x
  WHERE C_ALKALMAZOTTID = @alkalmazottId 
    AND x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKTANITOVEZGETTSEG_OSSZES s WHERE s.ELOZOTANEVIREKORDID = x.ID)

	UPDATE kvNew SET 
		 TOROLT = kvOld.TOROLT
		,LASTCHANGED = kvOld.LASTCHANGED
		,MODIFIER = kvOld.MODIFIER
		,SERIAL += 1
	FROM T_KKTANITOVEZGETTSEG_OSSZES kvOld
		INNER JOIN T_KKTANITOVEZGETTSEG_OSSZES kvNew ON kvNew.ELOZOTANEVIREKORDID=kvOld.ID
	WHERE kvOld.C_ALKALMAZOTTID=@alkalmazottId


  INSERT INTO T_KKGYOGYPEDVEGZETTSEG_OSSZES (
     C_KKGYOGYPEDTERTIPUSID
    ,C_ALKALMAZOTTID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     x.C_KKGYOGYPEDTERTIPUSID AS C_KKGYOGYPEDTERTIPUSID -- C_KKGYOGYPEDTERTIPUSID - int
    ,@kovAlkalmazottId   AS C_ALKALMAZOTTID         -- C_ALKALMAZOTTID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,'F'  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKGYOGYPEDVEGZETTSEG_OSSZES x
  WHERE C_ALKALMAZOTTID = @alkalmazottId
    AND x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKGYOGYPEDVEGZETTSEG_OSSZES s WHERE ELOZOTANEVIREKORDID = x.ID)

	  
  UPDATE kvNew SET 
		 TOROLT = kvOld.TOROLT
		,LASTCHANGED = kvOld.LASTCHANGED
		,MODIFIER = kvOld.MODIFIER
		,SERIAL += 1
  FROM T_KKGYOGYPEDVEGZETTSEG_OSSZES kvOld
	INNER JOIN T_KKGYOGYPEDVEGZETTSEG_OSSZES kvNew ON kvNew.ELOZOTANEVIREKORDID=kvOld.ID
  WHERE kvOld.C_ALKALMAZOTTID=@alkalmazottId

  INSERT INTO T_KKTANARVEGZETTSEG_OSSZES (
     C_EGYEB
    ,C_KKTANARVEGZETTSEGID
    ,C_ALKALMAZOTTID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT
     x.C_EGYEB AS C_EGYEB -- C_EGYEB - nvarchar(max)
    ,x.C_KKTANARVEGZETTSEGID AS C_KKTANARVEGZETTSEGID -- C_KKTANARVEGZETTSEGID - int
    ,@kovAlkalmazottId   AS C_ALKALMAZOTTID         -- C_ALKALMAZOTTID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,'F'  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKTANARVEGZETTSEG_OSSZES x
  WHERE C_ALKALMAZOTTID = @alkalmazottId
    AND x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKTANARVEGZETTSEG_OSSZES s WHERE s.ELOZOTANEVIREKORDID=x.ID)
	  
  UPDATE x SET 
     x.C_EGYEB = s.C_EGYEB
    ,x.MODIFIER = s.MODIFIER
	,x.TOROLT = s.TOROLT
    ,x.C_KKTANARVEGZETTSEGID = s.C_KKTANARVEGZETTSEGID
	,x.LASTCHANGED = s.LASTCHANGED
	,x.SERIAL += 1
  FROM T_KKTANARVEGZETTSEG_OSSZES x
    INNER JOIN T_KKTANARVEGZETTSEG_OSSZES s ON x.ELOZOTANEVIREKORDID = s.ID AND s.C_ALKALMAZOTTID = @alkalmazottId

  INSERT INTO T_KKTANTARGYKATEGORIA_OSSZES (
     C_KKSZAKTIPUSID
    ,C_KKTANARVEGZETTSEGID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     x.C_KKSZAKTIPUSID AS C_KKSZAKTIPUSID        -- C_KKSZAKTIPUSID - int
    ,tvs.ID   AS C_KKTANARVEGZETTSEGID   -- C_KKTANARVEGZETTSEGID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,0 AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKTANTARGYKATEGORIA_OSSZES x
    INNER JOIN T_KKTANARVEGZETTSEG_OSSZES tvx ON x.C_KKTANARVEGZETTSEGID = tvx.ID AND tvx.C_ALKALMAZOTTID = @alkalmazottId AND tvx.TOROLT = 'F'
    INNER JOIN T_KKTANARVEGZETTSEG_OSSZES tvs ON tvs.ELOZOTANEVIREKORDID = tvx.ID AND tvs.C_ALKALMAZOTTID = @kovAlkalmazottId  AND tvs.TOROLT = 'F'
  WHERE x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKTANTARGYKATEGORIA_OSSZES s WHERE s.ELOZOTANEVIREKORDID = x.ID)
   
  UPDATE tgyktNew SET 
     C_KKSZAKTIPUSID = tgykt.C_KKSZAKTIPUSID
    ,MODIFIER = tgykt.MODIFIER
	,TOROLT = tgykt.TOROLT
	,LASTCHANGED = tgykt.LASTCHANGED
	,SERIAL += 1
  FROM T_KKTANARVEGZETTSEG_OSSZES x
    INNER JOIN T_KKTANTARGYKATEGORIA_OSSZES tgykt ON tgykt.C_KKTANARVEGZETTSEGID = x.ID
	INNER JOIN T_KKTANTARGYKATEGORIA_OSSZES tgyktNew ON tgyktNew.ELOZOTANEVIREKORDID = tgykt.ID
  WHERE x.C_ALKALMAZOTTID = @alkalmazottId 
    

  INSERT INTO T_KKAMIVEGZETTSEG_OSSZES (
     C_KKAMIVEGZETTSEGTIPUSID
    ,C_ALKALMAZOTTID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     x.C_KKAMIVEGZETTSEGTIPUSID AS C_KKAMIVEGZETTSEGTIPUSID -- C_KKAMIVEGZETTSEGTIPUSID - int
    ,@kovAlkalmazottId   AS C_ALKALMAZOTTID         -- C_ALKALMAZOTTID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,0 AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKAMIVEGZETTSEG_OSSZES x
  WHERE C_ALKALMAZOTTID = @alkalmazottId
    AND x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKAMIVEGZETTSEG_OSSZES s WHERE ELOZOTANEVIREKORDID = x.ID)
  
  	  
  UPDATE kvNew SET 
  	 TOROLT = kvOld.TOROLT
  	,C_KKAMIVEGZETTSEGTIPUSID = kvOld.C_KKAMIVEGZETTSEGTIPUSID
	,MODIFIER = kvOld.MODIFIER
	,LASTCHANGED = kvOld.LASTCHANGED
	,SERIAL += 1
  FROM T_KKAMIVEGZETTSEG_OSSZES kvOld
  	INNER JOIN T_KKAMIVEGZETTSEG_OSSZES kvNew ON kvNew.ELOZOTANEVIREKORDID=kvOld.ID
  WHERE kvOld.C_ALKALMAZOTTID=@alkalmazottId
  
  INSERT INTO T_KKELEKTROAKUZENE_OSSZES (
     C_ELEKTRAKUTIPUSID
    ,C_KKAMIVEGZETTSEGID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     x.C_ELEKTRAKUTIPUSID AS C_ELEKTRAKUTIPUSID        -- C_ELEKTRAKUTIPUSID - int
    ,tvs.ID   AS C_KKAMIVEGZETTSEGID   -- C_KKAMIVEGZETTSEGID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,0 AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKELEKTROAKUZENE_OSSZES x
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES tvx ON x.C_KKAMIVEGZETTSEGID = tvx.ID AND tvx.C_ALKALMAZOTTID = @alkalmazottId AND tvx.TOROLT = 'F'
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES tvs ON tvs.ELOZOTANEVIREKORDID = tvx.ID AND tvs.C_ALKALMAZOTTID = @kovAlkalmazottId  AND tvs.TOROLT = 'F'
  WHERE x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKELEKTROAKUZENE_OSSZES s WHERE ELOZOTANEVIREKORDID = x.ID)
  
   UPDATE new SET
	 C_ELEKTRAKUTIPUSID = old.C_ELEKTRAKUTIPUSID
    ,TOROLT = old.TOROLT
	,MODIFIER = old.MODIFIER
	,LASTCHANGED = old.LASTCHANGED
	,SERIAL += 1
  FROM T_KKELEKTROAKUZENE_OSSZES old
	INNER JOIN T_KKELEKTROAKUZENE_OSSZES new ON old.ID=new.ELOZOTANEVIREKORDID
	INNER JOIN T_KKAMIVEGZETTSEG_OSSZES v ON v.ID=old.C_KKAMIVEGZETTSEGID AND v.C_ALKALMAZOTTID=@alkalmazottId
  

  INSERT INTO T_KKKLASSZIKUSZENE_OSSZES (
     C_KLASSZUKUSZENETIPUSID
    ,C_KKAMIVEGZETTSEGID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     x.C_KLASSZUKUSZENETIPUSID AS C_KLASSZUKUSZENETIPUSID        -- C_KLASSZUKUSZENETIPUSID - int
    ,tvs.ID   AS C_KKAMIVEGZETTSEGID   -- C_KKAMIVEGZETTSEGID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,0 AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKKLASSZIKUSZENE_OSSZES x
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES tvx ON x.C_KKAMIVEGZETTSEGID = tvx.ID AND tvx.C_ALKALMAZOTTID = @alkalmazottId AND tvx.TOROLT = 'F'
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES tvs ON tvs.ELOZOTANEVIREKORDID = tvx.ID AND tvs.C_ALKALMAZOTTID = @kovAlkalmazottId  AND tvs.TOROLT = 'F'
  WHERE x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKKLASSZIKUSZENE_OSSZES s WHERE ELOZOTANEVIREKORDID = x.ID)
	
  UPDATE new SET
	 C_KLASSZUKUSZENETIPUSID = old.C_KLASSZUKUSZENETIPUSID
    ,TOROLT = old.TOROLT
	,MODIFIER = old.MODIFIER
	,LASTCHANGED = old.LASTCHANGED
	,SERIAL += 1
  FROM T_KKKLASSZIKUSZENE_OSSZES old
	INNER JOIN T_KKKLASSZIKUSZENE_OSSZES new ON old.ID=new.ELOZOTANEVIREKORDID
	INNER JOIN T_KKAMIVEGZETTSEG_OSSZES v ON v.ID=old.C_KKAMIVEGZETTSEGID AND v.C_ALKALMAZOTTID=@alkalmazottId

  INSERT INTO T_KKNEPZENE_OSSZES (
     C_NEPZENETIPUSID
    ,C_KKAMIVEGZETTSEGID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     x.C_NEPZENETIPUSID AS C_NEPZENETIPUSID        -- C_NEPZENETIPUSID - int
    ,tvs.ID   AS C_KKAMIVEGZETTSEGID   -- C_KKAMIVEGZETTSEGID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,0 AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKNEPZENE_OSSZES x
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES tvx ON x.C_KKAMIVEGZETTSEGID = tvx.ID AND tvx.C_ALKALMAZOTTID = @alkalmazottId AND tvx.TOROLT = 'F'
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES tvs ON tvs.ELOZOTANEVIREKORDID = tvx.ID AND tvs.C_ALKALMAZOTTID = @kovAlkalmazottId  AND tvs.TOROLT = 'F'
  WHERE x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKNEPZENE_OSSZES s WHERE ELOZOTANEVIREKORDID = x.ID)

  UPDATE new SET
	 C_NEPZENETIPUSID = old.C_NEPZENETIPUSID
    ,TOROLT = old.TOROLT
	,MODIFIER = old.MODIFIER
	,LASTCHANGED = old.LASTCHANGED
	,SERIAL += 1
  FROM T_KKNEPZENE_OSSZES old
	INNER JOIN T_KKNEPZENE_OSSZES new ON old.ID=new.ELOZOTANEVIREKORDID
	INNER JOIN T_KKAMIVEGZETTSEG_OSSZES v ON v.ID=old.C_KKAMIVEGZETTSEGID AND v.C_ALKALMAZOTTID=@alkalmazottId

  INSERT INTO T_KKJAZZZENE_OSSZES (
     C_JAZZZENETIPUSID
    ,C_KKAMIVEGZETTSEGID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     x.C_JAZZZENETIPUSID AS C_JAZZZENETIPUSID        -- C_JAZZZENETIPUSID - int
    ,tvs.ID   AS C_KKAMIVEGZETTSEGID   -- C_KKAMIVEGZETTSEGID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@kovetkezoTanevId   AS C_TANEVID               -- C_TANEVID - int
    ,0 AS SERIAL                  -- SERIAL - int
    ,x.LASTCHANGED AS LASTCHANGED       -- LASTCHANGED - datetime
    ,x.LASTCHANGED AS CREATED           -- CREATED - datetime
    ,x.MODIFIER AS MODIFIER               -- MODIFIER - int
    ,x.MODIFIER AS CREATOR                -- CREATOR - int
    ,x.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_KKJAZZZENE_OSSZES x
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES tvx ON x.C_KKAMIVEGZETTSEGID = tvx.ID AND tvx.C_ALKALMAZOTTID = @alkalmazottId AND tvx.TOROLT = 'F'
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES tvs ON tvs.ELOZOTANEVIREKORDID = tvx.ID AND tvs.C_ALKALMAZOTTID = @kovAlkalmazottId  AND tvs.TOROLT = 'F'
  WHERE x.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KKJAZZZENE_OSSZES s WHERE ELOZOTANEVIREKORDID = x.ID)

   UPDATE new SET
	 C_JAZZZENETIPUSID = old.C_JAZZZENETIPUSID
    ,TOROLT = old.TOROLT
	,MODIFIER = old.MODIFIER
	,LASTCHANGED = old.LASTCHANGED
	,SERIAL += 1
  FROM T_KKJAZZZENE_OSSZES old
	INNER JOIN T_KKJAZZZENE_OSSZES new ON old.ID=new.ELOZOTANEVIREKORDID
	INNER JOIN T_KKAMIVEGZETTSEG_OSSZES v ON v.ID=old.C_KKAMIVEGZETTSEGID AND v.C_ALKALMAZOTTID=@alkalmazottId

  INSERT INTO T_KKTERULET_OSSZES(
     C_AMITERULETTIPUSID
    ,C_KKAMIVEGZETTSEGID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  )
  SELECT
     ktOld.C_AMITERULETTIPUSID
    ,vNew.ID
    ,vOld.C_INTEZMENYID
    ,vNew.C_TANEVID
    ,0
    ,vOld.LASTCHANGED
    ,vOld.LASTCHANGED
    ,vOld.MODIFIER
    ,vOld.CREATOR
    ,ktOld.ID
  FROM T_KKAMIVEGZETTSEG_OSSZES vOld
    INNER JOIN T_KKAMIVEGZETTSEG_OSSZES vNew ON vNew.ELOZOTANEVIREKORDID=vOld.ID AND vNew.TOROLT='F'
    INNER JOIN T_KKTERULET_OSSZES ktOld ON ktOld.C_KKAMIVEGZETTSEGID=vOld.ID AND ktOld.TOROLT='F' 
  WHERE vOld.C_ALKALMAZOTTID=@alkalmazottId AND vOld.TOROLT='F' 
  	AND NOT EXISTS(SELECT 1 FROM T_KKTERULET_OSSZES WHERE ELOZOTANEVIREKORDID = ktOld.ID)

  UPDATE new SET
	 C_AMITERULETTIPUSID = old.C_AMITERULETTIPUSID
    ,TOROLT = old.TOROLT
	,MODIFIER = old.MODIFIER
	,LASTCHANGED = old.LASTCHANGED
	,SERIAL += 1
  FROM T_KKTERULET_OSSZES old
	INNER JOIN T_KKTERULET_OSSZES new ON old.ID=new.ELOZOTANEVIREKORDID
	INNER JOIN T_KKAMIVEGZETTSEG_OSSZES v ON v.ID=old.C_KKAMIVEGZETTSEGID AND v.C_ALKALMAZOTTID=@alkalmazottId
  


END
GO