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

SET NUMERIC_ROUNDABORT OFF;


GO
/*
Table [dbo].[T_SZOVEGESERTEKELESEK] is being dropped.  Deployment will halt if the table contains data.
*/

--IF EXISTS (select top 1 1 from [dbo].[T_SZOVEGESERTEKELESEK])
--    RAISERROR (N'Rows were detected. The schema update is terminating because data loss might occur.', 16, 127) WITH NOWAIT


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTAS] DROP CONSTRAINT [FK_4025674_4025672];


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


GO
ALTER TABLE [dbo].[T_ORAK_TANAROK] DROP CONSTRAINT [FK_4025632_4025631];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025938_4025937];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025575_4025580];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025575_4025581];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025602_4025601];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025608_4025607];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025611_4025610];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025620_4025619];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025629_4025628];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025635_4025634];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025638_4025637];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4026315_4026314];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4027901_4027900];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_402557502_402557500];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_402557505_402557503];


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


GO
ALTER TABLE [dbo].[T_ORALATOGATAS] DROP CONSTRAINT [FK_4025795_4025794];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP CONSTRAINT [FK_4027536_4027535];


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


GO
ALTER TABLE [dbo].[T_SZOVEGESERTEKELESEK] DROP CONSTRAINT [FK_T_21038_4027627];


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


GO
ALTER TABLE [dbo].[T_SZOVEGESERTEKELESEK] DROP CONSTRAINT [FK_402762602_402762600];


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


GO
ALTER TABLE [dbo].[T_SZOVEGESERTEKELESEK] DROP CONSTRAINT [FK_402762605_402762603];


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


GO
DROP VIEW [dbo].[T_SZOVEGESERTEKELESEK_OSSZES];


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


GO
DROP TABLE [dbo].[T_SZOVEGESERTEKELESEK];


GO
PRINT N'Starting rebuilding table [dbo].[T_TANITASIORA]...';


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_TANITASIORA] (
    [ID]                         INT             IDENTITY (1, 1) NOT NULL,
    [C_BEJEGYZESIDEJE]           DATETIME        NOT NULL,
    [C_BONTOTT]                  CHAR (1)        DEFAULT ('F') NULL,
    [C_DATUM]                    DATETIME        NOT NULL,
    [C_HELYETTESITESOKA]         NVARCHAR (255)  NULL,
    [C_HELYETTESITESTIPUSA]      INT             NULL,
    [C_HETNAPJA]                 INT             NOT NULL,
    [C_HETSORSZAMA]              INT             NOT NULL,
    [C_IKTTANORA]                CHAR (1)        DEFAULT ('F') NULL,
    [C_MEGJEGYZES]               NVARCHAR (255)  NULL,
    [C_MEGTARTOTT]               CHAR (1)        DEFAULT ('T') NULL,
    [C_NAPSORSZAMA]              INT             NOT NULL,
    [C_NEMSZAKRENDSZERUORA]      CHAR (1)        DEFAULT ('F') NULL,
    [C_ORAKEZDETE]               DATETIME        NOT NULL,
    [C_ORASZAM]                  INT             NOT NULL,
    [C_ORAVEGE]                  DATETIME        NOT NULL,
    [C_SORSZAMOZANDO]            CHAR (1)        NULL,
    [C_TAMOPORA]                 CHAR (1)        DEFAULT ('F') NULL,
    [C_TEMA]                     NVARCHAR (1000) NULL,
    [C_TULORA]                   CHAR (1)        DEFAULT ('F') NULL,
    [C_OSZTALYFONOKI]            CHAR (1)        DEFAULT ('F') NULL,
    [C_TANORANKIVULIFOGLALKOZAS] CHAR (1)        DEFAULT ('F') NULL,
    [C_HAZIFELADAT]              NVARCHAR (MAX)  NULL,
    [C_DIFFERENCIALT]            CHAR (1)        NULL,
    [C_DRAMA]                    CHAR (1)        NULL,
    [C_IPRTANORA]                CHAR (1)        NULL,
    [C_KOOPERATIV]               CHAR (1)        NULL,
    [C_MERES]                    CHAR (1)        NULL,
    [C_MINDENNAPOSTESTNEVELES]   CHAR (1)        NULL,
    [C_NEMZETISEGIORA]           CHAR (1)        NULL,
    [C_PARHUZAMOSORA]            CHAR (1)        NULL,
    [C_TIOP12]                   CHAR (1)        NULL,
    [C_ADMINALTALKIIRT]          CHAR (1)        NULL,
    [C_CSENGETESIRENDID]         INT             NULL,
    [C_CSENGETESIRENDORAID]      INT             NULL,
    [C_ORAOSSZEVONASID]          INT             NULL,
    [C_TANMENETID]               INT             NULL,
    [C_TANTARGYID]               INT             NULL,
    [C_NAPTARINAPID]             INT             NULL,
    [C_ORARENDIORAID]            INT             NULL,
    [C_FOGLALKOZASID]            INT             NULL,
    [C_HELYETTESITOTANARID]      INT             NULL,
    [C_TEREMID]                  INT             NULL,
    [C_OSZTALYCSOPORTID]         INT             NULL,
    [C_ORATULAJDONOSID]          INT             NOT NULL,
    [C_UPLOADEDFILEID]           INT             NULL,
    [C_INTEZMENYID]              INT             NOT NULL,
    [C_TANEVID]                  INT             NOT NULL,
    [TOROLT]                     CHAR (1)        DEFAULT ('F') NOT NULL,
    [SERIAL]                     INT             DEFAULT ((0)) NOT NULL,
    [LASTCHANGED]                DATETIME        DEFAULT (getdate()) NOT NULL,
    [CREATED]                    DATETIME        DEFAULT (getdate()) NOT NULL,
    [MODIFIER]                   INT             NULL,
    [CREATOR]                    INT             NULL,
    [ELOZOTANEVIREKORDID]        INT             NULL,
    [NNID]                       INT             NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40255751] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_TANITASIORA])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANITASIORA] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_TANITASIORA] ([ID], [C_BEJEGYZESIDEJE], [C_BONTOTT], [C_DATUM], [C_HELYETTESITESOKA], [C_HELYETTESITESTIPUSA], [C_HETNAPJA], [C_HETSORSZAMA], [C_IKTTANORA], [C_MEGJEGYZES], [C_MEGTARTOTT], [C_NAPSORSZAMA], [C_NEMSZAKRENDSZERUORA], [C_ORAKEZDETE], [C_ORASZAM], [C_ORAVEGE], [C_SORSZAMOZANDO], [C_TAMOPORA], [C_TEMA], [C_TULORA], [C_OSZTALYFONOKI], [C_TANORANKIVULIFOGLALKOZAS], [C_HAZIFELADAT], [C_DIFFERENCIALT], [C_DRAMA], [C_IPRTANORA], [C_KOOPERATIV], [C_MERES], [C_MINDENNAPOSTESTNEVELES], [C_NEMZETISEGIORA], [C_PARHUZAMOSORA], [C_TIOP12], [C_ADMINALTALKIIRT], [C_CSENGETESIRENDID], [C_CSENGETESIRENDORAID], [C_TANMENETID], [C_TANTARGYID], [C_NAPTARINAPID], [C_ORARENDIORAID], [C_FOGLALKOZASID], [C_HELYETTESITOTANARID], [C_TEREMID], [C_OSZTALYCSOPORTID], [C_ORATULAJDONOSID], [C_UPLOADEDFILEID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_BEJEGYZESIDEJE],
                 [C_BONTOTT],
                 [C_DATUM],
                 [C_HELYETTESITESOKA],
                 [C_HELYETTESITESTIPUSA],
                 [C_HETNAPJA],
                 [C_HETSORSZAMA],
                 [C_IKTTANORA],
                 [C_MEGJEGYZES],
                 [C_MEGTARTOTT],
                 [C_NAPSORSZAMA],
                 [C_NEMSZAKRENDSZERUORA],
                 [C_ORAKEZDETE],
                 [C_ORASZAM],
                 [C_ORAVEGE],
                 [C_SORSZAMOZANDO],
                 [C_TAMOPORA],
                 [C_TEMA],
                 [C_TULORA],
                 [C_OSZTALYFONOKI],
                 [C_TANORANKIVULIFOGLALKOZAS],
                 [C_HAZIFELADAT],
                 [C_DIFFERENCIALT],
                 [C_DRAMA],
                 [C_IPRTANORA],
                 [C_KOOPERATIV],
                 [C_MERES],
                 [C_MINDENNAPOSTESTNEVELES],
                 [C_NEMZETISEGIORA],
                 [C_PARHUZAMOSORA],
                 [C_TIOP12],
                 [C_ADMINALTALKIIRT],
                 [C_CSENGETESIRENDID],
                 [C_CSENGETESIRENDORAID],
                 [C_TANMENETID],
                 [C_TANTARGYID],
                 [C_NAPTARINAPID],
                 [C_ORARENDIORAID],
                 [C_FOGLALKOZASID],
                 [C_HELYETTESITOTANARID],
                 [C_TEREMID],
                 [C_OSZTALYCSOPORTID],
                 [C_ORATULAJDONOSID],
                 [C_UPLOADEDFILEID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_TANITASIORA]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANITASIORA] OFF;
    END

