﻿DECLARE @sql nvarchar(max)   
SET @sql = N''
IF NOT EXISTS (SELECT 1 FROM VersionInfo WHERE [Description] = 'KRETEA2_5518')
  BEGIN
    SET @sql += N'
update ia SET ia.C_KOZTERULETJELLEGENEV = d.C_NAME
from T_INTEZMENYADATOK ia
  inner join T_DICTIONARYITEMBASE d on d.id = C_KOZTERULETJELLEG and ia.C_TANEVID = d.C_TANEVID and d.TOROLT = ''F''
  inner join T_INTEZMENY i ON i.ID = ia.C_INTEZMENYID AND i.TOROLT = ''F''
  inner join T_TANEV tv ON tv.ID = ia.C_TANEVID AND tv.TOROLT = ''F''
where ia.C_KOZTERULETJELLEGENEV <> d.C_NAME
  and ia.TOROLT = ''F''

update mh SET mh.C_KOZTERULETJELLEGENEV = d.C_NAME
from T_MUKODESIHELY mh
  inner join T_DICTIONARYITEMBASE d on d.id = mh.C_KOZTERULETJELLEG and mh.C_TANEVID = d.C_TANEVID and d.TOROLT = ''F''
  inner join T_INTEZMENY i ON i.ID = mh.C_INTEZMENYID AND i.TOROLT = ''F''
  inner join T_TANEV tv ON tv.ID = mh.C_TANEVID AND tv.TOROLT = ''F''
where mh.C_KOZTERULETJELLEGENEV <> d.C_NAME
  and mh.TOROLT = ''F''

update c SET c.C_KOZTERULETJELLEGENEV = d.C_NAME
from t_cim c
  inner join T_DICTIONARYITEMBASE d on d.id = c.C_KOZTERULETJELLEGE and c.C_TANEVID = d.C_TANEVID and d.TOROLT = ''F''
  inner join T_INTEZMENY i ON i.ID = c.C_INTEZMENYID AND i.TOROLT = ''F''
  inner join T_TANEV tv ON tv.ID = c.C_TANEVID AND tv.TOROLT = ''F''
where c.C_KOZTERULETJELLEGENEV <> d.C_NAME
  and c.TOROLT = ''F''

update n SET 
  n.C_KOZTERULETJELLEGENEV = d.C_NAME
  ,n.C_THKOZTERULETJELLEGENEV = dth.C_NAME
  ,n.C_TKKOZTERULETJELLEGENEV = dtk.C_NAME
  ,n.C_TKKOZTERULETJELLEGENEV2 = dtk2.C_NAME
from T_NEBULO n
  inner join T_DICTIONARYITEMBASE d on d.id = n.C_KOZTERULETJELLEGE and n.C_TANEVID = d.C_TANEVID and d.TOROLT = ''F''
  inner join T_DICTIONARYITEMBASE dth on dth.id = n.C_THKOZTERULETJELLEGE and n.C_TANEVID = dth.C_TANEVID and dth.TOROLT = ''F''
  inner join T_DICTIONARYITEMBASE dtk on dtk.id = n.C_TKKOZTERULETJELLEGE and n.C_TANEVID = dtk.C_TANEVID and dtk.TOROLT = ''F''
  inner join T_DICTIONARYITEMBASE dtk2 on dtk2.id = n.C_TKKOZTERULETJELLEGE2 and n.C_TANEVID = dtk2.C_TANEVID and dtk2.TOROLT = ''F''
  inner join T_INTEZMENY i ON i.ID = n.C_INTEZMENYID AND i.TOROLT = ''F''
  inner join T_TANEV tv ON tv.ID = n.C_TANEVID AND tv.TOROLT = ''F''
where n.C_KOZTERULETJELLEGENEV <> d.C_NAME
  and n.TOROLT = ''F'''
  END
EXEC sp_executesql @sql