﻿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_4025635_4025634]...';


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


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


GO
ALTER TABLE [dbo].[T_ORARENDIORA] DROP CONSTRAINT [FK_4025550_4025549];


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT] DROP CONSTRAINT [FK_4025954_4025953];


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


GO
ALTER TABLE [dbo].[T_TEREM_TANARITANTARGY] DROP CONSTRAINT [FK_4025951_4025950];


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


GO
ALTER TABLE [dbo].[T_TEREM_FOGLALKOZAS] DROP CONSTRAINT [FK_4025948_4025947];


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


GO
ALTER TABLE [dbo].[T_TERMEK_JOGYAKORLATOK] DROP CONSTRAINT [FK_4025913_4025912];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_ADATSZOLGALTATASENAPLO] (
    [ID]                         INT             IDENTITY (1, 1) NOT NULL,
    [C_ENAPLOFORRASA]            INT             NULL,
    [C_ENAPLOINFRAJA]            INT             NULL,
    [C_ENAPLOTIPUSA]             INT             NULL,
    [C_ISELLENORZO]              CHAR (1)        NULL,
    [C_ISENAPLO]                 CHAR (1)        NULL,
    [C_ISPAPIRNAPLO]             CHAR (1)        NULL,
    [C_SZERZODES]                VARBINARY (MAX) NULL,
    [C_SZERZODESKEZDETE]         DATETIME        NULL,
    [C_SZERZODESLEJARATA]        DATETIME        NULL,
    [C_SZERZODESOSSZEGE]         INT             NULL,
    [C_SZERZODOSZEMELYNEVE]      NVARCHAR (300)  NULL,
    [C_SZERZODOSZEMELYPOZICIOJA] INT             NULL,
    [C_SZERZODESFILENAME]        NVARCHAR (255)  NULL,
    [C_ENAPLOHASZNALATA]         INT             NULL,
    [C_PROJEKTOROKSZAMA]         INT             NULL,
    [C_WIFIELERES]               INT             NULL,
    [C_MUKODOPROJEKTOROKSZAMA]   INT             NULL,
    [C_TERMEKSZAMA]              INT             NULL,
    [C_WIFILEFEDETTTERMEKSZAMA]  INT             NULL,
    [C_INTEZMENYID]              INT             NOT NULL,
    [C_TANEVID]                  INT             NOT NULL,
    [TOROLT]                     CHAR (1)        DEFAULT ('F') NULL,
    [SERIAL]                     INT             NULL,
    [LASTCHANGED]                DATETIME        DEFAULT (getdate()) NULL,
    [CREATED]                    DATETIME        DEFAULT (getdate()) NULL,
    [MODIFIER]                   INT             NULL,
    [CREATOR]                    INT             NULL,
    [NNID]                       INT             NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40266941] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_ADATSZOLGALTATASENAPLO])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_ADATSZOLGALTATASENAPLO] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_ADATSZOLGALTATASENAPLO] ([ID], [C_ENAPLOFORRASA], [C_ENAPLOINFRAJA], [C_ENAPLOTIPUSA], [C_ISELLENORZO], [C_ISENAPLO], [C_ISPAPIRNAPLO], [C_SZERZODES], [C_SZERZODESKEZDETE], [C_SZERZODESLEJARATA], [C_SZERZODESOSSZEGE], [C_SZERZODOSZEMELYNEVE], [C_SZERZODOSZEMELYPOZICIOJA], [C_SZERZODESFILENAME], [C_ENAPLOHASZNALATA], [C_PROJEKTOROKSZAMA], [C_WIFIELERES], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID])
        SELECT   [ID],
                 [C_ENAPLOFORRASA],
                 [C_ENAPLOINFRAJA],
                 [C_ENAPLOTIPUSA],
                 [C_ISELLENORZO],
                 [C_ISENAPLO],
                 [C_ISPAPIRNAPLO],
                 [C_SZERZODES],
                 [C_SZERZODESKEZDETE],
                 [C_SZERZODESLEJARATA],
                 [C_SZERZODESOSSZEGE],
                 [C_SZERZODOSZEMELYNEVE],
                 [C_SZERZODOSZEMELYPOZICIOJA],
                 [C_SZERZODESFILENAME],
                 [C_ENAPLOHASZNALATA],
                 [C_PROJEKTOROKSZAMA],
                 [C_WIFIELERES],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID]
        FROM     [dbo].[T_ADATSZOLGALTATASENAPLO]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_ADATSZOLGALTATASENAPLO] OFF;
    END

DROP TABLE [dbo].[T_ADATSZOLGALTATASENAPLO];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_ADATSZOLGALTATASENAPLO]', N'T_ADATSZOLGALTATASENAPLO';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40266941]', N'PK_T_4026694', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_ADATSZOLGALTATASENAPLO].[I_T_4026694_4026695]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026694_4026695]
    ON [dbo].[T_ADATSZOLGALTATASENAPLO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ENAPLOFORRASA] ASC);


GO
PRINT N'Creating [dbo].[T_ADATSZOLGALTATASENAPLO].[I_T_4026694_4026696]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026694_4026696]
    ON [dbo].[T_ADATSZOLGALTATASENAPLO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ENAPLOINFRAJA] ASC);


GO
PRINT N'Creating [dbo].[T_ADATSZOLGALTATASENAPLO].[I_T_4026694_4026697]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026694_4026697]
    ON [dbo].[T_ADATSZOLGALTATASENAPLO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ENAPLOTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_ADATSZOLGALTATASENAPLO].[I_T_4026694_4026706]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026694_4026706]
    ON [dbo].[T_ADATSZOLGALTATASENAPLO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_SZERZODOSZEMELYPOZICIOJA] ASC);


GO
PRINT N'Creating [dbo].[T_ADATSZOLGALTATASENAPLO].[I_T_4026694_4027417]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026694_4027417]
    ON [dbo].[T_ADATSZOLGALTATASENAPLO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ENAPLOHASZNALATA] ASC);


GO
PRINT N'Creating [dbo].[T_ADATSZOLGALTATASENAPLO].[I_T_4026694_4027419]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026694_4027419]
    ON [dbo].[T_ADATSZOLGALTATASENAPLO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_WIFIELERES] ASC);


