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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025641_4025644];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025641_4026317];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025662_4025660];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025665_4025663];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025668_4025666];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025670_4025669];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_4025938_4025937];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_402564102_402564100];


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] DROP CONSTRAINT [FK_402564105_402564103];


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


GO
ALTER TABLE [dbo].[T_OSZTALYZATERTEKELES] DROP CONSTRAINT [FK_T_4025641_4025742];


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


GO
ALTER TABLE [dbo].[T_OSZTALYFONOKIERTEKELES] DROP CONSTRAINT [FK_T_4025641_4025743];


GO
PRINT N'Altering [dbo].[T_ERTEKELESMONDATBANK]...';


GO
ALTER TABLE [dbo].[T_ERTEKELESMONDATBANK] ALTER COLUMN [C_ROVIDNEV] NVARCHAR (3) NULL;


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_TANULOERTEKELES] (
    [ID]                  INT            IDENTITY (1, 1) NOT NULL,
    [C_ERTEKELESDATUM]    DATETIME       NOT NULL,
    [C_ERTEKELESTIPUSA]   INT            NOT NULL,
    [C_ERTEKELESSZOVEG]   NVARCHAR (MAX) NULL,
    [C_NEMMEGIRTDOLGOZAT] CHAR (1)       NULL,
    [C_ROGZITESDATUM]     DATETIME       NOT NULL,
    [C_OSZTALYZAT]        INT            NULL,
    [C_LATTAMOZAS]        CHAR (1)       DEFAULT ('F') NULL,
    [C_SZOVEGROVIDNEV]    NVARCHAR (3)   NULL,
    [C_TANTARGYID]        INT            NULL,
    [C_TANULOID]          INT            NOT NULL,
    [C_OSZTALYCSOPORTID]  INT            NULL,
    [C_ERTEKELOID]        INT            NOT NULL,
    [C_TANORAID]          INT            NULL,
    [C_INTEZMENYID]       INT            NOT NULL,
    [C_TANEVID]           INT            NOT NULL,
    [TOROLT]              CHAR (1)       DEFAULT ('F') NOT NULL,
    [SERIAL]              INT            DEFAULT ((0)) NOT NULL,
    [LASTCHANGED]         DATETIME       DEFAULT (getdate()) NOT NULL,
    [CREATED]             DATETIME       DEFAULT (getdate()) NOT NULL,
    [MODIFIER]            INT            NULL,
    [CREATOR]             INT            NULL,
    [ELOZOTANEVIREKORDID] INT            NULL,
    [NNID]                INT            NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40256411] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_TANULOERTEKELES])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANULOERTEKELES] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_TANULOERTEKELES] ([ID], [C_ERTEKELESDATUM], [C_ERTEKELESTIPUSA], [C_ERTEKELESSZOVEG], [C_NEMMEGIRTDOLGOZAT], [C_ROGZITESDATUM], [C_OSZTALYZAT], [C_LATTAMOZAS], [C_TANTARGYID], [C_TANULOID], [C_OSZTALYCSOPORTID], [C_ERTEKELOID], [C_TANORAID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_ERTEKELESDATUM],
                 [C_ERTEKELESTIPUSA],
                 [C_ERTEKELESSZOVEG],
                 [C_NEMMEGIRTDOLGOZAT],
                 [C_ROGZITESDATUM],
                 [C_OSZTALYZAT],
                 [C_LATTAMOZAS],
                 [C_TANTARGYID],
                 [C_TANULOID],
                 [C_OSZTALYCSOPORTID],
                 [C_ERTEKELOID],
                 [C_TANORAID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_TANULOERTEKELES]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TANULOERTEKELES] OFF;
    END

