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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_TANULOESEMENY] (
    [ID]                    INT            IDENTITY (1, 1) NOT NULL,
    [C_KEZDETE]             DATETIME       NOT NULL,
    [C_MEGJEGYZES]          NVARCHAR (255) NULL,
    [C_TARTALOM]            NVARCHAR (MAX) NOT NULL,
    [C_TIPUS]               INT            NOT NULL,
    [C_CIM]                 NVARCHAR (255) NULL,
    [C_FALIUJSAGMEGJELENES] CHAR (1)       DEFAULT ('T') NOT NULL,
    [C_MINDENOSZTALY]       CHAR (1)       DEFAULT ('T') NOT NULL,
    [C_MINDENTANULO]        CHAR (1)       DEFAULT ('T') NOT NULL,
    [C_VEGE]                DATETIME       NULL,
    [C_TANULOID]            INT            NOT NULL,
    [C_FELJEGYZOID]         INT            NOT NULL,
    [C_TANTARGYID]          INT            NULL,
    [C_TANITASIORAID]       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_40256991] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_TANULOESEMENY])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANULOESEMENY] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_TANULOESEMENY] ([ID], [C_KEZDETE], [C_MEGJEGYZES], [C_TARTALOM], [C_TIPUS], [C_TANULOID], [C_FELJEGYZOID], [C_TANTARGYID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID])
        SELECT   [ID],
                 [C_KEZDETE],
                 [C_MEGJEGYZES],
                 [C_TARTALOM],
                 [C_TIPUS],
                 [C_TANULOID],
                 [C_FELJEGYZOID],
                 [C_TANTARGYID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID]
        FROM     [dbo].[T_TANULOESEMENY]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANULOESEMENY] OFF;
    END

DROP TABLE [dbo].[T_TANULOESEMENY];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_TANULOESEMENY]', N'T_TANULOESEMENY';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40256991]', N'PK_T_4025699', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_TANULOESEMENY].[I_T_4025699_4025708]...';


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