GO
PRINT N'Creating [dbo].[T_ADATSZOLGALTATASENAPLO].[I_T_402669402_402669400]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402669402_402669400]
    ON [dbo].[T_ADATSZOLGALTATASENAPLO]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_ADATSZOLGALTATASENAPLO].[I_T_402669405_402669403]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402669405_402669403]
    ON [dbo].[T_ADATSZOLGALTATASENAPLO]([C_TANEVID] ASC);


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_ESZKOZ] (
    [ID]                     INT            IDENTITY (1, 1) NOT NULL,
    [C_BERELT]               CHAR (1)       NULL,
    [C_BESZERZESDATUM]       DATETIME       NULL,
    [C_BESZERZESIAR]         INT            NULL,
    [C_DARABSZAM]            INT            NULL,
    [C_FOKONYVISZAM]         NVARCHAR (255) NULL,
    [C_GYARISZAM]            NVARCHAR (255) NULL,
    [C_GYARTASIEV]           INT            NULL,
    [C_HALOZATIKAPCSOLAT]    CHAR (1)       NULL,
    [C_HORDOZHATO]           CHAR (1)       NULL,
    [C_INTERNETKAPCSOLAT]    CHAR (1)       NULL,
    [C_KATEGORIA]            INT            NULL,
    [C_LELTARISZAM]          NVARCHAR (200) NULL,
    [C_MEGJEGYZES]           NVARCHAR (255) NULL,
    [C_MULTIMEDIAS]          CHAR (1)       NULL,
    [C_NEV]                  NVARCHAR (255) NOT NULL,
    [C_PEDAGOGUSHOZZAFERHET] CHAR (1)       NULL,
    [C_SZALLITO]             NVARCHAR (255) NULL,
    [C_TANULOHOZZAFERHET]    CHAR (1)       NULL,
    [C_TARTOZEK]             CHAR (1)       DEFAULT ('F') NULL,
    [C_TARTOZEKHIVATKOZAS]   NVARCHAR (20)  NULL,
    [C_TIPUS]                INT            NULL,
    [C_CELJA]                INT            NULL,
    [C_JELLEGE]              INT            NULL,
    [C_MENNYISEGIEGYSEG]     INT            NULL,
    [C_VONALKOD]             NVARCHAR (128) NULL,
    [C_NEMMUKODIK]           CHAR (1)       DEFAULT ('F') NULL,
    [C_TEREMID]              INT            NOT NULL,
    [C_FELELOSID]            INT            NULL,
    [C_INTEZMENYID]          INT            NOT NULL,
    [C_TANEVID]              INT            NOT NULL,
    [TOROLT]                 CHAR (1)       DEFAULT ('F') NULL,
    [SERIAL]                 INT            NULL,
    [LASTCHANGED]            DATETIME       DEFAULT (getdate()) NULL,
    [CREATED]                DATETIME       DEFAULT (getdate()) NULL,
    [MODIFIER]               INT            NULL,
    [CREATOR]                INT            NULL,
    [NNID]                   INT            NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40250981] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_ESZKOZ])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_ESZKOZ] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_ESZKOZ] ([ID], [C_BERELT], [C_BESZERZESDATUM], [C_BESZERZESIAR], [C_DARABSZAM], [C_FOKONYVISZAM], [C_GYARISZAM], [C_GYARTASIEV], [C_HALOZATIKAPCSOLAT], [C_HORDOZHATO], [C_INTERNETKAPCSOLAT], [C_KATEGORIA], [C_LELTARISZAM], [C_MEGJEGYZES], [C_MULTIMEDIAS], [C_NEV], [C_PEDAGOGUSHOZZAFERHET], [C_SZALLITO], [C_TANULOHOZZAFERHET], [C_TARTOZEK], [C_TARTOZEKHIVATKOZAS], [C_TIPUS], [C_CELJA], [C_JELLEGE], [C_MENNYISEGIEGYSEG], [C_VONALKOD], [C_TEREMID], [C_FELELOSID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID])
        SELECT   [ID],
                 [C_BERELT],
                 [C_BESZERZESDATUM],
                 [C_BESZERZESIAR],
                 [C_DARABSZAM],
                 [C_FOKONYVISZAM],
                 [C_GYARISZAM],
                 [C_GYARTASIEV],
                 [C_HALOZATIKAPCSOLAT],
                 [C_HORDOZHATO],
                 [C_INTERNETKAPCSOLAT],
                 [C_KATEGORIA],
                 [C_LELTARISZAM],
                 [C_MEGJEGYZES],
                 [C_MULTIMEDIAS],
                 [C_NEV],
                 [C_PEDAGOGUSHOZZAFERHET],
                 [C_SZALLITO],
                 [C_TANULOHOZZAFERHET],
                 [C_TARTOZEK],
                 [C_TARTOZEKHIVATKOZAS],
                 [C_TIPUS],
                 [C_CELJA],
                 [C_JELLEGE],
                 [C_MENNYISEGIEGYSEG],
                 [C_VONALKOD],
                 [C_TEREMID],
                 [C_FELELOSID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID]
        FROM     [dbo].[T_ESZKOZ]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_ESZKOZ] OFF;
    END

DROP TABLE [dbo].[T_ESZKOZ];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_ESZKOZ]', N'T_ESZKOZ';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40250981]', N'PK_T_4025098', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_4025098_4025109]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025098_4025109]
    ON [dbo].[T_ESZKOZ]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_KATEGORIA] ASC);


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_4025098_4025119]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025098_4025119]
    ON [dbo].[T_ESZKOZ]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TIPUS] ASC);


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_4025098_4025247]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025098_4025247]
    ON [dbo].[T_ESZKOZ]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_CELJA] ASC);


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_4025098_4025248]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025098_4025248]
    ON [dbo].[T_ESZKOZ]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_JELLEGE] ASC);


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_4025098_4025249]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025098_4025249]
    ON [dbo].[T_ESZKOZ]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MENNYISEGIEGYSEG] ASC);


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_4025130_4025128]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025130_4025128]
    ON [dbo].[T_ESZKOZ]([C_TEREMID] ASC);


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_4025281_4025280]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025281_4025280]
    ON [dbo].[T_ESZKOZ]([C_FELELOSID] ASC);


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_402509802_402509800]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402509802_402509800]
    ON [dbo].[T_ESZKOZ]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_ESZKOZ].[I_T_402509805_402509803]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402509805_402509803]
    ON [dbo].[T_ESZKOZ]([C_TANEVID] ASC);


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_NEBULO] (
    [ID]                             INT             IDENTITY (1, 1) NOT NULL,
    [C_ALLAMPOLGARSAGA]              INT             NULL,
    [C_ALLAMPOLGARSAGA2]             INT             NULL,
    [C_ANYANYELVE]                   INT             NULL,
    [C_AZISKOLAALAKOHELYETOLEGYKILO] CHAR (1)        DEFAULT ('F') NULL,
    [C_EDESANYJASZULETESICSALADINEV] NVARCHAR (255)  NOT NULL,
    [C_EDESANYJASZULETESIUTONEVE]    NVARCHAR (255)  NOT NULL,
    [C_ELETVITELSZERUOTTLAKASKORZET] CHAR (1)        DEFAULT ('T') NULL,
    [C_ELOTAG]                       NVARCHAR (20)   NULL,
    [C_ETIKAHITERKOLCSTAN]           INT             NULL,
    [C_FELVETELSTATUSZA]             INT             NULL,
    [C_KEPVISELOCIMEAZONOS]          CHAR (1)        DEFAULT ('T') NOT NULL,
    [C_MEGJEGYZES]                   NVARCHAR (MAX)  NULL,
    [C_NEME]                         INT             NULL,
    [C_OKTATASIAZONOSITOSZAMA]       NVARCHAR (20)   NULL,
    [C_OVODAAZONOSITO]               NVARCHAR (20)   NULL,
    [C_OVODANEVE]                    NVARCHAR (255)  NULL,
    [C_SZAKERTOIBIZOTTSAGKORMANYHIV] CHAR (1)        DEFAULT ('F') NULL,
    [C_SZULETESICSALADINEVE]         NVARCHAR (255)  NULL,
    [C_SZULETESIHELY]                NVARCHAR (50)   NOT NULL,
    [C_SZULETESIIDO]                 DATETIME        NOT NULL,
    [C_SZULETESIORSZAG]              INT             NOT NULL,
    [C_SZULETESIUTONEVE]             NVARCHAR (255)  NULL,
    [C_SZULOJENEKMUNKAHELYEAZISKOLA] CHAR (1)        DEFAULT ('F') NULL,
    [C_SZULOJETESTVERETARTOSANBETEG] CHAR (1)        DEFAULT ('F') NULL,
    [C_TAGOZAT]                      INT             NULL,
    [C_TANULOCSALADINEVE]            NVARCHAR (255)  NOT NULL,
    [C_TANULOUTONEVE]                NVARCHAR (255)  NOT NULL,
    [C_TESTVEREAZINTEZMENYTANULOJA]  CHAR (1)        DEFAULT ('F') NULL,
    [C_TKEMAILCIM]                   NVARCHAR (255)  NULL,
    [C_TKNEVE]                       NVARCHAR (200)  NULL,
    [C_TKROKONSAGIFOKA]              INT             NULL,
    [C_TKTELEFON]                    NVARCHAR (20)   NULL,
    [C_AJTO]                         NVARCHAR (30)   NULL,
    [C_EMELET]                       NVARCHAR (40)   NULL,
    [C_HAZSZAM]                      NVARCHAR (30)   NULL,
    [C_HELYSEGNEV]                   NVARCHAR (255)  NULL,
    [C_IRANYITOSZAM]                 NVARCHAR (20)   NULL,
    [C_KOZTERULETJELLEGE]            INT             NULL,
    [C_KOZTERULETNEV]                NVARCHAR (40)   NULL,
    [C_TKAJTO]                       NVARCHAR (30)   NULL,
    [C_TKEMELET]                     NVARCHAR (40)   NULL,
    [C_TKHAZSZAM]                    NVARCHAR (30)   NULL,
    [C_TKHELYSEGNEV]                 NVARCHAR (255)  NULL,
    [C_TKIRANYITOSZAM]               NVARCHAR (20)   NULL,
    [C_TKKOZTERULETJELLEGE]          INT             NULL,
    [C_TKKOZTERULETNEV]              NVARCHAR (40)   NULL,
    [C_BTM]                          CHAR (1)        DEFAULT ('F') NULL,
    [C_BTMLETSZAMSULY]               NUMERIC (10, 2) DEFAULT ((1)) NULL,
    [C_SNI]                          CHAR (1)        DEFAULT ('F') NULL,
    [C_SNILETSZAMSULY]               NUMERIC (10, 2) DEFAULT ((1)) NULL,
    [C_THAJTO]                       NVARCHAR (30)   NULL,
    [C_THHAZSZAM]                    NVARCHAR (30)   NULL,
    [C_THHELYSEGNEV]                 NVARCHAR (255)  NULL,
    [C_THIRANYITOSZAM]               NVARCHAR (20)   NULL,
    [C_THKOZTERULETJELLEGE]          INT             NULL,
    [C_THKOZTERULETNEV]              NVARCHAR (40)   NULL,
    [C_THEMELET]                     NVARCHAR (40)   NULL,
    [C_TARTOZKODASIHELYAZONOS]       CHAR (1)        DEFAULT ('T') NOT NULL,
    [C_FELADATELLATASIHELYID]        INT             NULL,
    [C_INTEZMENYID]                  INT             NOT NULL,
    [C_TANEVID]                      INT             NOT NULL,
    [TOROLT]                         CHAR (1)        DEFAULT ('F') NULL,
    [SERIAL]                         INT             NULL,
    [LASTCHANGED]                    DATETIME        DEFAULT (getdate()) NULL,
    [CREATED]                        DATETIME        DEFAULT (getdate()) NULL,
    [MODIFIER]                       INT             NULL,
    [CREATOR]                        INT             NULL,
    [NNID]                           INT             NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40273631] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_NEBULO])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_NEBULO] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_NEBULO] ([ID], [C_ALLAMPOLGARSAGA], [C_ALLAMPOLGARSAGA2], [C_ANYANYELVE], [C_AZISKOLAALAKOHELYETOLEGYKILO], [C_EDESANYJASZULETESICSALADINEV], [C_EDESANYJASZULETESIUTONEVE], [C_ELETVITELSZERUOTTLAKASKORZET], [C_ELOTAG], [C_ETIKAHITERKOLCSTAN], [C_FELVETELSTATUSZA], [C_KEPVISELOCIMEAZONOS], [C_MEGJEGYZES], [C_NEME], [C_OKTATASIAZONOSITOSZAMA], [C_OVODAAZONOSITO], [C_OVODANEVE], [C_SZAKERTOIBIZOTTSAGKORMANYHIV], [C_SZULETESICSALADINEVE], [C_SZULETESIHELY], [C_SZULETESIIDO], [C_SZULETESIORSZAG], [C_SZULETESIUTONEVE], [C_SZULOJENEKMUNKAHELYEAZISKOLA], [C_SZULOJETESTVERETARTOSANBETEG], [C_TAGOZAT], [C_TANULOCSALADINEVE], [C_TANULOUTONEVE], [C_TESTVEREAZINTEZMENYTANULOJA], [C_TKEMAILCIM], [C_TKNEVE], [C_TKROKONSAGIFOKA], [C_TKTELEFON], [C_AJTO], [C_EMELET], [C_HAZSZAM], [C_HELYSEGNEV], [C_IRANYITOSZAM], [C_KOZTERULETJELLEGE], [C_KOZTERULETNEV], [C_TKAJTO], [C_TKEMELET], [C_TKHAZSZAM], [C_TKHELYSEGNEV], [C_TKIRANYITOSZAM], [C_TKKOZTERULETJELLEGE], [C_TKKOZTERULETNEV], [C_FELADATELLATASIHELYID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID])
        SELECT   [ID],
                 [C_ALLAMPOLGARSAGA],
                 [C_ALLAMPOLGARSAGA2],
                 [C_ANYANYELVE],
                 [C_AZISKOLAALAKOHELYETOLEGYKILO],
                 [C_EDESANYJASZULETESICSALADINEV],
                 [C_EDESANYJASZULETESIUTONEVE],
                 [C_ELETVITELSZERUOTTLAKASKORZET],
                 [C_ELOTAG],
                 [C_ETIKAHITERKOLCSTAN],
                 [C_FELVETELSTATUSZA],
                 [C_KEPVISELOCIMEAZONOS],
                 [C_MEGJEGYZES],
                 [C_NEME],
                 [C_OKTATASIAZONOSITOSZAMA],
                 [C_OVODAAZONOSITO],
                 [C_OVODANEVE],
                 [C_SZAKERTOIBIZOTTSAGKORMANYHIV],
                 [C_SZULETESICSALADINEVE],
                 [C_SZULETESIHELY],
                 [C_SZULETESIIDO],
                 [C_SZULETESIORSZAG],
                 [C_SZULETESIUTONEVE],
                 [C_SZULOJENEKMUNKAHELYEAZISKOLA],
                 [C_SZULOJETESTVERETARTOSANBETEG],
                 [C_TAGOZAT],
                 [C_TANULOCSALADINEVE],
                 [C_TANULOUTONEVE],
                 [C_TESTVEREAZINTEZMENYTANULOJA],
                 [C_TKEMAILCIM],
                 [C_TKNEVE],
                 [C_TKROKONSAGIFOKA],
                 [C_TKTELEFON],
                 [C_AJTO],
                 [C_EMELET],
                 [C_HAZSZAM],
                 [C_HELYSEGNEV],
                 [C_IRANYITOSZAM],
                 [C_KOZTERULETJELLEGE],
                 [C_KOZTERULETNEV],
                 [C_TKAJTO],
                 [C_TKEMELET],
                 [C_TKHAZSZAM],
                 [C_TKHELYSEGNEV],
                 [C_TKIRANYITOSZAM],
                 [C_TKKOZTERULETJELLEGE],
                 [C_TKKOZTERULETNEV],
                 [C_FELADATELLATASIHELYID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID]
        FROM     [dbo].[T_NEBULO]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_NEBULO] OFF;
    END

