﻿DROP INDEX IF EXISTS T_TANTERV.NCU_Tanterv_Vedett_TanevId_IntezmenyId
CREATE UNIQUE INDEX NCU_Tanterv_Vedett_TanevId_IntezmenyId
ON T_TANTERV (
   C_VEDETT
  ,C_TANEVID
  ,C_INTEZMENYID)
WHERE TOROLT='F' AND C_VEDETT='T'
WITH (FILLFACTOR = 80)
GO

UPDATE tr SET C_VEDETT = 'T'
FROM T_TANTERV tr
  INNER JOIN T_TANEV tn ON tn.ID=tr.C_TANEVID AND tn.TOROLT='F' 
WHERE tr.TOROLT='F' AND tr.C_NEV = 'Kerettanterv'

INSERT INTO T_TANTERV (
   C_CSOPORTTIPUSA
  ,C_EVFOLYAMIG
  ,C_EVFOLYAMTOL
  ,C_KERETTANTERVREEPULO
  ,C_NEV
  ,C_INTEZMENYID
  ,C_TANEVID
  ,TOROLT
  ,SERIAL
  ,LASTCHANGED
  ,CREATED
  ,MODIFIER
  ,CREATOR
  ,C_VEDETT
) SELECT
   (SELECT ID FROM T_DICTIONARYITEMBASE WHERE C_TYPE = 'CsoportTipus' AND C_NAME = 'Na' AND TOROLT='F' AND C_TANEVID = tn.ID)
  ,(SELECT ID FROM T_DICTIONARYITEMBASE WHERE C_TYPE = 'EvfolyamTipus' AND C_NAME = 'Na' AND TOROLT='F' AND C_TANEVID = tn.ID)
  ,(SELECT ID FROM T_DICTIONARYITEMBASE WHERE C_TYPE = 'EvfolyamTipus' AND C_NAME = 'Na' AND TOROLT='F' AND C_TANEVID = tn.ID)
  ,'T'
  ,'Kerettanterv'
  ,tn.C_INTEZMENYID
  ,tn.ID
  ,'F'
  ,0
  ,GETDATE()
  ,GETDATE()
  ,NULL
  ,1
  ,'T'
FROM T_TANEV tn
  INNER JOIN T_INTEZMENY i ON i.ID = tn.C_INTEZMENYID AND i.TOROLT='F'
WHERE NOT EXISTS (SELECT 1 FROM T_TANTERV WHERE TOROLT='F' AND C_TANEVID = tn.ID AND C_VEDETT='T')
  AND tn.TOROLT='F'