DROP TABLE [dbo].[T_TANULOERTEKELES];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_TANULOERTEKELES]', N'T_TANULOERTEKELES';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40256411]', N'PK_T_4025641', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4025644]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4025644]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ERTEKELESTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025641_4026317]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025641_4026317]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_OSZTALYZAT] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025662_4025660]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025662_4025660]
    ON [dbo].[T_TANULOERTEKELES]([C_TANTARGYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025665_4025663]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025665_4025663]
    ON [dbo].[T_TANULOERTEKELES]([C_TANULOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025668_4025666]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025668_4025666]
    ON [dbo].[T_TANULOERTEKELES]([C_OSZTALYCSOPORTID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025670_4025669]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025670_4025669]
    ON [dbo].[T_TANULOERTEKELES]([C_ERTEKELOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_4025938_4025937]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025938_4025937]
    ON [dbo].[T_TANULOERTEKELES]([C_TANORAID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_402564102_402564100]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402564102_402564100]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[I_T_402564105_402564103]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402564105_402564103]
    ON [dbo].[T_TANULOERTEKELES]([C_TANEVID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULOERTEKELES].[IDX_TANULOERTEKELES_TANULOID_TANTARGYID_ERTEKELESTIPUSA]...';


GO
CREATE NONCLUSTERED INDEX [IDX_TANULOERTEKELES_TANULOID_TANTARGYID_ERTEKELESTIPUSA]
    ON [dbo].[T_TANULOERTEKELES]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TANULOID] ASC, [C_TANTARGYID] ASC, [C_ERTEKELESTIPUSA] ASC);


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4025644] FOREIGN KEY ([C_ERTEKELESTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ERTEKELESTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025641_4026317] FOREIGN KEY ([C_OSZTALYZAT], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_OSZTALYZATTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025668_4025666] FOREIGN KEY ([C_OSZTALYCSOPORTID]) REFERENCES [dbo].[T_OSZTALYCSOPORT] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025670_4025669] FOREIGN KEY ([C_ERTEKELOID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025938_4025937] FOREIGN KEY ([C_TANORAID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_OSZTALYZATERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_T_4025641_4025742] FOREIGN KEY ([ID]) REFERENCES [dbo].[T_TANULOERTEKELES] ([ID]);


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


GO
ALTER TABLE [dbo].[T_OSZTALYFONOKIERTEKELES] WITH NOCHECK
    ADD CONSTRAINT [FK_T_4025641_4025743] FOREIGN KEY ([ID]) REFERENCES [dbo].[T_TANULOERTEKELES] ([ID]);


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


GO

  CREATE TRIGGER tr_tanuloertekelesLog ON T_TANULOERTEKELES 
  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_ERTEKELESDATUM, 121) AS C_ERTEKELESDATUM, CAST( C_ERTEKELESTIPUSA AS nvarchar(MAX)) AS C_ERTEKELESTIPUSA, CAST( C_ERTEKELESSZOVEG AS nvarchar(MAX)) AS C_ERTEKELESSZOVEG, CAST( C_NEMMEGIRTDOLGOZAT AS nvarchar(MAX)) AS C_NEMMEGIRTDOLGOZAT, CONVERT(nvarchar(MAX), C_ROGZITESDATUM, 121) AS C_ROGZITESDATUM, CAST( C_OSZTALYZAT AS nvarchar(MAX)) AS C_OSZTALYZAT, CAST( C_LATTAMOZAS AS nvarchar(MAX)) AS C_LATTAMOZAS, CAST( C_SZOVEGROVIDNEV AS nvarchar(MAX)) AS C_SZOVEGROVIDNEV, CAST( C_TANTARGYID AS nvarchar(MAX)) AS C_TANTARGYID, CAST( C_TANULOID AS nvarchar(MAX)) AS C_TANULOID, CAST( C_OSZTALYCSOPORTID AS nvarchar(MAX)) AS C_OSZTALYCSOPORTID, CAST( C_ERTEKELOID AS nvarchar(MAX)) AS C_ERTEKELOID, CAST( C_TANORAID AS nvarchar(MAX)) AS C_TANORAID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( ELOZOTANEVIREKORDID AS nvarchar(MAX)) AS ELOZOTANEVIREKORDID, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (C_ERTEKELESDATUM, C_ERTEKELESTIPUSA, C_ERTEKELESSZOVEG, C_NEMMEGIRTDOLGOZAT, C_ROGZITESDATUM, C_OSZTALYZAT, C_LATTAMOZAS, C_SZOVEGROVIDNEV, C_TANTARGYID, C_TANULOID, C_OSZTALYCSOPORTID, C_ERTEKELOID, C_TANORAID, TOROLT, ELOZOTANEVIREKORDID, 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_ERTEKELESDATUM, 121) AS C_ERTEKELESDATUM, CAST( C_ERTEKELESTIPUSA AS nvarchar(MAX)) AS C_ERTEKELESTIPUSA, CAST( C_ERTEKELESSZOVEG AS nvarchar(MAX)) AS C_ERTEKELESSZOVEG, CAST( C_NEMMEGIRTDOLGOZAT AS nvarchar(MAX)) AS C_NEMMEGIRTDOLGOZAT, CONVERT(nvarchar(MAX), C_ROGZITESDATUM, 121) AS C_ROGZITESDATUM, CAST( C_OSZTALYZAT AS nvarchar(MAX)) AS C_OSZTALYZAT, CAST( C_LATTAMOZAS AS nvarchar(MAX)) AS C_LATTAMOZAS, CAST( C_SZOVEGROVIDNEV AS nvarchar(MAX)) AS C_SZOVEGROVIDNEV, CAST( C_TANTARGYID AS nvarchar(MAX)) AS C_TANTARGYID, CAST( C_TANULOID AS nvarchar(MAX)) AS C_TANULOID, CAST( C_OSZTALYCSOPORTID AS nvarchar(MAX)) AS C_OSZTALYCSOPORTID, CAST( C_ERTEKELOID AS nvarchar(MAX)) AS C_ERTEKELOID, CAST( C_TANORAID AS nvarchar(MAX)) AS C_TANORAID, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( ELOZOTANEVIREKORDID AS nvarchar(MAX)) AS ELOZOTANEVIREKORDID, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (C_ERTEKELESDATUM, C_ERTEKELESTIPUSA, C_ERTEKELESSZOVEG, C_NEMMEGIRTDOLGOZAT, C_ROGZITESDATUM, C_OSZTALYZAT, C_LATTAMOZAS, C_SZOVEGROVIDNEV, C_TANTARGYID, C_TANULOID, C_OSZTALYCSOPORTID, C_ERTEKELOID, C_TANORAID, TOROLT, ELOZOTANEVIREKORDID, 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_TANULOERTEKELES', '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_TANULOERTEKELES', '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_TANULOERTEKELES', 
        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_ERTEKELESMONDATBANK_OSSZES]...';


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


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


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


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4025644];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025641_4026317];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025662_4025660];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025665_4025663];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025668_4025666];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025670_4025669];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_4025938_4025937];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_402564102_402564100];