DROP TABLE [dbo].[T_NEBULO];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_NEBULO]', N'T_NEBULO';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40273631]', N'PK_T_4027363', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027364]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027364]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ALLAMPOLGARSAGA] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027365]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027365]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ALLAMPOLGARSAGA2] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027366]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027366]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ANYANYELVE] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027373]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027373]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_FELVETELSTATUSZA] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027376]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027376]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_NEME] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027384]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027384]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_SZULETESIORSZAG] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027388]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027388]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TAGOZAT] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027394]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027394]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TKROKONSAGIFOKA] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027407]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027407]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_KOZTERULETJELLEGE] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027414]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027414]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TKKOZTERULETJELLEGE] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027363_4027483]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027483]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_THKOZTERULETJELLEGE] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_4027460_4027459]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027460_4027459]
    ON [dbo].[T_NEBULO]([C_FELADATELLATASIHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_402736302_402736300]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402736302_402736300]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_NEBULO].[I_T_402736305_402736303]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402736305_402736303]
    ON [dbo].[T_NEBULO]([C_TANEVID] ASC);


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_TEREM] (
    [ID]                     INT            IDENTITY (1, 1) NOT NULL,
    [C_AKTIV]                CHAR (1)       DEFAULT ('T') NULL,
    [C_JELLEG]               INT            NOT NULL,
    [C_KAPACITAS]            INT            NULL,
    [C_NEV]                  NVARCHAR (255) NOT NULL,
    [C_TERULET]              INT            NULL,
    [C_TOBBORATLEHETTARTANI] CHAR (1)       DEFAULT ('F') NULL,
    [C_VIZSGAKAPACITAS]      INT            NULL,
    [C_WIFILEFEDETT]         CHAR (1)       DEFAULT ('F') NULL,
    [C_MUKODESIHELYID]       INT            NOT NULL,
    [C_TEREMFELELOSID]       INT            NULL,
    [C_INTEZMENYID]          INT            NOT NULL,
    [C_TANEVID]              INT            NOT NULL,
    [TOROLT]                 CHAR (1)       DEFAULT ('F') NULL,
    [SERIAL]                 INT            NULL,
    [LASTCHANGED]            DATETIME       DEFAULT (getdate()) NULL,
    [CREATED]                DATETIME       DEFAULT (getdate()) NULL,
    [MODIFIER]               INT            NULL,
    [CREATOR]                INT            NULL,
    [NNID]                   INT            NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40251221] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_TEREM])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TEREM] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_TEREM] ([ID], [C_AKTIV], [C_JELLEG], [C_KAPACITAS], [C_NEV], [C_TERULET], [C_TOBBORATLEHETTARTANI], [C_VIZSGAKAPACITAS], [C_MUKODESIHELYID], [C_TEREMFELELOSID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID])
        SELECT   [ID],
                 [C_AKTIV],
                 [C_JELLEG],
                 [C_KAPACITAS],
                 [C_NEV],
                 [C_TERULET],
                 [C_TOBBORATLEHETTARTANI],
                 [C_VIZSGAKAPACITAS],
                 [C_MUKODESIHELYID],
                 [C_TEREMFELELOSID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID]
        FROM     [dbo].[T_TEREM]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TEREM] OFF;
    END

DROP TABLE [dbo].[T_TEREM];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_TEREM]', N'T_TEREM';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40251221]', N'PK_T_4025122', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_TEREM].[I_T_4025122_4025124]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025122_4025124]
    ON [dbo].[T_TEREM]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_JELLEG] ASC);


