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


GO
ALTER TABLE [dbo].[T_KIRTELEPHELY] DROP CONSTRAINT [FK_4026536_4026534];


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


GO
ALTER TABLE [dbo].[T_INTEZMENYADATOK] DROP CONSTRAINT [FK_4026244_4026243];


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


GO
ALTER TABLE [dbo].[T_INTEZMENYADATOK] DROP CONSTRAINT [FK_4026248_4026247];


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


GO
ALTER TABLE [dbo].[T_INTEZMENYADATOK] DROP CONSTRAINT [FK_4027522_4027521];


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


GO
ALTER TABLE [dbo].[T_INTEZMENYADATOK] DROP CONSTRAINT [FK_4025209_4026340];


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


GO
ALTER TABLE [dbo].[T_INTEZMENYADATOK] DROP CONSTRAINT [FK_4025209_4027515];


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


GO
ALTER TABLE [dbo].[T_MUKODESIHELY] DROP CONSTRAINT [FK_4026252_4026250];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_INTEZMENYADATOK] (
    [ID]                         INT             IDENTITY (1, 1) NOT NULL,
    [C_CIME]                     NVARCHAR (2000) NULL,
    [C_IGAZGATONEVE]             NVARCHAR (255)  NULL,
    [C_NEV]                      NVARCHAR (255)  NOT NULL,
    [C_OMKOD]                    NVARCHAR (20)   NOT NULL,
    [C_TELEFONSZAM]              NVARCHAR (20)   NULL,
    [C_EMAILCIM]                 NVARCHAR (255)  NULL,
    [C_IRANYITOSZAM]             NVARCHAR (20)   NOT NULL,
    [C_VAROS]                    NVARCHAR (255)  NOT NULL,
    [C_ROVIDNEV]                 NVARCHAR (255)  NULL,
    [C_ADMINEMAILCIM]            NVARCHAR (255)  NULL,
    [C_KRETATIPUS]               INT             NOT NULL,
    [C_ENGEDELYEZETTALLASHELYEK] NUMERIC (10, 2) NULL,
    [C_INTEZMENYHONLAPJA]        NVARCHAR (300)  NULL,
    [C_AJTO]                     NVARCHAR (30)   NULL,
    [C_EMELET]                   NVARCHAR (40)   NULL,
    [C_HAZSZAM]                  NVARCHAR (30)   NULL,
    [C_KOZTERULETJELLEG]         INT             NULL,
    [C_KOZTERULETNEV]            NVARCHAR (40)   NULL,
    [C_DOKUMENTUMFEJLEC]         NVARCHAR (MAX)  NULL,
    [C_DOKUMENTUMLABLEC]         NVARCHAR (MAX)  NULL,
    [C_DOKUMENTUMFEJLECHTML]     NVARCHAR (MAX)  NULL,
    [C_DOKUMENTUMLABLECHTML]     NVARCHAR (MAX)  NULL,
    [C_TANEVID]                  INT             NOT NULL,
    [C_INTEZMENYID]              INT             NOT NULL,
    [C_CIMERID]                  INT             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_40252091] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_INTEZMENYADATOK])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_INTEZMENYADATOK] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_INTEZMENYADATOK] ([ID], [C_CIME], [C_IGAZGATONEVE], [C_NEV], [C_OMKOD], [C_TELEFONSZAM], [C_EMAILCIM], [C_IRANYITOSZAM], [C_VAROS], [C_ROVIDNEV], [C_ADMINEMAILCIM], [C_KRETATIPUS], [C_ENGEDELYEZETTALLASHELYEK], [C_INTEZMENYHONLAPJA], [C_AJTO], [C_EMELET], [C_HAZSZAM], [C_KOZTERULETJELLEG], [C_KOZTERULETNEV], [C_DOKUMENTUMFEJLEC], [C_DOKUMENTUMLABLEC], [C_TANEVID], [C_INTEZMENYID], [C_CIMERID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID])
        SELECT   [ID],
                 [C_CIME],
                 [C_IGAZGATONEVE],
                 [C_NEV],
                 [C_OMKOD],
                 [C_TELEFONSZAM],
                 [C_EMAILCIM],
                 [C_IRANYITOSZAM],
                 [C_VAROS],
                 [C_ROVIDNEV],
                 [C_ADMINEMAILCIM],
                 [C_KRETATIPUS],
                 [C_ENGEDELYEZETTALLASHELYEK],
                 [C_INTEZMENYHONLAPJA],
                 [C_AJTO],
                 [C_EMELET],
                 [C_HAZSZAM],
                 [C_KOZTERULETJELLEG],
                 [C_KOZTERULETNEV],
                 [C_DOKUMENTUMFEJLEC],
                 [C_DOKUMENTUMLABLEC],
                 [C_TANEVID],
                 [C_INTEZMENYID],
                 [C_CIMERID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID]
        FROM     [dbo].[T_INTEZMENYADATOK]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_INTEZMENYADATOK] OFF;
    END