ALTER TABLE [dbo].[T_TANULOERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_402564105_402564103];

ALTER TABLE [dbo].[T_OSZTALYZATERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_T_4025641_4025742];

ALTER TABLE [dbo].[T_OSZTALYFONOKIERTEKELES] WITH CHECK CHECK CONSTRAINT [FK_T_4025641_4025743];


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_TANULOERTEKELES', 'T_TANULOERTEKELES_OSSZES',
  'T_ERTEKELESMONDATBANK','T_ERTEKELESMONDATBANK_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


-- Triggerek inicializálás az összes táblára
DECLARE tableCursor CURSOR LOCAL FOR 
  SELECT DISTINCT TABLE_NAME
  FROM INFORMATION_SCHEMA.COLUMNS 
  WHERE COLUMN_NAME IN ('CREATOR', 'MODIFIER')
    AND TABLE_NAME NOT IN ('T_ENTITYHISTORY', 'T_ENTITYATTRIBUTEHISTORY', 'T_LOG', 'T_GLOBALLOCK', 'T_USERPROFILE', 'T_OLDALLATOGATOTTSAG')
    AND TABLE_NAME IN (
        SELECT t.name 
        FROM sys.tables t
        INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
        WHERE s.name ='dbo' 
    )

DECLARE @table nvarchar(35)

OPEN tableCursor  
FETCH NEXT FROM tableCursor INTO @table  

WHILE @@FETCH_STATUS = 0 BEGIN  
  EXEC sp_Global_CreateTriggers @table
  FETCH NEXT FROM tableCursor INTO @table  
END  

CLOSE tableCursor  
DEALLOCATE tableCursor 