GO
PRINT N'Creating [dbo].[T_TEREM].[I_T_4025412_4025410]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025412_4025410]
    ON [dbo].[T_TEREM]([C_MUKODESIHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_TEREM].[I_T_4026131_4026130]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026131_4026130]
    ON [dbo].[T_TEREM]([C_TEREMFELELOSID] ASC);


GO
PRINT N'Creating [dbo].[T_TEREM].[I_T_402512202_402512200]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402512202_402512200]
    ON [dbo].[T_TEREM]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TEREM].[I_T_402512205_402512203]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402512205_402512203]
    ON [dbo].[T_TEREM]([C_TANEVID] ASC);


GO
PRINT N'Creating [dbo].[T_TEREM].[UK_TEREMNEV]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [UK_TEREMNEV]
    ON [dbo].[T_TEREM]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_NEV] ASC, [C_MUKODESIHELYID] ASC) WHERE ([TOROLT]='F');


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


GO
ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH NOCHECK
    ADD CONSTRAINT [FK_4026694_4026695] FOREIGN KEY ([C_ENAPLOFORRASA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ENAPLOFORRAS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH NOCHECK
    ADD CONSTRAINT [FK_4026694_4026696] FOREIGN KEY ([C_ENAPLOINFRAJA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ENAPLOINFRA] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH NOCHECK
    ADD CONSTRAINT [FK_4026694_4026697] FOREIGN KEY ([C_ENAPLOTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ENAPLOTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH NOCHECK
    ADD CONSTRAINT [FK_4026694_4026706] FOREIGN KEY ([C_SZERZODOSZEMELYPOZICIOJA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_SZERZODOSZEMELYPOZICIO] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH NOCHECK
    ADD CONSTRAINT [FK_4026694_4027417] FOREIGN KEY ([C_ENAPLOHASZNALATA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ENAPLOHASZNALAT] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH NOCHECK
    ADD CONSTRAINT [FK_4026694_4027419] FOREIGN KEY ([C_WIFIELERES], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_WIFIELERES] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


GO
ALTER TABLE [dbo].[T_ESZKOZ] WITH NOCHECK
    ADD CONSTRAINT [FK_4025098_4025109] FOREIGN KEY ([C_KATEGORIA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ESZKOZKATEGORIA] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ESZKOZ] WITH NOCHECK
    ADD CONSTRAINT [FK_4025098_4025119] FOREIGN KEY ([C_TIPUS], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ESZKOZTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ESZKOZ] WITH NOCHECK
    ADD CONSTRAINT [FK_4025098_4025247] FOREIGN KEY ([C_CELJA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ESZKOZCEL] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ESZKOZ] WITH NOCHECK
    ADD CONSTRAINT [FK_4025098_4025248] FOREIGN KEY ([C_JELLEGE], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ESZKOZJELLEG] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_ESZKOZ] WITH NOCHECK
    ADD CONSTRAINT [FK_4025098_4025249] FOREIGN KEY ([C_MENNYISEGIEGYSEG], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_MERTEKEGYSEGTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


GO
ALTER TABLE [dbo].[T_ESZKOZ] WITH NOCHECK
    ADD CONSTRAINT [FK_4025281_4025280] FOREIGN KEY ([C_FELELOSID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


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


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


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027414] FOREIGN KEY ([C_TKKOZTERULETJELLEGE], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_KOZTERULETJELLEG] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027364] FOREIGN KEY ([C_ALLAMPOLGARSAGA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ALLAMPOLGARSAG] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027365] FOREIGN KEY ([C_ALLAMPOLGARSAGA2], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ALLAMPOLGARSAG] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027366] FOREIGN KEY ([C_ANYANYELVE], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ANYANYELV] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027373] FOREIGN KEY ([C_FELVETELSTATUSZA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_FELVETELSTATUSZATIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027376] FOREIGN KEY ([C_NEME], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_NEM] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027384] FOREIGN KEY ([C_SZULETESIORSZAG], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ORSZAGTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027388] FOREIGN KEY ([C_TAGOZAT], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_TAGOZATTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027394] FOREIGN KEY ([C_TKROKONSAGIFOKA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ROKONSAGIFOK] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027407] FOREIGN KEY ([C_KOZTERULETJELLEGE], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_KOZTERULETJELLEG] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


GO
ALTER TABLE [dbo].[T_NEBULO] WITH NOCHECK
    ADD CONSTRAINT [FK_4027363_4027483] FOREIGN KEY ([C_THKOZTERULETJELLEGE], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_KOZTERULETJELLEG] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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_402512202_402512200]...';


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


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


GO
ALTER TABLE [dbo].[T_TEREM] WITH NOCHECK
    ADD CONSTRAINT [FK_4025122_4025124] FOREIGN KEY ([C_JELLEG], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_TEREMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_TEREM] WITH NOCHECK
    ADD CONSTRAINT [FK_4025412_4025410] FOREIGN KEY ([C_MUKODESIHELYID]) REFERENCES [dbo].[T_MUKODESIHELY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TEREM] WITH NOCHECK
    ADD CONSTRAINT [FK_4026131_4026130] FOREIGN KEY ([C_TEREMFELELOSID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


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


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


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


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


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_TERMEK_JOGYAKORLATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4025913_4025912] FOREIGN KEY ([C_TERMEKID]) REFERENCES [dbo].[T_TEREM] ([ID]);


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


GO

  CREATE TRIGGER tr_adatszolgaltatasenaploLog ON T_ADATSZOLGALTATASENAPLO 
  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_ENAPLOFORRASA AS nvarchar(MAX)) AS C_ENAPLOFORRASA, CAST( C_ENAPLOINFRAJA AS nvarchar(MAX)) AS C_ENAPLOINFRAJA, CAST( C_ENAPLOTIPUSA AS nvarchar(MAX)) AS C_ENAPLOTIPUSA, CAST( C_ISELLENORZO AS nvarchar(MAX)) AS C_ISELLENORZO, CAST( C_ISENAPLO AS nvarchar(MAX)) AS C_ISENAPLO, CAST( C_ISPAPIRNAPLO AS nvarchar(MAX)) AS C_ISPAPIRNAPLO, CONVERT(nvarchar(MAX), C_SZERZODESKEZDETE, 121) AS C_SZERZODESKEZDETE, CONVERT(nvarchar(MAX), C_SZERZODESLEJARATA, 121) AS C_SZERZODESLEJARATA, CAST( C_SZERZODESOSSZEGE AS nvarchar(MAX)) AS C_SZERZODESOSSZEGE, CAST( C_SZERZODOSZEMELYNEVE AS nvarchar(MAX)) AS C_SZERZODOSZEMELYNEVE, CAST( C_SZERZODOSZEMELYPOZICIOJA AS nvarchar(MAX)) AS C_SZERZODOSZEMELYPOZICIOJA, CAST( C_SZERZODESFILENAME AS nvarchar(MAX)) AS C_SZERZODESFILENAME, CAST( C_ENAPLOHASZNALATA AS nvarchar(MAX)) AS C_ENAPLOHASZNALATA, CAST( C_PROJEKTOROKSZAMA AS nvarchar(MAX)) AS C_PROJEKTOROKSZAMA, CAST( C_WIFIELERES AS nvarchar(MAX)) AS C_WIFIELERES, CAST( C_MUKODOPROJEKTOROKSZAMA AS nvarchar(MAX)) AS C_MUKODOPROJEKTOROKSZAMA, CAST( C_TERMEKSZAMA AS nvarchar(MAX)) AS C_TERMEKSZAMA, CAST( C_WIFILEFEDETTTERMEKSZAMA AS nvarchar(MAX)) AS C_WIFILEFEDETTTERMEKSZAMA, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_ENAPLOFORRASA, C_ENAPLOINFRAJA, C_ENAPLOTIPUSA, C_ISELLENORZO, C_ISENAPLO, C_ISPAPIRNAPLO, C_SZERZODESKEZDETE, C_SZERZODESLEJARATA, C_SZERZODESOSSZEGE, C_SZERZODOSZEMELYNEVE, C_SZERZODOSZEMELYPOZICIOJA, C_SZERZODESFILENAME, C_ENAPLOHASZNALATA, C_PROJEKTOROKSZAMA, C_WIFIELERES, C_MUKODOPROJEKTOROKSZAMA, C_TERMEKSZAMA, C_WIFILEFEDETTTERMEKSZAMA, TOROLT, 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_ENAPLOFORRASA AS nvarchar(MAX)) AS C_ENAPLOFORRASA, CAST( C_ENAPLOINFRAJA AS nvarchar(MAX)) AS C_ENAPLOINFRAJA, CAST( C_ENAPLOTIPUSA AS nvarchar(MAX)) AS C_ENAPLOTIPUSA, CAST( C_ISELLENORZO AS nvarchar(MAX)) AS C_ISELLENORZO, CAST( C_ISENAPLO AS nvarchar(MAX)) AS C_ISENAPLO, CAST( C_ISPAPIRNAPLO AS nvarchar(MAX)) AS C_ISPAPIRNAPLO, CONVERT(nvarchar(MAX), C_SZERZODESKEZDETE, 121) AS C_SZERZODESKEZDETE, CONVERT(nvarchar(MAX), C_SZERZODESLEJARATA, 121) AS C_SZERZODESLEJARATA, CAST( C_SZERZODESOSSZEGE AS nvarchar(MAX)) AS C_SZERZODESOSSZEGE, CAST( C_SZERZODOSZEMELYNEVE AS nvarchar(MAX)) AS C_SZERZODOSZEMELYNEVE, CAST( C_SZERZODOSZEMELYPOZICIOJA AS nvarchar(MAX)) AS C_SZERZODOSZEMELYPOZICIOJA, CAST( C_SZERZODESFILENAME AS nvarchar(MAX)) AS C_SZERZODESFILENAME, CAST( C_ENAPLOHASZNALATA AS nvarchar(MAX)) AS C_ENAPLOHASZNALATA, CAST( C_PROJEKTOROKSZAMA AS nvarchar(MAX)) AS C_PROJEKTOROKSZAMA, CAST( C_WIFIELERES AS nvarchar(MAX)) AS C_WIFIELERES, CAST( C_MUKODOPROJEKTOROKSZAMA AS nvarchar(MAX)) AS C_MUKODOPROJEKTOROKSZAMA, CAST( C_TERMEKSZAMA AS nvarchar(MAX)) AS C_TERMEKSZAMA, CAST( C_WIFILEFEDETTTERMEKSZAMA AS nvarchar(MAX)) AS C_WIFILEFEDETTTERMEKSZAMA, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_ENAPLOFORRASA, C_ENAPLOINFRAJA, C_ENAPLOTIPUSA, C_ISELLENORZO, C_ISENAPLO, C_ISPAPIRNAPLO, C_SZERZODESKEZDETE, C_SZERZODESLEJARATA, C_SZERZODESOSSZEGE, C_SZERZODOSZEMELYNEVE, C_SZERZODOSZEMELYPOZICIOJA, C_SZERZODESFILENAME, C_ENAPLOHASZNALATA, C_PROJEKTOROKSZAMA, C_WIFIELERES, C_MUKODOPROJEKTOROKSZAMA, C_TERMEKSZAMA, C_WIFILEFEDETTTERMEKSZAMA, TOROLT, 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_ADATSZOLGALTATASENAPLO', '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_ADATSZOLGALTATASENAPLO', '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_ADATSZOLGALTATASENAPLO', 
        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_eszkozLog]...';


GO

  CREATE TRIGGER tr_eszkozLog ON T_ESZKOZ 
  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_BERELT AS nvarchar(MAX)) AS C_BERELT, CONVERT(nvarchar(MAX), C_BESZERZESDATUM, 121) AS C_BESZERZESDATUM, CAST( C_BESZERZESIAR AS nvarchar(MAX)) AS C_BESZERZESIAR, CAST( C_DARABSZAM AS nvarchar(MAX)) AS C_DARABSZAM, CAST( C_FOKONYVISZAM AS nvarchar(MAX)) AS C_FOKONYVISZAM, CAST( C_GYARISZAM AS nvarchar(MAX)) AS C_GYARISZAM, CAST( C_GYARTASIEV AS nvarchar(MAX)) AS C_GYARTASIEV, CAST( C_HALOZATIKAPCSOLAT AS nvarchar(MAX)) AS C_HALOZATIKAPCSOLAT, CAST( C_HORDOZHATO AS nvarchar(MAX)) AS C_HORDOZHATO, CAST( C_INTERNETKAPCSOLAT AS nvarchar(MAX)) AS C_INTERNETKAPCSOLAT, CAST( C_KATEGORIA AS nvarchar(MAX)) AS C_KATEGORIA, CAST( C_LELTARISZAM AS nvarchar(MAX)) AS C_LELTARISZAM, CAST( C_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_MULTIMEDIAS AS nvarchar(MAX)) AS C_MULTIMEDIAS, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_PEDAGOGUSHOZZAFERHET AS nvarchar(MAX)) AS C_PEDAGOGUSHOZZAFERHET, CAST( C_SZALLITO AS nvarchar(MAX)) AS C_SZALLITO, CAST( C_TANULOHOZZAFERHET AS nvarchar(MAX)) AS C_TANULOHOZZAFERHET, CAST( C_TARTOZEK AS nvarchar(MAX)) AS C_TARTOZEK, CAST( C_TARTOZEKHIVATKOZAS AS nvarchar(MAX)) AS C_TARTOZEKHIVATKOZAS, CAST( C_TIPUS AS nvarchar(MAX)) AS C_TIPUS, CAST( C_CELJA AS nvarchar(MAX)) AS C_CELJA, CAST( C_JELLEGE AS nvarchar(MAX)) AS C_JELLEGE, CAST( C_MENNYISEGIEGYSEG AS nvarchar(MAX)) AS C_MENNYISEGIEGYSEG, CAST( C_VONALKOD AS nvarchar(MAX)) AS C_VONALKOD, CAST( C_NEMMUKODIK AS nvarchar(MAX)) AS C_NEMMUKODIK, CAST( C_TEREMID AS nvarchar(MAX)) AS C_TEREMID, CAST( C_FELELOSID AS nvarchar(MAX)) AS C_FELELOSID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_BERELT, C_BESZERZESDATUM, C_BESZERZESIAR, C_DARABSZAM, C_FOKONYVISZAM, C_GYARISZAM, C_GYARTASIEV, C_HALOZATIKAPCSOLAT, C_HORDOZHATO, C_INTERNETKAPCSOLAT, C_KATEGORIA, C_LELTARISZAM, C_MEGJEGYZES, C_MULTIMEDIAS, C_NEV, C_PEDAGOGUSHOZZAFERHET, C_SZALLITO, C_TANULOHOZZAFERHET, C_TARTOZEK, C_TARTOZEKHIVATKOZAS, C_TIPUS, C_CELJA, C_JELLEGE, C_MENNYISEGIEGYSEG, C_VONALKOD, C_NEMMUKODIK, C_TEREMID, C_FELELOSID, TOROLT, 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_BERELT AS nvarchar(MAX)) AS C_BERELT, CONVERT(nvarchar(MAX), C_BESZERZESDATUM, 121) AS C_BESZERZESDATUM, CAST( C_BESZERZESIAR AS nvarchar(MAX)) AS C_BESZERZESIAR, CAST( C_DARABSZAM AS nvarchar(MAX)) AS C_DARABSZAM, CAST( C_FOKONYVISZAM AS nvarchar(MAX)) AS C_FOKONYVISZAM, CAST( C_GYARISZAM AS nvarchar(MAX)) AS C_GYARISZAM, CAST( C_GYARTASIEV AS nvarchar(MAX)) AS C_GYARTASIEV, CAST( C_HALOZATIKAPCSOLAT AS nvarchar(MAX)) AS C_HALOZATIKAPCSOLAT, CAST( C_HORDOZHATO AS nvarchar(MAX)) AS C_HORDOZHATO, CAST( C_INTERNETKAPCSOLAT AS nvarchar(MAX)) AS C_INTERNETKAPCSOLAT, CAST( C_KATEGORIA AS nvarchar(MAX)) AS C_KATEGORIA, CAST( C_LELTARISZAM AS nvarchar(MAX)) AS C_LELTARISZAM, CAST( C_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_MULTIMEDIAS AS nvarchar(MAX)) AS C_MULTIMEDIAS, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_PEDAGOGUSHOZZAFERHET AS nvarchar(MAX)) AS C_PEDAGOGUSHOZZAFERHET, CAST( C_SZALLITO AS nvarchar(MAX)) AS C_SZALLITO, CAST( C_TANULOHOZZAFERHET AS nvarchar(MAX)) AS C_TANULOHOZZAFERHET, CAST( C_TARTOZEK AS nvarchar(MAX)) AS C_TARTOZEK, CAST( C_TARTOZEKHIVATKOZAS AS nvarchar(MAX)) AS C_TARTOZEKHIVATKOZAS, CAST( C_TIPUS AS nvarchar(MAX)) AS C_TIPUS, CAST( C_CELJA AS nvarchar(MAX)) AS C_CELJA, CAST( C_JELLEGE AS nvarchar(MAX)) AS C_JELLEGE, CAST( C_MENNYISEGIEGYSEG AS nvarchar(MAX)) AS C_MENNYISEGIEGYSEG, CAST( C_VONALKOD AS nvarchar(MAX)) AS C_VONALKOD, CAST( C_NEMMUKODIK AS nvarchar(MAX)) AS C_NEMMUKODIK, CAST( C_TEREMID AS nvarchar(MAX)) AS C_TEREMID, CAST( C_FELELOSID AS nvarchar(MAX)) AS C_FELELOSID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_BERELT, C_BESZERZESDATUM, C_BESZERZESIAR, C_DARABSZAM, C_FOKONYVISZAM, C_GYARISZAM, C_GYARTASIEV, C_HALOZATIKAPCSOLAT, C_HORDOZHATO, C_INTERNETKAPCSOLAT, C_KATEGORIA, C_LELTARISZAM, C_MEGJEGYZES, C_MULTIMEDIAS, C_NEV, C_PEDAGOGUSHOZZAFERHET, C_SZALLITO, C_TANULOHOZZAFERHET, C_TARTOZEK, C_TARTOZEKHIVATKOZAS, C_TIPUS, C_CELJA, C_JELLEGE, C_MENNYISEGIEGYSEG, C_VONALKOD, C_NEMMUKODIK, C_TEREMID, C_FELELOSID, TOROLT, 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_ESZKOZ', '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_ESZKOZ', '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_ESZKOZ', 
        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_nebuloLog]...';