DROP TABLE [dbo].[T_INTEZMENYADATOK];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_INTEZMENYADATOK]', N'T_INTEZMENYADATOK';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40252091]', N'PK_T_4025209', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_INTEZMENYADATOK].[I_T_4025209_4026340]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025209_4026340]
    ON [dbo].[T_INTEZMENYADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_KRETATIPUS] ASC);


GO
PRINT N'Creating [dbo].[T_INTEZMENYADATOK].[I_T_4025209_4027515]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025209_4027515]
    ON [dbo].[T_INTEZMENYADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_KOZTERULETJELLEG] ASC);


GO
PRINT N'Creating [dbo].[T_INTEZMENYADATOK].[I_T_4026244_4026243]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026244_4026243]
    ON [dbo].[T_INTEZMENYADATOK]([C_TANEVID] ASC);


GO
PRINT N'Creating [dbo].[T_INTEZMENYADATOK].[I_T_4026248_4026247]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026248_4026247]
    ON [dbo].[T_INTEZMENYADATOK]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_INTEZMENYADATOK].[I_T_4027522_4027521]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027522_4027521]
    ON [dbo].[T_INTEZMENYADATOK]([C_CIMERID] ASC);


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


GO
ALTER TABLE [dbo].[T_KIRTELEPHELY] WITH NOCHECK
    ADD CONSTRAINT [FK_4026536_4026534] FOREIGN KEY ([C_INTEZMENYADATOKID]) REFERENCES [dbo].[T_INTEZMENYADATOK] ([ID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_INTEZMENYADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4027522_4027521] FOREIGN KEY ([C_CIMERID]) REFERENCES [dbo].[T_UPLOADEDFILE] ([ID]);


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


GO
ALTER TABLE [dbo].[T_INTEZMENYADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4025209_4026340] FOREIGN KEY ([C_KRETATIPUS], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_KRETATIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


GO
ALTER TABLE [dbo].[T_MUKODESIHELY] WITH NOCHECK
    ADD CONSTRAINT [FK_4026252_4026250] FOREIGN KEY ([C_INTEZMENYADATOKID]) REFERENCES [dbo].[T_INTEZMENYADATOK] ([ID]);


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


GO

  CREATE TRIGGER tr_intezmenyadatokLog ON T_INTEZMENYADATOK 
  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_CIME AS nvarchar(MAX)) AS C_CIME, CAST( C_IGAZGATONEVE AS nvarchar(MAX)) AS C_IGAZGATONEVE, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_OMKOD AS nvarchar(MAX)) AS C_OMKOD, CAST( C_TELEFONSZAM AS nvarchar(MAX)) AS C_TELEFONSZAM, CAST( C_EMAILCIM AS nvarchar(MAX)) AS C_EMAILCIM, CAST( C_IRANYITOSZAM AS nvarchar(MAX)) AS C_IRANYITOSZAM, CAST( C_VAROS AS nvarchar(MAX)) AS C_VAROS, CAST( C_ROVIDNEV AS nvarchar(MAX)) AS C_ROVIDNEV, CAST( C_ADMINEMAILCIM AS nvarchar(MAX)) AS C_ADMINEMAILCIM, CAST( C_KRETATIPUS AS nvarchar(MAX)) AS C_KRETATIPUS, CAST( C_ENGEDELYEZETTALLASHELYEK AS nvarchar(MAX)) AS C_ENGEDELYEZETTALLASHELYEK, CAST( C_INTEZMENYHONLAPJA AS nvarchar(MAX)) AS C_INTEZMENYHONLAPJA, 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_KOZTERULETJELLEG AS nvarchar(MAX)) AS C_KOZTERULETJELLEG, CAST( C_KOZTERULETNEV AS nvarchar(MAX)) AS C_KOZTERULETNEV, CAST( C_DOKUMENTUMFEJLEC AS nvarchar(MAX)) AS C_DOKUMENTUMFEJLEC, CAST( C_DOKUMENTUMLABLEC AS nvarchar(MAX)) AS C_DOKUMENTUMLABLEC, CAST( C_DOKUMENTUMFEJLECHTML AS nvarchar(MAX)) AS C_DOKUMENTUMFEJLECHTML, CAST( C_DOKUMENTUMLABLECHTML AS nvarchar(MAX)) AS C_DOKUMENTUMLABLECHTML, CAST( C_CIMERID AS nvarchar(MAX)) AS C_CIMERID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_CIME, C_IGAZGATONEVE, C_NEV, C_OMKOD, C_TELEFONSZAM, C_EMAILCIM, C_IRANYITOSZAM, C_VAROS, C_ROVIDNEV, C_ADMINEMAILCIM, C_KRETATIPUS, C_ENGEDELYEZETTALLASHELYEK, C_INTEZMENYHONLAPJA, C_AJTO, C_EMELET, C_HAZSZAM, C_KOZTERULETJELLEG, C_KOZTERULETNEV, C_DOKUMENTUMFEJLEC, C_DOKUMENTUMLABLEC, C_DOKUMENTUMFEJLECHTML, C_DOKUMENTUMLABLECHTML, C_CIMERID, 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_CIME AS nvarchar(MAX)) AS C_CIME, CAST( C_IGAZGATONEVE AS nvarchar(MAX)) AS C_IGAZGATONEVE, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_OMKOD AS nvarchar(MAX)) AS C_OMKOD, CAST( C_TELEFONSZAM AS nvarchar(MAX)) AS C_TELEFONSZAM, CAST( C_EMAILCIM AS nvarchar(MAX)) AS C_EMAILCIM, CAST( C_IRANYITOSZAM AS nvarchar(MAX)) AS C_IRANYITOSZAM, CAST( C_VAROS AS nvarchar(MAX)) AS C_VAROS, CAST( C_ROVIDNEV AS nvarchar(MAX)) AS C_ROVIDNEV, CAST( C_ADMINEMAILCIM AS nvarchar(MAX)) AS C_ADMINEMAILCIM, CAST( C_KRETATIPUS AS nvarchar(MAX)) AS C_KRETATIPUS, CAST( C_ENGEDELYEZETTALLASHELYEK AS nvarchar(MAX)) AS C_ENGEDELYEZETTALLASHELYEK, CAST( C_INTEZMENYHONLAPJA AS nvarchar(MAX)) AS C_INTEZMENYHONLAPJA, 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_KOZTERULETJELLEG AS nvarchar(MAX)) AS C_KOZTERULETJELLEG, CAST( C_KOZTERULETNEV AS nvarchar(MAX)) AS C_KOZTERULETNEV, CAST( C_DOKUMENTUMFEJLEC AS nvarchar(MAX)) AS C_DOKUMENTUMFEJLEC, CAST( C_DOKUMENTUMLABLEC AS nvarchar(MAX)) AS C_DOKUMENTUMLABLEC, CAST( C_DOKUMENTUMFEJLECHTML AS nvarchar(MAX)) AS C_DOKUMENTUMFEJLECHTML, CAST( C_DOKUMENTUMLABLECHTML AS nvarchar(MAX)) AS C_DOKUMENTUMLABLECHTML, CAST( C_CIMERID AS nvarchar(MAX)) AS C_CIMERID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_CIME, C_IGAZGATONEVE, C_NEV, C_OMKOD, C_TELEFONSZAM, C_EMAILCIM, C_IRANYITOSZAM, C_VAROS, C_ROVIDNEV, C_ADMINEMAILCIM, C_KRETATIPUS, C_ENGEDELYEZETTALLASHELYEK, C_INTEZMENYHONLAPJA, C_AJTO, C_EMELET, C_HAZSZAM, C_KOZTERULETJELLEG, C_KOZTERULETNEV, C_DOKUMENTUMFEJLEC, C_DOKUMENTUMLABLEC, C_DOKUMENTUMFEJLECHTML, C_DOKUMENTUMLABLECHTML, C_CIMERID, 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_INTEZMENYADATOK', '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_INTEZMENYADATOK', '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_INTEZMENYADATOK', 
        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_INTEZMENYADATOK_OSSZES]...';


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


