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


GO
ALTER TABLE [dbo].[T_NEBULO] DROP CONSTRAINT [FK_4027460_4027459];


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] DROP CONSTRAINT [FK_4026032_4026030];


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] DROP CONSTRAINT [FK_402601002_402601000];


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] DROP CONSTRAINT [FK_4026010_4026011];


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] DROP CONSTRAINT [FK_402601005_402601003];


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


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


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


GO
ALTER TABLE [dbo].[T_JOGYAKORLAT] DROP CONSTRAINT [FK_4026037_4026036];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026125_4026124];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_FELADATELLATASIHELY] (
    [ID]                             INT      IDENTITY (1, 1) NOT NULL,
    [C_OKTATASINEVELESIFELADATTIPUS] INT      NOT NULL,
    [C_ENGEDELYEZETTOSSZLETSZAM]     INT      NULL,
    [C_ENGEDELYEZETTOSZTALY]         INT      NULL,
    [C_MUKODESIHELYID]               INT      NOT 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_40260101] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_FELADATELLATASIHELY])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_FELADATELLATASIHELY] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_FELADATELLATASIHELY] ([ID], [C_OKTATASINEVELESIFELADATTIPUS], [C_MUKODESIHELYID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID])
        SELECT   [ID],
                 [C_OKTATASINEVELESIFELADATTIPUS],
                 [C_MUKODESIHELYID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID]
        FROM     [dbo].[T_FELADATELLATASIHELY]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_FELADATELLATASIHELY] OFF;
    END

DROP TABLE [dbo].[T_FELADATELLATASIHELY];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_FELADATELLATASIHELY]', N'T_FELADATELLATASIHELY';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40260101]', N'PK_T_4026010', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_FELADATELLATASIHELY].[I_T_4026010_4026011]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026010_4026011]
    ON [dbo].[T_FELADATELLATASIHELY]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_OKTATASINEVELESIFELADATTIPUS] ASC);


GO
PRINT N'Creating [dbo].[T_FELADATELLATASIHELY].[I_T_4026032_4026030]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026032_4026030]
    ON [dbo].[T_FELADATELLATASIHELY]([C_MUKODESIHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_FELADATELLATASIHELY].[I_T_402601002_402601000]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402601002_402601000]
    ON [dbo].[T_FELADATELLATASIHELY]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_FELADATELLATASIHELY].[I_T_402601005_402601003]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402601005_402601003]
    ON [dbo].[T_FELADATELLATASIHELY]([C_TANEVID] ASC);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH NOCHECK
    ADD CONSTRAINT [FK_4026010_4026011] FOREIGN KEY ([C_OKTATASINEVELESIFELADATTIPUS], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_OKTATASINEVELESIFELADAT] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


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


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


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


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


GO

  CREATE TRIGGER tr_feladatellatasihelyLog ON T_FELADATELLATASIHELY 
  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_OKTATASINEVELESIFELADATTIPUS AS nvarchar(MAX)) AS C_OKTATASINEVELESIFELADATTIPUS, CAST( C_ENGEDELYEZETTOSSZLETSZAM AS nvarchar(MAX)) AS C_ENGEDELYEZETTOSSZLETSZAM, CAST( C_ENGEDELYEZETTOSZTALY AS nvarchar(MAX)) AS C_ENGEDELYEZETTOSZTALY, CAST( C_MUKODESIHELYID AS nvarchar(MAX)) AS C_MUKODESIHELYID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_OKTATASINEVELESIFELADATTIPUS, C_ENGEDELYEZETTOSSZLETSZAM, C_ENGEDELYEZETTOSZTALY, C_MUKODESIHELYID, 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_OKTATASINEVELESIFELADATTIPUS AS nvarchar(MAX)) AS C_OKTATASINEVELESIFELADATTIPUS, CAST( C_ENGEDELYEZETTOSSZLETSZAM AS nvarchar(MAX)) AS C_ENGEDELYEZETTOSSZLETSZAM, CAST( C_ENGEDELYEZETTOSZTALY AS nvarchar(MAX)) AS C_ENGEDELYEZETTOSZTALY, CAST( C_MUKODESIHELYID AS nvarchar(MAX)) AS C_MUKODESIHELYID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_OKTATASINEVELESIFELADATTIPUS, C_ENGEDELYEZETTOSSZLETSZAM, C_ENGEDELYEZETTOSZTALY, C_MUKODESIHELYID, 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_FELADATELLATASIHELY', '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_FELADATELLATASIHELY', '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_FELADATELLATASIHELY', 
        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_FELADATELLATASIHELY_OSSZES]...';


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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_NEBULO] WITH CHECK CHECK CONSTRAINT [FK_4027460_4027459];

ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH CHECK CHECK CONSTRAINT [FK_4026032_4026030];

ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH CHECK CHECK CONSTRAINT [FK_402601002_402601000];

ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH CHECK CHECK CONSTRAINT [FK_4026010_4026011];

ALTER TABLE [dbo].[T_FELADATELLATASIHELY] WITH CHECK CHECK CONSTRAINT [FK_402601005_402601003];

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

ALTER TABLE [dbo].[T_JOGYAKORLAT] WITH CHECK CHECK CONSTRAINT [FK_4026037_4026036];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026125_4026124];


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_FELADATELLATASIHELY', 'T_FELADATELLATASIHELY_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