GO

  CREATE TRIGGER tr_nebuloLog ON T_NEBULO 
  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_ALLAMPOLGARSAGA AS nvarchar(MAX)) AS C_ALLAMPOLGARSAGA, CAST( C_ALLAMPOLGARSAGA2 AS nvarchar(MAX)) AS C_ALLAMPOLGARSAGA2, CAST( C_ANYANYELVE AS nvarchar(MAX)) AS C_ANYANYELVE, CAST( C_AZISKOLAALAKOHELYETOLEGYKILO AS nvarchar(MAX)) AS C_AZISKOLAALAKOHELYETOLEGYKILO, CAST( C_EDESANYJASZULETESICSALADINEV AS nvarchar(MAX)) AS C_EDESANYJASZULETESICSALADINEV, CAST( C_EDESANYJASZULETESIUTONEVE AS nvarchar(MAX)) AS C_EDESANYJASZULETESIUTONEVE, CAST( C_ELETVITELSZERUOTTLAKASKORZET AS nvarchar(MAX)) AS C_ELETVITELSZERUOTTLAKASKORZET, CAST( C_ELOTAG AS nvarchar(MAX)) AS C_ELOTAG, CAST( C_ETIKAHITERKOLCSTAN AS nvarchar(MAX)) AS C_ETIKAHITERKOLCSTAN, CAST( C_FELVETELSTATUSZA AS nvarchar(MAX)) AS C_FELVETELSTATUSZA, CAST( C_KEPVISELOCIMEAZONOS AS nvarchar(MAX)) AS C_KEPVISELOCIMEAZONOS, CAST( C_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_NEME AS nvarchar(MAX)) AS C_NEME, CAST( C_OKTATASIAZONOSITOSZAMA AS nvarchar(MAX)) AS C_OKTATASIAZONOSITOSZAMA, CAST( C_OVODAAZONOSITO AS nvarchar(MAX)) AS C_OVODAAZONOSITO, CAST( C_OVODANEVE AS nvarchar(MAX)) AS C_OVODANEVE, CAST( C_SZAKERTOIBIZOTTSAGKORMANYHIV AS nvarchar(MAX)) AS C_SZAKERTOIBIZOTTSAGKORMANYHIV, CAST( C_SZULETESICSALADINEVE AS nvarchar(MAX)) AS C_SZULETESICSALADINEVE, CAST( C_SZULETESIHELY AS nvarchar(MAX)) AS C_SZULETESIHELY, CONVERT(nvarchar(MAX), C_SZULETESIIDO, 121) AS C_SZULETESIIDO, CAST( C_SZULETESIORSZAG AS nvarchar(MAX)) AS C_SZULETESIORSZAG, CAST( C_SZULETESIUTONEVE AS nvarchar(MAX)) AS C_SZULETESIUTONEVE, CAST( C_SZULOJENEKMUNKAHELYEAZISKOLA AS nvarchar(MAX)) AS C_SZULOJENEKMUNKAHELYEAZISKOLA, CAST( C_SZULOJETESTVERETARTOSANBETEG AS nvarchar(MAX)) AS C_SZULOJETESTVERETARTOSANBETEG, CAST( C_TAGOZAT AS nvarchar(MAX)) AS C_TAGOZAT, CAST( C_TANULOCSALADINEVE AS nvarchar(MAX)) AS C_TANULOCSALADINEVE, CAST( C_TANULOUTONEVE AS nvarchar(MAX)) AS C_TANULOUTONEVE, CAST( C_TESTVEREAZINTEZMENYTANULOJA AS nvarchar(MAX)) AS C_TESTVEREAZINTEZMENYTANULOJA, CAST( C_TKEMAILCIM AS nvarchar(MAX)) AS C_TKEMAILCIM, CAST( C_TKNEVE AS nvarchar(MAX)) AS C_TKNEVE, CAST( C_TKROKONSAGIFOKA AS nvarchar(MAX)) AS C_TKROKONSAGIFOKA, CAST( C_TKTELEFON AS nvarchar(MAX)) AS C_TKTELEFON, CAST( C_AJTO AS nvarchar(MAX)) AS C_AJTO, CAST( C_EMELET AS nvarchar(MAX)) AS C_EMELET, CAST( C_HAZSZAM AS nvarchar(MAX)) AS C_HAZSZAM, CAST( C_HELYSEGNEV AS nvarchar(MAX)) AS C_HELYSEGNEV, CAST( C_IRANYITOSZAM AS nvarchar(MAX)) AS C_IRANYITOSZAM, CAST( C_KOZTERULETJELLEGE AS nvarchar(MAX)) AS C_KOZTERULETJELLEGE, CAST( C_KOZTERULETNEV AS nvarchar(MAX)) AS C_KOZTERULETNEV, CAST( C_TKAJTO AS nvarchar(MAX)) AS C_TKAJTO, CAST( C_TKEMELET AS nvarchar(MAX)) AS C_TKEMELET, CAST( C_TKHAZSZAM AS nvarchar(MAX)) AS C_TKHAZSZAM, CAST( C_TKHELYSEGNEV AS nvarchar(MAX)) AS C_TKHELYSEGNEV, CAST( C_TKIRANYITOSZAM AS nvarchar(MAX)) AS C_TKIRANYITOSZAM, CAST( C_TKKOZTERULETJELLEGE AS nvarchar(MAX)) AS C_TKKOZTERULETJELLEGE, CAST( C_TKKOZTERULETNEV AS nvarchar(MAX)) AS C_TKKOZTERULETNEV, CAST( C_BTM AS nvarchar(MAX)) AS C_BTM, CAST( C_BTMLETSZAMSULY AS nvarchar(MAX)) AS C_BTMLETSZAMSULY, CAST( C_SNI AS nvarchar(MAX)) AS C_SNI, CAST( C_SNILETSZAMSULY AS nvarchar(MAX)) AS C_SNILETSZAMSULY, CAST( C_THAJTO AS nvarchar(MAX)) AS C_THAJTO, CAST( C_THHAZSZAM AS nvarchar(MAX)) AS C_THHAZSZAM, CAST( C_THHELYSEGNEV AS nvarchar(MAX)) AS C_THHELYSEGNEV, CAST( C_THIRANYITOSZAM AS nvarchar(MAX)) AS C_THIRANYITOSZAM, CAST( C_THKOZTERULETJELLEGE AS nvarchar(MAX)) AS C_THKOZTERULETJELLEGE, CAST( C_THKOZTERULETNEV AS nvarchar(MAX)) AS C_THKOZTERULETNEV, CAST( C_THEMELET AS nvarchar(MAX)) AS C_THEMELET, CAST( C_TARTOZKODASIHELYAZONOS AS nvarchar(MAX)) AS C_TARTOZKODASIHELYAZONOS, CAST( C_FELADATELLATASIHELYID AS nvarchar(MAX)) AS C_FELADATELLATASIHELYID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_ALLAMPOLGARSAGA, C_ALLAMPOLGARSAGA2, C_ANYANYELVE, C_AZISKOLAALAKOHELYETOLEGYKILO, C_EDESANYJASZULETESICSALADINEV, C_EDESANYJASZULETESIUTONEVE, C_ELETVITELSZERUOTTLAKASKORZET, C_ELOTAG, C_ETIKAHITERKOLCSTAN, C_FELVETELSTATUSZA, C_KEPVISELOCIMEAZONOS, C_MEGJEGYZES, C_NEME, C_OKTATASIAZONOSITOSZAMA, C_OVODAAZONOSITO, C_OVODANEVE, C_SZAKERTOIBIZOTTSAGKORMANYHIV, C_SZULETESICSALADINEVE, C_SZULETESIHELY, C_SZULETESIIDO, C_SZULETESIORSZAG, C_SZULETESIUTONEVE, C_SZULOJENEKMUNKAHELYEAZISKOLA, C_SZULOJETESTVERETARTOSANBETEG, C_TAGOZAT, C_TANULOCSALADINEVE, C_TANULOUTONEVE, C_TESTVEREAZINTEZMENYTANULOJA, C_TKEMAILCIM, C_TKNEVE, C_TKROKONSAGIFOKA, C_TKTELEFON, C_AJTO, C_EMELET, C_HAZSZAM, C_HELYSEGNEV, C_IRANYITOSZAM, C_KOZTERULETJELLEGE, C_KOZTERULETNEV, C_TKAJTO, C_TKEMELET, C_TKHAZSZAM, C_TKHELYSEGNEV, C_TKIRANYITOSZAM, C_TKKOZTERULETJELLEGE, C_TKKOZTERULETNEV, C_BTM, C_BTMLETSZAMSULY, C_SNI, C_SNILETSZAMSULY, C_THAJTO, C_THHAZSZAM, C_THHELYSEGNEV, C_THIRANYITOSZAM, C_THKOZTERULETJELLEGE, C_THKOZTERULETNEV, C_THEMELET, C_TARTOZKODASIHELYAZONOS, C_FELADATELLATASIHELYID, TOROLT, 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_ALLAMPOLGARSAGA AS nvarchar(MAX)) AS C_ALLAMPOLGARSAGA, CAST( C_ALLAMPOLGARSAGA2 AS nvarchar(MAX)) AS C_ALLAMPOLGARSAGA2, CAST( C_ANYANYELVE AS nvarchar(MAX)) AS C_ANYANYELVE, CAST( C_AZISKOLAALAKOHELYETOLEGYKILO AS nvarchar(MAX)) AS C_AZISKOLAALAKOHELYETOLEGYKILO, CAST( C_EDESANYJASZULETESICSALADINEV AS nvarchar(MAX)) AS C_EDESANYJASZULETESICSALADINEV, CAST( C_EDESANYJASZULETESIUTONEVE AS nvarchar(MAX)) AS C_EDESANYJASZULETESIUTONEVE, CAST( C_ELETVITELSZERUOTTLAKASKORZET AS nvarchar(MAX)) AS C_ELETVITELSZERUOTTLAKASKORZET, CAST( C_ELOTAG AS nvarchar(MAX)) AS C_ELOTAG, CAST( C_ETIKAHITERKOLCSTAN AS nvarchar(MAX)) AS C_ETIKAHITERKOLCSTAN, CAST( C_FELVETELSTATUSZA AS nvarchar(MAX)) AS C_FELVETELSTATUSZA, CAST( C_KEPVISELOCIMEAZONOS AS nvarchar(MAX)) AS C_KEPVISELOCIMEAZONOS, CAST( C_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_NEME AS nvarchar(MAX)) AS C_NEME, CAST( C_OKTATASIAZONOSITOSZAMA AS nvarchar(MAX)) AS C_OKTATASIAZONOSITOSZAMA, CAST( C_OVODAAZONOSITO AS nvarchar(MAX)) AS C_OVODAAZONOSITO, CAST( C_OVODANEVE AS nvarchar(MAX)) AS C_OVODANEVE, CAST( C_SZAKERTOIBIZOTTSAGKORMANYHIV AS nvarchar(MAX)) AS C_SZAKERTOIBIZOTTSAGKORMANYHIV, CAST( C_SZULETESICSALADINEVE AS nvarchar(MAX)) AS C_SZULETESICSALADINEVE, CAST( C_SZULETESIHELY AS nvarchar(MAX)) AS C_SZULETESIHELY, CONVERT(nvarchar(MAX), C_SZULETESIIDO, 121) AS C_SZULETESIIDO, CAST( C_SZULETESIORSZAG AS nvarchar(MAX)) AS C_SZULETESIORSZAG, CAST( C_SZULETESIUTONEVE AS nvarchar(MAX)) AS C_SZULETESIUTONEVE, CAST( C_SZULOJENEKMUNKAHELYEAZISKOLA AS nvarchar(MAX)) AS C_SZULOJENEKMUNKAHELYEAZISKOLA, CAST( C_SZULOJETESTVERETARTOSANBETEG AS nvarchar(MAX)) AS C_SZULOJETESTVERETARTOSANBETEG, CAST( C_TAGOZAT AS nvarchar(MAX)) AS C_TAGOZAT, CAST( C_TANULOCSALADINEVE AS nvarchar(MAX)) AS C_TANULOCSALADINEVE, CAST( C_TANULOUTONEVE AS nvarchar(MAX)) AS C_TANULOUTONEVE, CAST( C_TESTVEREAZINTEZMENYTANULOJA AS nvarchar(MAX)) AS C_TESTVEREAZINTEZMENYTANULOJA, CAST( C_TKEMAILCIM AS nvarchar(MAX)) AS C_TKEMAILCIM, CAST( C_TKNEVE AS nvarchar(MAX)) AS C_TKNEVE, CAST( C_TKROKONSAGIFOKA AS nvarchar(MAX)) AS C_TKROKONSAGIFOKA, CAST( C_TKTELEFON AS nvarchar(MAX)) AS C_TKTELEFON, CAST( C_AJTO AS nvarchar(MAX)) AS C_AJTO, CAST( C_EMELET AS nvarchar(MAX)) AS C_EMELET, CAST( C_HAZSZAM AS nvarchar(MAX)) AS C_HAZSZAM, CAST( C_HELYSEGNEV AS nvarchar(MAX)) AS C_HELYSEGNEV, CAST( C_IRANYITOSZAM AS nvarchar(MAX)) AS C_IRANYITOSZAM, CAST( C_KOZTERULETJELLEGE AS nvarchar(MAX)) AS C_KOZTERULETJELLEGE, CAST( C_KOZTERULETNEV AS nvarchar(MAX)) AS C_KOZTERULETNEV, CAST( C_TKAJTO AS nvarchar(MAX)) AS C_TKAJTO, CAST( C_TKEMELET AS nvarchar(MAX)) AS C_TKEMELET, CAST( C_TKHAZSZAM AS nvarchar(MAX)) AS C_TKHAZSZAM, CAST( C_TKHELYSEGNEV AS nvarchar(MAX)) AS C_TKHELYSEGNEV, CAST( C_TKIRANYITOSZAM AS nvarchar(MAX)) AS C_TKIRANYITOSZAM, CAST( C_TKKOZTERULETJELLEGE AS nvarchar(MAX)) AS C_TKKOZTERULETJELLEGE, CAST( C_TKKOZTERULETNEV AS nvarchar(MAX)) AS C_TKKOZTERULETNEV, CAST( C_BTM AS nvarchar(MAX)) AS C_BTM, CAST( C_BTMLETSZAMSULY AS nvarchar(MAX)) AS C_BTMLETSZAMSULY, CAST( C_SNI AS nvarchar(MAX)) AS C_SNI, CAST( C_SNILETSZAMSULY AS nvarchar(MAX)) AS C_SNILETSZAMSULY, CAST( C_THAJTO AS nvarchar(MAX)) AS C_THAJTO, CAST( C_THHAZSZAM AS nvarchar(MAX)) AS C_THHAZSZAM, CAST( C_THHELYSEGNEV AS nvarchar(MAX)) AS C_THHELYSEGNEV, CAST( C_THIRANYITOSZAM AS nvarchar(MAX)) AS C_THIRANYITOSZAM, CAST( C_THKOZTERULETJELLEGE AS nvarchar(MAX)) AS C_THKOZTERULETJELLEGE, CAST( C_THKOZTERULETNEV AS nvarchar(MAX)) AS C_THKOZTERULETNEV, CAST( C_THEMELET AS nvarchar(MAX)) AS C_THEMELET, CAST( C_TARTOZKODASIHELYAZONOS AS nvarchar(MAX)) AS C_TARTOZKODASIHELYAZONOS, CAST( C_FELADATELLATASIHELYID AS nvarchar(MAX)) AS C_FELADATELLATASIHELYID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_ALLAMPOLGARSAGA, C_ALLAMPOLGARSAGA2, C_ANYANYELVE, C_AZISKOLAALAKOHELYETOLEGYKILO, C_EDESANYJASZULETESICSALADINEV, C_EDESANYJASZULETESIUTONEVE, C_ELETVITELSZERUOTTLAKASKORZET, C_ELOTAG, C_ETIKAHITERKOLCSTAN, C_FELVETELSTATUSZA, C_KEPVISELOCIMEAZONOS, C_MEGJEGYZES, C_NEME, C_OKTATASIAZONOSITOSZAMA, C_OVODAAZONOSITO, C_OVODANEVE, C_SZAKERTOIBIZOTTSAGKORMANYHIV, C_SZULETESICSALADINEVE, C_SZULETESIHELY, C_SZULETESIIDO, C_SZULETESIORSZAG, C_SZULETESIUTONEVE, C_SZULOJENEKMUNKAHELYEAZISKOLA, C_SZULOJETESTVERETARTOSANBETEG, C_TAGOZAT, C_TANULOCSALADINEVE, C_TANULOUTONEVE, C_TESTVEREAZINTEZMENYTANULOJA, C_TKEMAILCIM, C_TKNEVE, C_TKROKONSAGIFOKA, C_TKTELEFON, C_AJTO, C_EMELET, C_HAZSZAM, C_HELYSEGNEV, C_IRANYITOSZAM, C_KOZTERULETJELLEGE, C_KOZTERULETNEV, C_TKAJTO, C_TKEMELET, C_TKHAZSZAM, C_TKHELYSEGNEV, C_TKIRANYITOSZAM, C_TKKOZTERULETJELLEGE, C_TKKOZTERULETNEV, C_BTM, C_BTMLETSZAMSULY, C_SNI, C_SNILETSZAMSULY, C_THAJTO, C_THHAZSZAM, C_THHELYSEGNEV, C_THIRANYITOSZAM, C_THKOZTERULETJELLEGE, C_THKOZTERULETNEV, C_THEMELET, C_TARTOZKODASIHELYAZONOS, C_FELADATELLATASIHELYID, TOROLT, 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_NEBULO', '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_NEBULO', '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_NEBULO', 
        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_teremLog]...';