GO
PRINT N'Refreshing [stage].[sp_MrgIntezmenyHasznalat]...';


GO
EXECUTE sp_refreshsqlmodule N'[stage].[sp_MrgIntezmenyHasznalat]';


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


GO
PRINT N'Refreshing [stage].[sp_MrgStage]...';


GO
EXECUTE sp_refreshsqlmodule N'[stage].[sp_MrgStage]';


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


GO
ALTER TABLE [dbo].[T_KIRTELEPHELY] WITH CHECK CHECK CONSTRAINT [FK_4026536_4026534];

ALTER TABLE [dbo].[T_INTEZMENYADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026244_4026243];

ALTER TABLE [dbo].[T_INTEZMENYADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026248_4026247];

ALTER TABLE [dbo].[T_INTEZMENYADATOK] WITH CHECK CHECK CONSTRAINT [FK_4027522_4027521];

ALTER TABLE [dbo].[T_INTEZMENYADATOK] WITH CHECK CHECK CONSTRAINT [FK_4025209_4026340];

ALTER TABLE [dbo].[T_INTEZMENYADATOK] WITH CHECK CHECK CONSTRAINT [FK_4025209_4027515];

ALTER TABLE [dbo].[T_MUKODESIHELY] WITH CHECK CHECK CONSTRAINT [FK_4026252_4026250];


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_INTEZMENYADATOK', 'T_INTEZMENYADATOK_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
PRINT N'Update complete.';


GO
