﻿-------------------------------------------
--Csak egy alapértelmezett email cím legyen
-------------------------------------------
;WITH CTE AS (
    SELECT *, RANK() OVER (PARTITION BY C_FELHASZNALOID, C_TANEVID,C_GONDVISELOID ORDER BY ID DESC) EMAIL_RANK
    FROM T_EMAIL E        
    WHERE C_ALAPERTELMEZETT = 'T'
    AND TOROLT = 'F'
)
UPDATE CTE 
SET  C_ALAPERTELMEZETT = 'F'
    ,MODIFIER = 0
    ,LASTCHANGED = GETDATE()
    ,SERIAL += 1
WHERE EMAIL_RANK <> 1

-------------------------------------------
--Csak egy alapértelmezett lakcím legyen
-------------------------------------------
;WITH CTE AS (
    SELECT *, RANK() OVER (PARTITION BY C_FELHASZNALOID, C_TANEVID,C_GONDVISELOID ORDER BY ID DESC) CIM_RANK
    FROM T_CIM E        
    WHERE C_ALAPERTELMEZETT = 'T'
    AND TOROLT = 'F'
)
UPDATE CTE 
SET  C_ALAPERTELMEZETT = 'F'
    ,MODIFIER = 0
    ,LASTCHANGED = GETDATE()
    ,SERIAL += 1
WHERE CIM_RANK <> 1

-------------------------------------------
--Csak egy alapértelmezett telefon legyen
-------------------------------------------
;WITH CTE AS (
    SELECT *, RANK() OVER (PARTITION BY C_FELHASZNALOID, C_TANEVID,C_GONDVISELOID ORDER BY ID DESC) TELEFON_RANK
    FROM T_TELEFON E        
    WHERE C_ALAPERTELMEZETT = 'T'
    AND TOROLT = 'F'
)
UPDATE CTE 
SET  C_ALAPERTELMEZETT = 'F'
    ,MODIFIER = 0
    ,LASTCHANGED = GETDATE()
    ,SERIAL += 1
WHERE TELEFON_RANK <> 1