GO

  CREATE TRIGGER tr_teremLog ON T_TEREM 
  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_AKTIV AS nvarchar(MAX)) AS C_AKTIV, CAST( C_JELLEG AS nvarchar(MAX)) AS C_JELLEG, CAST( C_KAPACITAS AS nvarchar(MAX)) AS C_KAPACITAS, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_TERULET AS nvarchar(MAX)) AS C_TERULET, CAST( C_TOBBORATLEHETTARTANI AS nvarchar(MAX)) AS C_TOBBORATLEHETTARTANI, CAST( C_VIZSGAKAPACITAS AS nvarchar(MAX)) AS C_VIZSGAKAPACITAS, CAST( C_WIFILEFEDETT AS nvarchar(MAX)) AS C_WIFILEFEDETT, CAST( C_MUKODESIHELYID AS nvarchar(MAX)) AS C_MUKODESIHELYID, CAST( C_TEREMFELELOSID AS nvarchar(MAX)) AS C_TEREMFELELOSID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_AKTIV, C_JELLEG, C_KAPACITAS, C_NEV, C_TERULET, C_TOBBORATLEHETTARTANI, C_VIZSGAKAPACITAS, C_WIFILEFEDETT, C_MUKODESIHELYID, C_TEREMFELELOSID, TOROLT, 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_AKTIV AS nvarchar(MAX)) AS C_AKTIV, CAST( C_JELLEG AS nvarchar(MAX)) AS C_JELLEG, CAST( C_KAPACITAS AS nvarchar(MAX)) AS C_KAPACITAS, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_TERULET AS nvarchar(MAX)) AS C_TERULET, CAST( C_TOBBORATLEHETTARTANI AS nvarchar(MAX)) AS C_TOBBORATLEHETTARTANI, CAST( C_VIZSGAKAPACITAS AS nvarchar(MAX)) AS C_VIZSGAKAPACITAS, CAST( C_WIFILEFEDETT AS nvarchar(MAX)) AS C_WIFILEFEDETT, CAST( C_MUKODESIHELYID AS nvarchar(MAX)) AS C_MUKODESIHELYID, CAST( C_TEREMFELELOSID AS nvarchar(MAX)) AS C_TEREMFELELOSID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_AKTIV, C_JELLEG, C_KAPACITAS, C_NEV, C_TERULET, C_TOBBORATLEHETTARTANI, C_VIZSGAKAPACITAS, C_WIFILEFEDETT, C_MUKODESIHELYID, C_TEREMFELELOSID, TOROLT, 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_TEREM', '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_TEREM', '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_TEREM', 
        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_ADATSZOLGALTATASENAPLO_OSSZES]...';


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


