﻿UPDATE o SET C_NEV=C_NEV+' ('+CAST(rn as varchar(4))+')' FROM
T_ORATERV o
  INNER JOIN(
SELECT ID,rn FROM (
SELECT
   ROW_NUMBER() OVER (PARTITION BY o.C_NEV,o.C_TANEVID ORDER BY o.CREATED) rn
  ,o.ID
FROM T_ORATERV o
  INNER JOIN(
    SELECT 
       COUNT(*) db
      ,C_NEV
      ,C_TANEVID 
    FROM T_ORATERV
    WHERE TOROLT='F'
    GROUP BY C_NEV,C_TANEVID
    HAVING COUNT(*)>1) x ON x.C_NEV=o.C_NEV AND x.C_TANEVID=o.C_TANEVID AND o.TOROLT='F') y
    WHERE y.rn>1) tmp ON tmp.ID=o.ID
    
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'NCU_Oraterv_IntezmenyId_TanevId_Nev') BEGIN
  CREATE UNIQUE NONCLUSTERED INDEX NCU_Oraterv_IntezmenyId_TanevId_Nev
    ON dbo.T_ORATERV (C_INTEZMENYID , C_TANEVID , C_NEV)
  WHERE TOROLT='F'
  WITH (FILLFACTOR = 80)
END