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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025638_4025637];


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


GO
ALTER TABLE [dbo].[T_TANMENET] DROP CONSTRAINT [FK_4025599_4025598];


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


GO
ALTER TABLE [dbo].[T_ORARENDIORA] DROP CONSTRAINT [FK_4026324_4026322];


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


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


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_OSZTALY] DROP CONSTRAINT [FK_T_4025296_4025338];


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


GO
ALTER TABLE [dbo].[T_TANULOCSOPORT] DROP CONSTRAINT [FK_4025526_4025525];


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


GO
ALTER TABLE [dbo].[T_CSOPORT] DROP CONSTRAINT [FK_T_4025296_4025339];


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT_FALIUJSAGBEJE] DROP CONSTRAINT [FK_4025829_4025828];


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT_TANEVRENDJE] DROP CONSTRAINT [FK_4025514_4025513];


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] DROP CONSTRAINT [FK_4025431_4025429];


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


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


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_OSZTALYCSOPORT] (
    [ID]                      INT            IDENTITY (1, 1) NOT NULL,
    [C_MEGJEGYZES]            NVARCHAR (255) NULL,
    [C_NEV]                   NVARCHAR (255) NOT NULL,
    [C_VEGZOSEVFOLYAM]        CHAR (1)       DEFAULT ('F') NULL,
    [C_EVFOLYAMTIPUSA]        INT            NULL,
    [C_IMPORTALT]             CHAR (1)       DEFAULT ('F') NULL,
    [C_TERVEZETTLETSZAM]      INT            NULL,
    [C_KERESZTFELEVES]        CHAR (1)       DEFAULT ('F') NULL,
    [C_TEREMID]               INT            NULL,
    [C_FELADATELLATASIHELYID] 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_40252961] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_OSZTALYCSOPORT])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_OSZTALYCSOPORT] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_OSZTALYCSOPORT] ([ID], [C_MEGJEGYZES], [C_NEV], [C_VEGZOSEVFOLYAM], [C_EVFOLYAMTIPUSA], [C_IMPORTALT], [C_TERVEZETTLETSZAM], [C_TEREMID], [C_FELADATELLATASIHELYID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID])
        SELECT   [ID],
                 [C_MEGJEGYZES],
                 [C_NEV],
                 [C_VEGZOSEVFOLYAM],
                 [C_EVFOLYAMTIPUSA],
                 [C_IMPORTALT],
                 [C_TERVEZETTLETSZAM],
                 [C_TEREMID],
                 [C_FELADATELLATASIHELYID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID]
        FROM     [dbo].[T_OSZTALYCSOPORT]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_OSZTALYCSOPORT] OFF;
    END

DROP TABLE [dbo].[T_OSZTALYCSOPORT];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_OSZTALYCSOPORT]', N'T_OSZTALYCSOPORT';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40252961]', N'PK_T_4025296', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_OSZTALYCSOPORT].[I_T_4025296_4025418]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025296_4025418]
    ON [dbo].[T_OSZTALYCSOPORT]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_EVFOLYAMTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_OSZTALYCSOPORT].[I_T_4025954_4025953]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025954_4025953]
    ON [dbo].[T_OSZTALYCSOPORT]([C_TEREMID] ASC);


GO
PRINT N'Creating [dbo].[T_OSZTALYCSOPORT].[I_T_4026025_4026024]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026025_4026024]
    ON [dbo].[T_OSZTALYCSOPORT]([C_FELADATELLATASIHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_OSZTALYCSOPORT].[I_T_402529602_402529600]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402529602_402529600]
    ON [dbo].[T_OSZTALYCSOPORT]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_OSZTALYCSOPORT].[I_T_402529605_402529603]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402529605_402529603]
    ON [dbo].[T_OSZTALYCSOPORT]([C_TANEVID] ASC);


