﻿GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO
PRINT N'Dropping [dbo].[FK_4028294_4028295]...';


GO
ALTER TABLE [dbo].[T_KEPESITES] DROP CONSTRAINT [FK_4028294_4028295];


GO


UPDATE k SET
  k.C_KEPESITESTIPUSID = szak.ID
FROM T_KEPESITES k
  INNER JOIN (SELECT DISTINCT ID, C_NAME FROM T_DICTIONARYITEMBASE WHERE C_TYPE = 'TargyKategoriaTipus') targy ON targy.ID = k.C_KEPESITESTIPUSID
  INNER JOIN (SELECT DISTINCT ID, C_NAME FROM T_DICTIONARYITEMBASE WHERE C_TYPE = 'SzakTipus') szak ON szak.C_NAME = targy.C_NAME


DELETE FROM T_KEPESITES WHERE C_KEPESITESTIPUSID NOT IN (SELECT DISTINCT ID FROM T_DICTIONARYITEMBASE WHERE C_TYPE = 'SzakTipus')


GO
PRINT N'Creating [dbo].[FK_4028294_4028295]...';


GO
ALTER TABLE [dbo].[T_KEPESITES] WITH NOCHECK
    ADD CONSTRAINT [FK_4028294_4028295] FOREIGN KEY ([C_KEPESITESTIPUSID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_SZAKTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Checking existing data against newly created constraints';


GO
ALTER TABLE [dbo].[T_KEPESITES] WITH CHECK CHECK CONSTRAINT [FK_4028294_4028295];


GO
PRINT N'Update complete.';


GO