GO
PRINT N'Refreshing [dbo].[T_ESZKOZ_OSSZES]...';


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


GO
PRINT N'Refreshing [dbo].[T_NEBULO_OSSZES]...';


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


GO
PRINT N'Refreshing [dbo].[T_TEREM_OSSZES]...';


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


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


GO
ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH CHECK CHECK CONSTRAINT [FK_4026694_4026695];

ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH CHECK CHECK CONSTRAINT [FK_4026694_4026696];

ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH CHECK CHECK CONSTRAINT [FK_4026694_4026697];

ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH CHECK CHECK CONSTRAINT [FK_4026694_4026706];

ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH CHECK CHECK CONSTRAINT [FK_402669402_402669400];

ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH CHECK CHECK CONSTRAINT [FK_402669405_402669403];

ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH CHECK CHECK CONSTRAINT [FK_4026694_4027417];

ALTER TABLE [dbo].[T_ADATSZOLGALTATASENAPLO] WITH CHECK CHECK CONSTRAINT [FK_4026694_4027419];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_402509802_402509800];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_4025098_4025109];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_4025098_4025119];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_4025098_4025247];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_4025098_4025248];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_4025098_4025249];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_4025130_4025128];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_4025281_4025280];

ALTER TABLE [dbo].[T_ESZKOZ] WITH CHECK CHECK CONSTRAINT [FK_402509805_402509803];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027414];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_402736305_402736303];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_402736302_402736300];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027364];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027365];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027366];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027373];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027376];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027384];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027388];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027394];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027407];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027460_4027459];