GO
PRINT N'Creating [dbo].[T_OSZTALYCSOPORT].[UK_TANEV_OSZTALYNEV]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [UK_TANEV_OSZTALYNEV]
    ON [dbo].[T_OSZTALYCSOPORT]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_NEV] ASC) WHERE ([TOROLT]='F');


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT] WITH NOCHECK
    ADD CONSTRAINT [FK_4025954_4025953] FOREIGN KEY ([C_TEREMID]) REFERENCES [dbo].[T_TEREM] ([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_402529602_402529600]...';


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


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


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


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT] WITH NOCHECK
    ADD CONSTRAINT [FK_4025296_4025418] FOREIGN KEY ([C_EVFOLYAMTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EVFOLYAMTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_OSZTALY] WITH NOCHECK
    ADD CONSTRAINT [FK_T_4025296_4025338] FOREIGN KEY ([ID]) REFERENCES [dbo].[T_OSZTALYCSOPORT] ([ID]);


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


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


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


GO
ALTER TABLE [dbo].[T_CSOPORT] WITH NOCHECK
    ADD CONSTRAINT [FK_T_4025296_4025339] FOREIGN KEY ([ID]) REFERENCES [dbo].[T_OSZTALYCSOPORT] ([ID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4025431_4025429] FOREIGN KEY ([C_OSZTALYCSOPORTID]) REFERENCES [dbo].[T_OSZTALYCSOPORT] ([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].[tr_osztalycsoportLog]...';


GO

  CREATE TRIGGER tr_osztalycsoportLog ON T_OSZTALYCSOPORT 
  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_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_VEGZOSEVFOLYAM AS nvarchar(MAX)) AS C_VEGZOSEVFOLYAM, CAST( C_EVFOLYAMTIPUSA AS nvarchar(MAX)) AS C_EVFOLYAMTIPUSA, CAST( C_IMPORTALT AS nvarchar(MAX)) AS C_IMPORTALT, CAST( C_TERVEZETTLETSZAM AS nvarchar(MAX)) AS C_TERVEZETTLETSZAM, CAST( C_KERESZTFELEVES AS nvarchar(MAX)) AS C_KERESZTFELEVES, CAST( C_TEREMID AS nvarchar(MAX)) AS C_TEREMID, 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_MEGJEGYZES, C_NEV, C_VEGZOSEVFOLYAM, C_EVFOLYAMTIPUSA, C_IMPORTALT, C_TERVEZETTLETSZAM, C_KERESZTFELEVES, C_TEREMID, 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_MEGJEGYZES AS nvarchar(MAX)) AS C_MEGJEGYZES, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_VEGZOSEVFOLYAM AS nvarchar(MAX)) AS C_VEGZOSEVFOLYAM, CAST( C_EVFOLYAMTIPUSA AS nvarchar(MAX)) AS C_EVFOLYAMTIPUSA, CAST( C_IMPORTALT AS nvarchar(MAX)) AS C_IMPORTALT, CAST( C_TERVEZETTLETSZAM AS nvarchar(MAX)) AS C_TERVEZETTLETSZAM, CAST( C_KERESZTFELEVES AS nvarchar(MAX)) AS C_KERESZTFELEVES, CAST( C_TEREMID AS nvarchar(MAX)) AS C_TEREMID, 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_MEGJEGYZES, C_NEV, C_VEGZOSEVFOLYAM, C_EVFOLYAMTIPUSA, C_IMPORTALT, C_TERVEZETTLETSZAM, C_KERESZTFELEVES, C_TEREMID, 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_OSZTALYCSOPORT', '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_OSZTALYCSOPORT', '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_OSZTALYCSOPORT', 
        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_OSZTALYCSOPORT_OSSZES]...';


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


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] WITH CHECK CHECK CONSTRAINT [FK_4025638_4025637];

ALTER TABLE [dbo].[T_TANMENET] WITH CHECK CHECK CONSTRAINT [FK_4025599_4025598];

ALTER TABLE [dbo].[T_ORARENDIORA] WITH CHECK CHECK CONSTRAINT [FK_4026324_4026322];

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

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

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

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

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

ALTER TABLE [dbo].[T_OSZTALY] WITH CHECK CHECK CONSTRAINT [FK_T_4025296_4025338];

ALTER TABLE [dbo].[T_TANULOCSOPORT] WITH CHECK CHECK CONSTRAINT [FK_4025526_4025525];

ALTER TABLE [dbo].[T_CSOPORT] WITH CHECK CHECK CONSTRAINT [FK_T_4025296_4025339];

ALTER TABLE [dbo].[T_OSZTALYCSOPORT_FALIUJSAGBEJE] WITH CHECK CHECK CONSTRAINT [FK_4025829_4025828];

ALTER TABLE [dbo].[T_OSZTALYCSOPORT_TANEVRENDJE] WITH CHECK CHECK CONSTRAINT [FK_4025514_4025513];

ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_4025431_4025429];

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


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_OSZTALYCSOPORT', 'T_OSZTALYCSOPORT_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