DROP TABLE [dbo].[T_TANITASIORA];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_TANITASIORA]', N'T_TANITASIORA';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40255751]', N'PK_T_4025575', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025575_4025580]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025575_4025580]
    ON [dbo].[T_TANITASIORA]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_HELYETTESITESTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025575_4025581]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025575_4025581]
    ON [dbo].[T_TANITASIORA]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_HETNAPJA] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025602_4025601]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025602_4025601]
    ON [dbo].[T_TANITASIORA]([C_TANMENETID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025608_4025607]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025608_4025607]
    ON [dbo].[T_TANITASIORA]([C_TANTARGYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025611_4025610]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025611_4025610]
    ON [dbo].[T_TANITASIORA]([C_NAPTARINAPID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025620_4025619]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025620_4025619]
    ON [dbo].[T_TANITASIORA]([C_ORARENDIORAID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025623_4025622]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025623_4025622]
    ON [dbo].[T_TANITASIORA]([C_FOGLALKOZASID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025629_4025628]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025629_4025628]
    ON [dbo].[T_TANITASIORA]([C_HELYETTESITOTANARID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025635_4025634]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025635_4025634]
    ON [dbo].[T_TANITASIORA]([C_TEREMID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4025638_4025637]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025638_4025637]
    ON [dbo].[T_TANITASIORA]([C_OSZTALYCSOPORTID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4026315_4026314]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026315_4026314]
    ON [dbo].[T_TANITASIORA]([C_ORATULAJDONOSID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_4027901_4027900]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027901_4027900]
    ON [dbo].[T_TANITASIORA]([C_UPLOADEDFILEID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_402557502_402557500]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402557502_402557500]
    ON [dbo].[T_TANITASIORA]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[I_T_402557505_402557503]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402557505_402557503]
    ON [dbo].[T_TANITASIORA]([C_TANEVID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[IDX_TANITASIORA_NAPTARINAPID_ORASZAM]...';


GO
CREATE NONCLUSTERED INDEX [IDX_TANITASIORA_NAPTARINAPID_ORASZAM]
    ON [dbo].[T_TANITASIORA]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_NAPTARINAPID] ASC, [C_ORASZAM] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[IDX_TANITASIORA_NAPTARINAPID_TANTARGYID_OSZTALYCSOPORTID]...';


GO
CREATE NONCLUSTERED INDEX [IDX_TANITASIORA_NAPTARINAPID_TANTARGYID_OSZTALYCSOPORTID]
    ON [dbo].[T_TANITASIORA]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_NAPTARINAPID] ASC, [C_TANTARGYID] ASC, [C_OSZTALYCSOPORTID] ASC);


