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

SET NUMERIC_ROUNDABORT OFF;


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_KEPVISELO2CIMEAZONOS]         CHAR (1)        DEFAULT ('T') NOT NULL,
    [C_TKAJTO2]                      NVARCHAR (30)   NULL,
    [C_TKEMAILCIM2]                  NVARCHAR (255)  NULL,
    [C_TKEMELET2]                    NVARCHAR (40)   NULL,
    [C_TKHAZSZAM2]                   NVARCHAR (30)   NULL,
    [C_TKHELYSEGNEV2]                NVARCHAR (255)  NULL,
    [C_TKIRANYITOSZAM2]              NVARCHAR (20)   NULL,
    [C_TKKOZTERULETJELLEGE2]         INT             NULL,
    [C_TKKOZTERULETNEV2]             NVARCHAR (40)   NULL,
    [C_TKNEVE2]                      NVARCHAR (200)  NULL,
    [C_TKROKONSAGIFOKA2]             INT             NULL,
    [C_TKTELEFON2]                   NVARCHAR (20)   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_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], [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_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],
                 [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_4027363_4027499]...';


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


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


GO
CREATE NONCLUSTERED INDEX [I_T_4027363_4027502]
    ON [dbo].[T_NEBULO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TKROKONSAGIFOKA2] 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'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_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_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_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_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_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_4027363_4027499]...';


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


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


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


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_KEPVISELO2CIMEAZONOS AS nvarchar(MAX)) AS C_KEPVISELO2CIMEAZONOS, CAST( C_TKAJTO2 AS nvarchar(MAX)) AS C_TKAJTO2, CAST( C_TKEMAILCIM2 AS nvarchar(MAX)) AS C_TKEMAILCIM2, CAST( C_TKEMELET2 AS nvarchar(MAX)) AS C_TKEMELET2, CAST( C_TKHAZSZAM2 AS nvarchar(MAX)) AS C_TKHAZSZAM2, CAST( C_TKHELYSEGNEV2 AS nvarchar(MAX)) AS C_TKHELYSEGNEV2, CAST( C_TKIRANYITOSZAM2 AS nvarchar(MAX)) AS C_TKIRANYITOSZAM2, CAST( C_TKKOZTERULETJELLEGE2 AS nvarchar(MAX)) AS C_TKKOZTERULETJELLEGE2, CAST( C_TKKOZTERULETNEV2 AS nvarchar(MAX)) AS C_TKKOZTERULETNEV2, CAST( C_TKNEVE2 AS nvarchar(MAX)) AS C_TKNEVE2, CAST( C_TKROKONSAGIFOKA2 AS nvarchar(MAX)) AS C_TKROKONSAGIFOKA2, CAST( C_TKTELEFON2 AS nvarchar(MAX)) AS C_TKTELEFON2, 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_KEPVISELO2CIMEAZONOS, C_TKAJTO2, C_TKEMAILCIM2, C_TKEMELET2, C_TKHAZSZAM2, C_TKHELYSEGNEV2, C_TKIRANYITOSZAM2, C_TKKOZTERULETJELLEGE2, C_TKKOZTERULETNEV2, C_TKNEVE2, C_TKROKONSAGIFOKA2, C_TKTELEFON2, 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_KEPVISELO2CIMEAZONOS AS nvarchar(MAX)) AS C_KEPVISELO2CIMEAZONOS, CAST( C_TKAJTO2 AS nvarchar(MAX)) AS C_TKAJTO2, CAST( C_TKEMAILCIM2 AS nvarchar(MAX)) AS C_TKEMAILCIM2, CAST( C_TKEMELET2 AS nvarchar(MAX)) AS C_TKEMELET2, CAST( C_TKHAZSZAM2 AS nvarchar(MAX)) AS C_TKHAZSZAM2, CAST( C_TKHELYSEGNEV2 AS nvarchar(MAX)) AS C_TKHELYSEGNEV2, CAST( C_TKIRANYITOSZAM2 AS nvarchar(MAX)) AS C_TKIRANYITOSZAM2, CAST( C_TKKOZTERULETJELLEGE2 AS nvarchar(MAX)) AS C_TKKOZTERULETJELLEGE2, CAST( C_TKKOZTERULETNEV2 AS nvarchar(MAX)) AS C_TKKOZTERULETNEV2, CAST( C_TKNEVE2 AS nvarchar(MAX)) AS C_TKNEVE2, CAST( C_TKROKONSAGIFOKA2 AS nvarchar(MAX)) AS C_TKROKONSAGIFOKA2, CAST( C_TKTELEFON2 AS nvarchar(MAX)) AS C_TKTELEFON2, 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_KEPVISELO2CIMEAZONOS, C_TKAJTO2, C_TKEMAILCIM2, C_TKEMELET2, C_TKHAZSZAM2, C_TKHELYSEGNEV2, C_TKIRANYITOSZAM2, C_TKKOZTERULETJELLEGE2, C_TKKOZTERULETNEV2, C_TKNEVE2, C_TKROKONSAGIFOKA2, C_TKTELEFON2, 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'Refreshing [dbo].[T_NEBULO_OSSZES]...';


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


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


GO
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_4027363_4027414];

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

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

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

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

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

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


GO
PRINT N'Update complete.';


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_NEBULO', 'T_NEBULO_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