ALTER TABLE [dbo].[T_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027363_4027483];

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

ALTER TABLE [dbo].[T_TEREM] WITH CHECK CHECK CONSTRAINT [FK_402512202_402512200];

ALTER TABLE [dbo].[T_TEREM] WITH CHECK CHECK CONSTRAINT [FK_4025122_4025124];

ALTER TABLE [dbo].[T_TEREM] WITH CHECK CHECK CONSTRAINT [FK_4025412_4025410];

ALTER TABLE [dbo].[T_TEREM] WITH CHECK CHECK CONSTRAINT [FK_4026131_4026130];

ALTER TABLE [dbo].[T_TEREM] WITH CHECK CHECK CONSTRAINT [FK_402512205_402512203];

ALTER TABLE [dbo].[T_ORARENDIORA] WITH CHECK CHECK CONSTRAINT [FK_4025550_4025549];

ALTER TABLE [dbo].[T_OSZTALYCSOPORT] WITH CHECK CHECK CONSTRAINT [FK_4025954_4025953];

ALTER TABLE [dbo].[T_TEREM_TANARITANTARGY] WITH CHECK CHECK CONSTRAINT [FK_4025951_4025950];

ALTER TABLE [dbo].[T_TEREM_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_4025948_4025947];

ALTER TABLE [dbo].[T_TERMEK_JOGYAKORLATOK] WITH CHECK CHECK CONSTRAINT [FK_4025913_4025912];


GO

PRINT '--- intézményi view-k (dbo táblákkal megegyező nevű és a tábla_OSSZES nevűek) frissítése ---'

DECLARE  @viewname nvarchar(100), @pureview nvarchar(100)

DECLARE semaviewk CURSOR LOCAL FOR 
  SELECT '['+TABLE_SCHEMA + '].[' + TABLE_NAME + ']', TABLE_NAME
  FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'VIEW' and TABLE_SCHEMA LIKE 'KR[_]%[_]Schema'
  AND TABLE_NAME in (
  'T_ADATSZOLGALTATASENAPLO', 'T_ADATSZOLGALTATASENAPLO_OSSZES',
  'T_ESZKOZ', 'T_ESZKOZ_OSSZES',
  'T_NEBULO', 'T_NEBULO_OSSZES',
  'T_TEREM', 'T_TEREM_OSSZES'
  ) -- ha csak néhány VIEW frissült, akkor a gyorsabb futásért ide fel lehet sorolni
  ORDER BY TABLE_SCHEMA, TABLE_NAME

OPEN semaviewk
FETCH NEXT FROM semaviewk INTO @viewname, @pureview

WHILE @@FETCH_STATUS = 0 BEGIN
	PRINT @viewname
	EXEC sp_refreshview @viewname

    FETCH NEXT FROM semaviewk INTO @viewname, @pureview
END             

CLOSE semaviewk
DEALLOCATE semaviewk
GO