GO
PRINT N'Creating [dbo].[T_TANITASIORA].[IX_TANITASIORA_INTEZMENYID_TANEVID_OSZTALYCSOPORTID_TANTARGYID]...';


GO
CREATE NONCLUSTERED INDEX [IX_TANITASIORA_INTEZMENYID_TANEVID_OSZTALYCSOPORTID_TANTARGYID]
    ON [dbo].[T_TANITASIORA]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_OSZTALYCSOPORTID] ASC, [C_TANTARGYID] ASC)
    INCLUDE([TOROLT], [C_DATUM]);


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


GO
CREATE TABLE [dbo].[T_ERTEKELESMONDATBANK] (
    [ID]                      INT            IDENTITY (1, 1) NOT NULL,
    [C_ERTEKELESTIPUSA]       INT            NULL,
    [C_EVFOLYAMTIPUSA]        INT            NULL,
    [C_ROVIDNEV]              NVARCHAR (3)   NOT NULL,
    [C_SZOVEG]                NVARCHAR (MAX) NOT NULL,
    [C_TANTARGYID]            INT            NULL,
    [C_FELADATELLATASIHELYID] INT            NULL,
    [C_INTEZMENYID]           INT            NOT NULL,
    [C_TANEVID]               INT            NOT NULL,
    [TOROLT]                  CHAR (1)       NOT NULL,
    [SERIAL]                  INT            NOT NULL,
    [LASTCHANGED]             DATETIME       NOT NULL,
    [CREATED]                 DATETIME       NOT NULL,
    [MODIFIER]                INT            NULL,
    [CREATOR]                 INT            NULL,
    [ELOZOTANEVIREKORDID]     INT            NULL,
    [NNID]                    INT            NULL,
    CONSTRAINT [PK_T_4027915] PRIMARY KEY CLUSTERED ([ID] ASC)
);


GO
PRINT N'Creating [dbo].[T_ERTEKELESMONDATBANK].[I_T_4027915_4027916]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027915_4027916]
    ON [dbo].[T_ERTEKELESMONDATBANK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ERTEKELESTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_ERTEKELESMONDATBANK].[I_T_4027915_4027917]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027915_4027917]
    ON [dbo].[T_ERTEKELESMONDATBANK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_EVFOLYAMTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_ERTEKELESMONDATBANK].[I_T_4027923_4027921]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027923_4027921]
    ON [dbo].[T_ERTEKELESMONDATBANK]([C_TANTARGYID] ASC);


GO
PRINT N'Creating [dbo].[T_ERTEKELESMONDATBANK].[I_T_4027926_4027924]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027926_4027924]
    ON [dbo].[T_ERTEKELESMONDATBANK]([C_FELADATELLATASIHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_ERTEKELESMONDATBANK].[I_T_402791502_402791500]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402791502_402791500]
    ON [dbo].[T_ERTEKELESMONDATBANK]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_ERTEKELESMONDATBANK].[I_T_402791505_402791503]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402791505_402791503]
    ON [dbo].[T_ERTEKELESMONDATBANK]([C_TANEVID] ASC);