GO
PRINT N'Creating [dbo].[T_TANULOESEMENY].[I_T_4025711_4025710]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025711_4025710]
    ON [dbo].[T_TANULOESEMENY]([C_TANULOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOESEMENY].[I_T_4025714_4025713]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025714_4025713]
    ON [dbo].[T_TANULOESEMENY]([C_FELJEGYZOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOESEMENY].[I_T_4025942_4025941]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025942_4025941]
    ON [dbo].[T_TANULOESEMENY]([C_TANTARGYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOESEMENY].[I_T_4027536_4027535]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027536_4027535]
    ON [dbo].[T_TANULOESEMENY]([C_TANITASIORAID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOESEMENY].[I_T_402569902_402569900]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402569902_402569900]
    ON [dbo].[T_TANULOESEMENY]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOESEMENY].[I_T_402569905_402569903]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402569905_402569903]
    ON [dbo].[T_TANULOESEMENY]([C_TANEVID] ASC);


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


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


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] WITH NOCHECK
    ADD CONSTRAINT [FK_4025711_4025710] FOREIGN KEY ([C_TANULOID]) REFERENCES [dbo].[T_TANULO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] WITH NOCHECK
    ADD CONSTRAINT [FK_4025714_4025713] FOREIGN KEY ([C_FELJEGYZOID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


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


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


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


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


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


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


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


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


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


GO

  CREATE TRIGGER tr_tanuloesemenyLog ON T_TANULOESEMENY 
  FOR INSERT, DELETE, UPDATE AS
  BEGIN
    SET NOCOUNT ON
    DECLARE
      @insTable TABLE (objectId int, intezmenyId int, tanevId int, creator int, modifier int, col nvarchar(30), val varchar(max))
    DECLARE  
      @delTable TABLE (objectId int, intezmenyId int, tanevId int, creator int, modifier int, col nvarchar(30), val varchar(max))
    DECLARE 
      @linkTable TABLE (id int, objectId int, intezmenyId int, tanevId int)
    DECLARE  
      @action char(1),
      @xml xml    
    
    SET @action =
      CASE 
        WHEN NOT EXISTS (SELECT 1 FROM DELETED) THEN 'I'
        WHEN NOT EXISTS (SELECT 1 FROM INSERTED) THEN 'D'
        ELSE 'U'
      END  
    
      
    INSERT INTO @insTable (objectId, intezmenyId, tanevId, creator, modifier, col, val)
    SELECT ID, C_INTEZMENYID, C_TANEVID, CREATOR, MODIFIER, col, val
      FROM 
       (
       SELECT  ID, C_INTEZMENYID AS C_INTEZMENYID, C_TANEVID AS C_TANEVID, CREATOR, MODIFIER, CONVERT(nvarchar(MAX), C_KEZDETE, 121) AS C_KEZDETE, CAST( C_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_TARTALOM AS nvarchar(MAX)) AS C_TARTALOM, CAST( C_TIPUS AS nvarchar(MAX)) AS C_TIPUS, CAST( C_CIM AS nvarchar(MAX)) AS C_CIM, CAST( C_FALIUJSAGMEGJELENES AS nvarchar(MAX)) AS C_FALIUJSAGMEGJELENES, CAST( C_MINDENOSZTALY AS nvarchar(MAX)) AS C_MINDENOSZTALY, CAST( C_MINDENTANULO AS nvarchar(MAX)) AS C_MINDENTANULO, CONVERT(nvarchar(MAX), C_VEGE, 121) AS C_VEGE, CAST( C_TANULOID AS nvarchar(MAX)) AS C_TANULOID, CAST( C_FELJEGYZOID AS nvarchar(MAX)) AS C_FELJEGYZOID, CAST( C_TANTARGYID AS nvarchar(MAX)) AS C_TANTARGYID, CAST( C_TANITASIORAID AS nvarchar(MAX)) AS C_TANITASIORAID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_KEZDETE, C_MEGJEGYZES, C_TARTALOM, C_TIPUS, C_CIM, C_FALIUJSAGMEGJELENES, C_MINDENOSZTALY, C_MINDENTANULO, C_VEGE, C_TANULOID, C_FELJEGYZOID, C_TANTARGYID, C_TANITASIORAID, 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, CONVERT(nvarchar(MAX), C_KEZDETE, 121) AS C_KEZDETE, CAST( C_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_TARTALOM AS nvarchar(MAX)) AS C_TARTALOM, CAST( C_TIPUS AS nvarchar(MAX)) AS C_TIPUS, CAST( C_CIM AS nvarchar(MAX)) AS C_CIM, CAST( C_FALIUJSAGMEGJELENES AS nvarchar(MAX)) AS C_FALIUJSAGMEGJELENES, CAST( C_MINDENOSZTALY AS nvarchar(MAX)) AS C_MINDENOSZTALY, CAST( C_MINDENTANULO AS nvarchar(MAX)) AS C_MINDENTANULO, CONVERT(nvarchar(MAX), C_VEGE, 121) AS C_VEGE, CAST( C_TANULOID AS nvarchar(MAX)) AS C_TANULOID, CAST( C_FELJEGYZOID AS nvarchar(MAX)) AS C_FELJEGYZOID, CAST( C_TANTARGYID AS nvarchar(MAX)) AS C_TANTARGYID, CAST( C_TANITASIORAID AS nvarchar(MAX)) AS C_TANITASIORAID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_KEZDETE, C_MEGJEGYZES, C_TARTALOM, C_TIPUS, C_CIM, C_FALIUJSAGMEGJELENES, C_MINDENOSZTALY, C_MINDENTANULO, C_VEGE, C_TANULOID, C_FELJEGYZOID, C_TANTARGYID, C_TANITASIORAID, 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_TANULOESEMENY', '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_TANULOESEMENY', '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_TANULOESEMENY', 
        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_TANULOESEMENY_OSSZES]...';


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] WITH CHECK CHECK CONSTRAINT [FK_4025699_4025708];

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

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

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

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

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

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


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_TANULOESEMENY', 'T_TANULOESEMENY_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