GO
PRINT N'Creating unnamed constraint on [dbo].[T_ERTEKELESMONDATBANK]...';


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK]
    ADD DEFAULT ('F') FOR [TOROLT];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_ERTEKELESMONDATBANK]...';


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK]
    ADD DEFAULT ((0)) FOR [SERIAL];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_ERTEKELESMONDATBANK]...';


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK]
    ADD DEFAULT (getdate()) FOR [LASTCHANGED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_ERTEKELESMONDATBANK]...';


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK]
    ADD DEFAULT (getdate()) FOR [CREATED];


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4025674_4025672] FOREIGN KEY ([C_TANITASIORAKID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


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


GO
ALTER TABLE [dbo].[T_ORAK_TANAROK] WITH NOCHECK
    ADD CONSTRAINT [FK_4025632_4025631] FOREIGN KEY ([C_ORAKID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025938_4025937] FOREIGN KEY ([C_TANORAID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025575_4025580] FOREIGN KEY ([C_HELYETTESITESTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_HELYETTESITESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025575_4025581] FOREIGN KEY ([C_HETNAPJA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_HETNAPJATIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025602_4025601] FOREIGN KEY ([C_TANMENETID]) REFERENCES [dbo].[T_TANMENET] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025608_4025607] FOREIGN KEY ([C_TANTARGYID]) REFERENCES [dbo].[T_TANTARGY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025611_4025610] FOREIGN KEY ([C_NAPTARINAPID]) REFERENCES [dbo].[T_NAPTARINAP] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025620_4025619] FOREIGN KEY ([C_ORARENDIORAID]) REFERENCES [dbo].[T_ORARENDIORA] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025623_4025622] FOREIGN KEY ([C_FOGLALKOZASID]) REFERENCES [dbo].[T_FOGLALKOZAS] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025629_4025628] FOREIGN KEY ([C_HELYETTESITOTANARID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025635_4025634] FOREIGN KEY ([C_TEREMID]) REFERENCES [dbo].[T_TEREM] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4025638_4025637] FOREIGN KEY ([C_OSZTALYCSOPORTID]) REFERENCES [dbo].[T_OSZTALYCSOPORT] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4026315_4026314] FOREIGN KEY ([C_ORATULAJDONOSID]) REFERENCES [dbo].[T_FELHASZNALO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_4027901_4027900] FOREIGN KEY ([C_UPLOADEDFILEID]) REFERENCES [dbo].[T_UPLOADEDFILE] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_402557502_402557500] FOREIGN KEY ([C_INTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH NOCHECK
    ADD CONSTRAINT [FK_402557505_402557503] FOREIGN KEY ([C_TANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO
ALTER TABLE [dbo].[T_ORALATOGATAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4025795_4025794] FOREIGN KEY ([C_MEGTARTOTTORAID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] WITH NOCHECK
    ADD CONSTRAINT [FK_4027536_4027535] FOREIGN KEY ([C_TANITASIORAID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


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


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH NOCHECK
    ADD CONSTRAINT [FK_4027915_4027916] FOREIGN KEY ([C_ERTEKELESTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ERTEKELESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH NOCHECK
    ADD CONSTRAINT [FK_4027915_4027917] FOREIGN KEY ([C_EVFOLYAMTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH NOCHECK
    ADD CONSTRAINT [FK_4027923_4027921] FOREIGN KEY ([C_TANTARGYID]) REFERENCES [dbo].[T_TANTARGY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH NOCHECK
    ADD CONSTRAINT [FK_4027926_4027924] FOREIGN KEY ([C_FELADATELLATASIHELYID]) REFERENCES [dbo].[T_FELADATELLATASIHELY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH NOCHECK
    ADD CONSTRAINT [FK_402791502_402791500] FOREIGN KEY ([C_INTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH NOCHECK
    ADD CONSTRAINT [FK_402791505_402791503] FOREIGN KEY ([C_TANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO

  CREATE TRIGGER tr_tanitasioraLog ON T_TANITASIORA 
  FOR INSERT, DELETE, UPDATE AS
  BEGIN
    SET NOCOUNT ON
    DECLARE
      @insTable TABLE (objectId int, intezmenyId int, tanevId int, creator int, modifier int, col nvarchar(30), val varchar(max))
    DECLARE  
      @delTable TABLE (objectId int, intezmenyId int, tanevId int, creator int, modifier int, col nvarchar(30), val varchar(max))
    DECLARE 
      @linkTable TABLE (id int, objectId int, intezmenyId int, tanevId int)
    DECLARE  
      @action char(1),
      @xml xml    
    
    SET @action =
      CASE 
        WHEN NOT EXISTS (SELECT 1 FROM DELETED) THEN 'I'
        WHEN NOT EXISTS (SELECT 1 FROM INSERTED) THEN 'D'
        ELSE 'U'
      END  
    
      
    INSERT INTO @insTable (objectId, intezmenyId, tanevId, creator, modifier, col, val)
    SELECT ID, C_INTEZMENYID, C_TANEVID, CREATOR, MODIFIER, col, val
      FROM 
       (
       SELECT  ID, C_INTEZMENYID AS C_INTEZMENYID, C_TANEVID AS C_TANEVID, CREATOR, MODIFIER, CONVERT(nvarchar(MAX), C_BEJEGYZESIDEJE, 121) AS C_BEJEGYZESIDEJE, CAST( C_BONTOTT AS nvarchar(MAX)) AS C_BONTOTT, CONVERT(nvarchar(MAX), C_DATUM, 121) AS C_DATUM, CAST( C_HELYETTESITESOKA AS nvarchar(MAX)) AS C_HELYETTESITESOKA, CAST( C_HELYETTESITESTIPUSA AS nvarchar(MAX)) AS C_HELYETTESITESTIPUSA, CAST( C_HETNAPJA AS nvarchar(MAX)) AS C_HETNAPJA, CAST( C_HETSORSZAMA AS nvarchar(MAX)) AS C_HETSORSZAMA, CAST( C_IKTTANORA AS nvarchar(MAX)) AS C_IKTTANORA, CAST( C_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_MEGTARTOTT AS nvarchar(MAX)) AS C_MEGTARTOTT, CAST( C_NAPSORSZAMA AS nvarchar(MAX)) AS C_NAPSORSZAMA, CAST( C_NEMSZAKRENDSZERUORA AS nvarchar(MAX)) AS C_NEMSZAKRENDSZERUORA, CONVERT(nvarchar(MAX), C_ORAKEZDETE, 121) AS C_ORAKEZDETE, CAST( C_ORASZAM AS nvarchar(MAX)) AS C_ORASZAM, CONVERT(nvarchar(MAX), C_ORAVEGE, 121) AS C_ORAVEGE, CAST( C_SORSZAMOZANDO AS nvarchar(MAX)) AS C_SORSZAMOZANDO, CAST( C_TAMOPORA AS nvarchar(MAX)) AS C_TAMOPORA, CAST( C_TEMA AS nvarchar(MAX)) AS C_TEMA, CAST( C_TULORA AS nvarchar(MAX)) AS C_TULORA, CAST( C_OSZTALYFONOKI AS nvarchar(MAX)) AS C_OSZTALYFONOKI, CAST( C_TANORANKIVULIFOGLALKOZAS AS nvarchar(MAX)) AS C_TANORANKIVULIFOGLALKOZAS, CAST( C_HAZIFELADAT AS nvarchar(MAX)) AS C_HAZIFELADAT, CAST( C_DIFFERENCIALT AS nvarchar(MAX)) AS C_DIFFERENCIALT, CAST( C_DRAMA AS nvarchar(MAX)) AS C_DRAMA, CAST( C_IPRTANORA AS nvarchar(MAX)) AS C_IPRTANORA, CAST( C_KOOPERATIV AS nvarchar(MAX)) AS C_KOOPERATIV, CAST( C_MERES AS nvarchar(MAX)) AS C_MERES, CAST( C_MINDENNAPOSTESTNEVELES AS nvarchar(MAX)) AS C_MINDENNAPOSTESTNEVELES, CAST( C_NEMZETISEGIORA AS nvarchar(MAX)) AS C_NEMZETISEGIORA, CAST( C_PARHUZAMOSORA AS nvarchar(MAX)) AS C_PARHUZAMOSORA, CAST( C_TIOP12 AS nvarchar(MAX)) AS C_TIOP12, CAST( C_ADMINALTALKIIRT AS nvarchar(MAX)) AS C_ADMINALTALKIIRT, CAST( C_CSENGETESIRENDID AS nvarchar(MAX)) AS C_CSENGETESIRENDID, CAST( C_CSENGETESIRENDORAID AS nvarchar(MAX)) AS C_CSENGETESIRENDORAID, CAST( C_ORAOSSZEVONASID AS nvarchar(MAX)) AS C_ORAOSSZEVONASID, CAST( C_TANMENETID AS nvarchar(MAX)) AS C_TANMENETID, CAST( C_TANTARGYID AS nvarchar(MAX)) AS C_TANTARGYID, CAST( C_NAPTARINAPID AS nvarchar(MAX)) AS C_NAPTARINAPID, CAST( C_ORARENDIORAID AS nvarchar(MAX)) AS C_ORARENDIORAID, CAST( C_FOGLALKOZASID AS nvarchar(MAX)) AS C_FOGLALKOZASID, CAST( C_HELYETTESITOTANARID AS nvarchar(MAX)) AS C_HELYETTESITOTANARID, CAST( C_TEREMID AS nvarchar(MAX)) AS C_TEREMID, CAST( C_OSZTALYCSOPORTID AS nvarchar(MAX)) AS C_OSZTALYCSOPORTID, CAST( C_ORATULAJDONOSID AS nvarchar(MAX)) AS C_ORATULAJDONOSID, CAST( C_UPLOADEDFILEID AS nvarchar(MAX)) AS C_UPLOADEDFILEID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( ELOZOTANEVIREKORDID AS nvarchar(MAX)) AS ELOZOTANEVIREKORDID, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_BEJEGYZESIDEJE, C_BONTOTT, C_DATUM, C_HELYETTESITESOKA, C_HELYETTESITESTIPUSA, C_HETNAPJA, C_HETSORSZAMA, C_IKTTANORA, C_MEGJEGYZES, C_MEGTARTOTT, C_NAPSORSZAMA, C_NEMSZAKRENDSZERUORA, C_ORAKEZDETE, C_ORASZAM, C_ORAVEGE, C_SORSZAMOZANDO, C_TAMOPORA, C_TEMA, C_TULORA, C_OSZTALYFONOKI, C_TANORANKIVULIFOGLALKOZAS, C_HAZIFELADAT, C_DIFFERENCIALT, C_DRAMA, C_IPRTANORA, C_KOOPERATIV, C_MERES, C_MINDENNAPOSTESTNEVELES, C_NEMZETISEGIORA, C_PARHUZAMOSORA, C_TIOP12, C_ADMINALTALKIIRT, C_CSENGETESIRENDID, C_CSENGETESIRENDORAID, C_ORAOSSZEVONASID, C_TANMENETID, C_TANTARGYID, C_NAPTARINAPID, C_ORARENDIORAID, C_FOGLALKOZASID, C_HELYETTESITOTANARID, C_TEREMID, C_OSZTALYCSOPORTID, C_ORATULAJDONOSID, C_UPLOADEDFILEID, TOROLT, ELOZOTANEVIREKORDID, NNID)) AS unpvt

    INSERT INTO @delTable (objectId, intezmenyId, tanevId, creator, modifier, col, val)
    SELECT ID, C_INTEZMENYID, C_TANEVID, CREATOR, MODIFIER, col, val
      FROM 
       (
       SELECT  ID, C_INTEZMENYID AS C_INTEZMENYID, C_TANEVID AS C_TANEVID, CREATOR, MODIFIER, CONVERT(nvarchar(MAX), C_BEJEGYZESIDEJE, 121) AS C_BEJEGYZESIDEJE, CAST( C_BONTOTT AS nvarchar(MAX)) AS C_BONTOTT, CONVERT(nvarchar(MAX), C_DATUM, 121) AS C_DATUM, CAST( C_HELYETTESITESOKA AS nvarchar(MAX)) AS C_HELYETTESITESOKA, CAST( C_HELYETTESITESTIPUSA AS nvarchar(MAX)) AS C_HELYETTESITESTIPUSA, CAST( C_HETNAPJA AS nvarchar(MAX)) AS C_HETNAPJA, CAST( C_HETSORSZAMA AS nvarchar(MAX)) AS C_HETSORSZAMA, CAST( C_IKTTANORA AS nvarchar(MAX)) AS C_IKTTANORA, CAST( C_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_MEGTARTOTT AS nvarchar(MAX)) AS C_MEGTARTOTT, CAST( C_NAPSORSZAMA AS nvarchar(MAX)) AS C_NAPSORSZAMA, CAST( C_NEMSZAKRENDSZERUORA AS nvarchar(MAX)) AS C_NEMSZAKRENDSZERUORA, CONVERT(nvarchar(MAX), C_ORAKEZDETE, 121) AS C_ORAKEZDETE, CAST( C_ORASZAM AS nvarchar(MAX)) AS C_ORASZAM, CONVERT(nvarchar(MAX), C_ORAVEGE, 121) AS C_ORAVEGE, CAST( C_SORSZAMOZANDO AS nvarchar(MAX)) AS C_SORSZAMOZANDO, CAST( C_TAMOPORA AS nvarchar(MAX)) AS C_TAMOPORA, CAST( C_TEMA AS nvarchar(MAX)) AS C_TEMA, CAST( C_TULORA AS nvarchar(MAX)) AS C_TULORA, CAST( C_OSZTALYFONOKI AS nvarchar(MAX)) AS C_OSZTALYFONOKI, CAST( C_TANORANKIVULIFOGLALKOZAS AS nvarchar(MAX)) AS C_TANORANKIVULIFOGLALKOZAS, CAST( C_HAZIFELADAT AS nvarchar(MAX)) AS C_HAZIFELADAT, CAST( C_DIFFERENCIALT AS nvarchar(MAX)) AS C_DIFFERENCIALT, CAST( C_DRAMA AS nvarchar(MAX)) AS C_DRAMA, CAST( C_IPRTANORA AS nvarchar(MAX)) AS C_IPRTANORA, CAST( C_KOOPERATIV AS nvarchar(MAX)) AS C_KOOPERATIV, CAST( C_MERES AS nvarchar(MAX)) AS C_MERES, CAST( C_MINDENNAPOSTESTNEVELES AS nvarchar(MAX)) AS C_MINDENNAPOSTESTNEVELES, CAST( C_NEMZETISEGIORA AS nvarchar(MAX)) AS C_NEMZETISEGIORA, CAST( C_PARHUZAMOSORA AS nvarchar(MAX)) AS C_PARHUZAMOSORA, CAST( C_TIOP12 AS nvarchar(MAX)) AS C_TIOP12, CAST( C_ADMINALTALKIIRT AS nvarchar(MAX)) AS C_ADMINALTALKIIRT, CAST( C_CSENGETESIRENDID AS nvarchar(MAX)) AS C_CSENGETESIRENDID, CAST( C_CSENGETESIRENDORAID AS nvarchar(MAX)) AS C_CSENGETESIRENDORAID, CAST( C_ORAOSSZEVONASID AS nvarchar(MAX)) AS C_ORAOSSZEVONASID, CAST( C_TANMENETID AS nvarchar(MAX)) AS C_TANMENETID, CAST( C_TANTARGYID AS nvarchar(MAX)) AS C_TANTARGYID, CAST( C_NAPTARINAPID AS nvarchar(MAX)) AS C_NAPTARINAPID, CAST( C_ORARENDIORAID AS nvarchar(MAX)) AS C_ORARENDIORAID, CAST( C_FOGLALKOZASID AS nvarchar(MAX)) AS C_FOGLALKOZASID, CAST( C_HELYETTESITOTANARID AS nvarchar(MAX)) AS C_HELYETTESITOTANARID, CAST( C_TEREMID AS nvarchar(MAX)) AS C_TEREMID, CAST( C_OSZTALYCSOPORTID AS nvarchar(MAX)) AS C_OSZTALYCSOPORTID, CAST( C_ORATULAJDONOSID AS nvarchar(MAX)) AS C_ORATULAJDONOSID, CAST( C_UPLOADEDFILEID AS nvarchar(MAX)) AS C_UPLOADEDFILEID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( ELOZOTANEVIREKORDID AS nvarchar(MAX)) AS ELOZOTANEVIREKORDID, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_BEJEGYZESIDEJE, C_BONTOTT, C_DATUM, C_HELYETTESITESOKA, C_HELYETTESITESTIPUSA, C_HETNAPJA, C_HETSORSZAMA, C_IKTTANORA, C_MEGJEGYZES, C_MEGTARTOTT, C_NAPSORSZAMA, C_NEMSZAKRENDSZERUORA, C_ORAKEZDETE, C_ORASZAM, C_ORAVEGE, C_SORSZAMOZANDO, C_TAMOPORA, C_TEMA, C_TULORA, C_OSZTALYFONOKI, C_TANORANKIVULIFOGLALKOZAS, C_HAZIFELADAT, C_DIFFERENCIALT, C_DRAMA, C_IPRTANORA, C_KOOPERATIV, C_MERES, C_MINDENNAPOSTESTNEVELES, C_NEMZETISEGIORA, C_PARHUZAMOSORA, C_TIOP12, C_ADMINALTALKIIRT, C_CSENGETESIRENDID, C_CSENGETESIRENDORAID, C_ORAOSSZEVONASID, C_TANMENETID, C_TANTARGYID, C_NAPTARINAPID, C_ORARENDIORAID, C_FOGLALKOZASID, C_HELYETTESITOTANARID, C_TEREMID, C_OSZTALYCSOPORTID, C_ORATULAJDONOSID, C_UPLOADEDFILEID, TOROLT, ELOZOTANEVIREKORDID, NNID)) AS unpvt
    
    IF @action = 'I' BEGIN
      INSERT INTO T_ENTITYHISTORY (C_ALTERATIONDATE, C_ENTITYID, C_ENTITYNAME, C_REASON, C_FELHASZNALOID, C_INTEZMENYID, C_TANEVID)
      OUTPUT INSERTED.ID, INSERTED.C_ENTITYID, INSERTED.C_INTEZMENYID, INSERTED.C_TANEVID INTO @linkTable
      SELECT DISTINCT GETDATE(), objectId, 'T_TANITASIORA', 'New', creator, intezmenyId, tanevId FROM @insTable
      
      INSERT INTO T_ENTITYATTRIBUTEHISTORY (C_CURRENTVALUE, C_ORIGINALVALUE, C_PROPERTYNAME, C_ENTITYHISTORYID)
      SELECT val, NULL, col, lk.id 
      FROM @insTable i
      INNER JOIN @linkTable lk ON i.objectId = lk.objectId AND i.intezmenyId = lk.intezmenyId AND i.tanevId = lk.tanevId
      
    END

    IF @action = 'D' BEGIN
      INSERT INTO T_ENTITYHISTORY (C_ALTERATIONDATE, C_ENTITYID, C_ENTITYNAME, C_REASON, C_FELHASZNALOID, C_INTEZMENYID, C_TANEVID)
      OUTPUT INSERTED.ID, INSERTED.C_ENTITYID, INSERTED.C_INTEZMENYID, INSERTED.C_TANEVID INTO @linkTable
      SELECT DISTINCT GETDATE(), objectId, 'T_TANITASIORA', 'Removed', modifier, intezmenyId, tanevId FROM @delTable
      
      INSERT INTO T_ENTITYATTRIBUTEHISTORY (C_CURRENTVALUE, C_ORIGINALVALUE, C_PROPERTYNAME, C_ENTITYHISTORYID)
      SELECT val, NULL, col, lk.id 
      FROM @delTable i
      INNER JOIN @linkTable lk ON i.objectId = lk.objectId AND i.intezmenyId = lk.intezmenyId AND i.tanevId = lk.tanevId
      
    END

    IF @action = 'U' BEGIN
      INSERT INTO T_ENTITYHISTORY (C_ALTERATIONDATE, C_ENTITYID, C_ENTITYNAME, C_REASON, C_FELHASZNALOID, C_INTEZMENYID, C_TANEVID)
      OUTPUT INSERTED.ID, INSERTED.C_ENTITYID, INSERTED.C_INTEZMENYID, INSERTED.C_TANEVID INTO @linkTable
      SELECT DISTINCT GETDATE(), i.objectId, 'T_TANITASIORA', 
        CASE 
          WHEN i.val = 'T' AND d.val = 'F' THEN 'Logical Removed'
          WHEN i.val = 'F' AND d.val = 'T' THEN 'Restored'
          ELSE 'Modified' 
        END, i.modifier, i.intezmenyId, i.tanevId 
      FROM @insTable i
      INNER JOIN @delTable d ON i.intezmenyId = d.intezmenyId AND i.objectId = d.objectId AND i.tanevId = d.tanevId AND i.col = d.col
      WHERE i.col = 'TOROLT' AND d.COL = 'TOROLT'
      
      INSERT INTO T_ENTITYATTRIBUTEHISTORY (C_CURRENTVALUE, C_ORIGINALVALUE, C_PROPERTYNAME, C_ENTITYHISTORYID)
      SELECT i.val, d.val, ISNULL(i.col, d.col), lk.id 
      FROM @delTable d
        FULL JOIN @insTable i ON i.objectId = d.objectId AND i.intezmenyId = d.intezmenyId AND i.tanevId = d.tanevId AND i.col = d.col
        INNER JOIN @linkTable lk ON ISNULL(i.objectId, d.objectId) = lk.objectId AND ISNULL(i.intezmenyId, d.intezmenyId) = lk.intezmenyId AND ISNULL(i.tanevId, d.tanevId) = lk.tanevId
      WHERE (i.val <> d.val OR ((i.val IS NULL OR d.val IS NULL) AND (i.val IS NOT NULL OR d.val IS NOT NULL)))
    END  
  END
GO
PRINT N'Creating [dbo].[tr_ertekelesmondatbankLog]...';


GO

  CREATE TRIGGER tr_ertekelesmondatbankLog ON T_ERTEKELESMONDATBANK 
  FOR INSERT, DELETE, UPDATE AS
  BEGIN
    SET NOCOUNT ON
    DECLARE
      @insTable TABLE (objectId int, intezmenyId int, tanevId int, creator int, modifier int, col nvarchar(30), val varchar(max))
    DECLARE  
      @delTable TABLE (objectId int, intezmenyId int, tanevId int, creator int, modifier int, col nvarchar(30), val varchar(max))
    DECLARE 
      @linkTable TABLE (id int, objectId int, intezmenyId int, tanevId int)
    DECLARE  
      @action char(1),
      @xml xml    
    
    SET @action =
      CASE 
        WHEN NOT EXISTS (SELECT 1 FROM DELETED) THEN 'I'
        WHEN NOT EXISTS (SELECT 1 FROM INSERTED) THEN 'D'
        ELSE 'U'
      END  
    
      
    INSERT INTO @insTable (objectId, intezmenyId, tanevId, creator, modifier, col, val)
    SELECT ID, C_INTEZMENYID, C_TANEVID, CREATOR, MODIFIER, col, val
      FROM 
       (
       SELECT  ID, C_INTEZMENYID AS C_INTEZMENYID, C_TANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( C_ERTEKELESTIPUSA AS nvarchar(MAX)) AS C_ERTEKELESTIPUSA, CAST( C_EVFOLYAMTIPUSA AS nvarchar(MAX)) AS C_EVFOLYAMTIPUSA, CAST( C_ROVIDNEV AS nvarchar(MAX)) AS C_ROVIDNEV, CAST( C_SZOVEG AS nvarchar(MAX)) AS C_SZOVEG, CAST( C_TANTARGYID AS nvarchar(MAX)) AS C_TANTARGYID, CAST( C_FELADATELLATASIHELYID AS nvarchar(MAX)) AS C_FELADATELLATASIHELYID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( ELOZOTANEVIREKORDID AS nvarchar(MAX)) AS ELOZOTANEVIREKORDID, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_ERTEKELESTIPUSA, C_EVFOLYAMTIPUSA, C_ROVIDNEV, C_SZOVEG, C_TANTARGYID, C_FELADATELLATASIHELYID, TOROLT, ELOZOTANEVIREKORDID, NNID)) AS unpvt

    INSERT INTO @delTable (objectId, intezmenyId, tanevId, creator, modifier, col, val)
    SELECT ID, C_INTEZMENYID, C_TANEVID, CREATOR, MODIFIER, col, val
      FROM 
       (
       SELECT  ID, C_INTEZMENYID AS C_INTEZMENYID, C_TANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( C_ERTEKELESTIPUSA AS nvarchar(MAX)) AS C_ERTEKELESTIPUSA, CAST( C_EVFOLYAMTIPUSA AS nvarchar(MAX)) AS C_EVFOLYAMTIPUSA, CAST( C_ROVIDNEV AS nvarchar(MAX)) AS C_ROVIDNEV, CAST( C_SZOVEG AS nvarchar(MAX)) AS C_SZOVEG, CAST( C_TANTARGYID AS nvarchar(MAX)) AS C_TANTARGYID, CAST( C_FELADATELLATASIHELYID AS nvarchar(MAX)) AS C_FELADATELLATASIHELYID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( ELOZOTANEVIREKORDID AS nvarchar(MAX)) AS ELOZOTANEVIREKORDID, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_ERTEKELESTIPUSA, C_EVFOLYAMTIPUSA, C_ROVIDNEV, C_SZOVEG, C_TANTARGYID, C_FELADATELLATASIHELYID, TOROLT, ELOZOTANEVIREKORDID, NNID)) AS unpvt
    
    IF @action = 'I' BEGIN
      INSERT INTO T_ENTITYHISTORY (C_ALTERATIONDATE, C_ENTITYID, C_ENTITYNAME, C_REASON, C_FELHASZNALOID, C_INTEZMENYID, C_TANEVID)
      OUTPUT INSERTED.ID, INSERTED.C_ENTITYID, INSERTED.C_INTEZMENYID, INSERTED.C_TANEVID INTO @linkTable
      SELECT DISTINCT GETDATE(), objectId, 'T_ERTEKELESMONDATBANK', 'New', creator, intezmenyId, tanevId FROM @insTable
      
      INSERT INTO T_ENTITYATTRIBUTEHISTORY (C_CURRENTVALUE, C_ORIGINALVALUE, C_PROPERTYNAME, C_ENTITYHISTORYID)
      SELECT val, NULL, col, lk.id 
      FROM @insTable i
      INNER JOIN @linkTable lk ON i.objectId = lk.objectId AND i.intezmenyId = lk.intezmenyId AND i.tanevId = lk.tanevId
      
    END

    IF @action = 'D' BEGIN
      INSERT INTO T_ENTITYHISTORY (C_ALTERATIONDATE, C_ENTITYID, C_ENTITYNAME, C_REASON, C_FELHASZNALOID, C_INTEZMENYID, C_TANEVID)
      OUTPUT INSERTED.ID, INSERTED.C_ENTITYID, INSERTED.C_INTEZMENYID, INSERTED.C_TANEVID INTO @linkTable
      SELECT DISTINCT GETDATE(), objectId, 'T_ERTEKELESMONDATBANK', 'Removed', modifier, intezmenyId, tanevId FROM @delTable
      
      INSERT INTO T_ENTITYATTRIBUTEHISTORY (C_CURRENTVALUE, C_ORIGINALVALUE, C_PROPERTYNAME, C_ENTITYHISTORYID)
      SELECT val, NULL, col, lk.id 
      FROM @delTable i
      INNER JOIN @linkTable lk ON i.objectId = lk.objectId AND i.intezmenyId = lk.intezmenyId AND i.tanevId = lk.tanevId
      
    END

    IF @action = 'U' BEGIN
      INSERT INTO T_ENTITYHISTORY (C_ALTERATIONDATE, C_ENTITYID, C_ENTITYNAME, C_REASON, C_FELHASZNALOID, C_INTEZMENYID, C_TANEVID)
      OUTPUT INSERTED.ID, INSERTED.C_ENTITYID, INSERTED.C_INTEZMENYID, INSERTED.C_TANEVID INTO @linkTable
      SELECT DISTINCT GETDATE(), i.objectId, 'T_ERTEKELESMONDATBANK', 
        CASE 
          WHEN i.val = 'T' AND d.val = 'F' THEN 'Logical Removed'
          WHEN i.val = 'F' AND d.val = 'T' THEN 'Restored'
          ELSE 'Modified' 
        END, i.modifier, i.intezmenyId, i.tanevId 
      FROM @insTable i
      INNER JOIN @delTable d ON i.intezmenyId = d.intezmenyId AND i.objectId = d.objectId AND i.tanevId = d.tanevId AND i.col = d.col
      WHERE i.col = 'TOROLT' AND d.COL = 'TOROLT'
      
      INSERT INTO T_ENTITYATTRIBUTEHISTORY (C_CURRENTVALUE, C_ORIGINALVALUE, C_PROPERTYNAME, C_ENTITYHISTORYID)
      SELECT i.val, d.val, ISNULL(i.col, d.col), lk.id 
      FROM @delTable d
        FULL JOIN @insTable i ON i.objectId = d.objectId AND i.intezmenyId = d.intezmenyId AND i.tanevId = d.tanevId AND i.col = d.col
        INNER JOIN @linkTable lk ON ISNULL(i.objectId, d.objectId) = lk.objectId AND ISNULL(i.intezmenyId, d.intezmenyId) = lk.intezmenyId AND ISNULL(i.tanevId, d.tanevId) = lk.tanevId
      WHERE (i.val <> d.val OR ((i.val IS NULL OR d.val IS NULL) AND (i.val IS NOT NULL OR d.val IS NOT NULL)))
    END  
  END
GO
PRINT N'Refreshing [dbo].[T_TANITASIORA_OSSZES]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[T_TANITASIORA_OSSZES]';


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


GO
ALTER TABLE [dbo].[T_TANULOMULASZTAS] WITH CHECK CHECK CONSTRAINT [FK_4025674_4025672];

ALTER TABLE [dbo].[T_ORAK_TANAROK] WITH CHECK CHECK CONSTRAINT [FK_4025632_4025631];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025938_4025937];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025575_4025580];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025575_4025581];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025602_4025601];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025608_4025607];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025611_4025610];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025620_4025619];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025623_4025622];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025629_4025628];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025635_4025634];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025638_4025637];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4026315_4026314];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4027901_4027900];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_402557502_402557500];

ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_402557505_402557503];

ALTER TABLE [dbo].[T_ORALATOGATAS] WITH CHECK CHECK CONSTRAINT [FK_4025795_4025794];

ALTER TABLE [dbo].[T_TANULOESEMENY] WITH CHECK CHECK CONSTRAINT [FK_4027536_4027535];

ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH CHECK CHECK CONSTRAINT [FK_4027915_4027916];

ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH CHECK CHECK CONSTRAINT [FK_4027915_4027917];

ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH CHECK CHECK CONSTRAINT [FK_4027923_4027921];

ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH CHECK CHECK CONSTRAINT [FK_4027926_4027924];

ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH CHECK CHECK CONSTRAINT [FK_402791502_402791500];

ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] WITH CHECK CHECK CONSTRAINT [FK_402791505_402791503];


GO
PRINT N'Update complete.';


GO
