﻿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].[T_TANULOESEMENY].[I_T_4025711_4025710]...';


GO
DROP INDEX [I_T_4025711_4025710]
    ON [dbo].[T_TANULOESEMENY];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP CONSTRAINT [FK_4025711_4025710];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP CONSTRAINT [FK_4025714_4025713];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP CONSTRAINT [FK_4025942_4025941];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP CONSTRAINT [FK_4027536_4027535];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP CONSTRAINT [FK_402569902_402569900];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP CONSTRAINT [FK_402569905_402569903];


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


GO
ALTER TABLE [dbo].[T_TANULOESEMENY] DROP CONSTRAINT [FK_4025699_4025708];


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_TARGY]               NVARCHAR (255) NOT NULL,
    [C_FALIUJSAGMEGJELENES] CHAR (1)       NULL,
    [C_MINDENKINEK]         CHAR (1)       NULL,
    [C_VEGE]                DATETIME       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_FALIUJSAGMEGJELENES], [C_VEGE], [C_FELJEGYZOID], [C_TANTARGYID], [C_TANITASIORAID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [NNID], [C_TARGY])
        SELECT   [ID],
                 [C_KEZDETE],
                 [C_MEGJEGYZES],
                 [C_TARTALOM],
                 [C_TIPUS],
                 [C_FALIUJSAGMEGJELENES],
                 [C_VEGE],
                 [C_FELJEGYZOID],
                 [C_TANTARGYID],
                 [C_TANITASIORAID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [NNID],
                 [C_TARGY]
        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_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].[T_OSZTALYCSOPORT_TANULOESEMENY]...';


GO
CREATE TABLE [dbo].[T_OSZTALYCSOPORT_TANULOESEMENY] (
    [C_OSZTALYCSOPORTID] INT NOT NULL,
    [C_TANULOESEMENYID]  INT NOT NULL,
    [NNID]               INT NULL,
    CONSTRAINT [PK_T_T_OSZTALYCSOPORT_TANULOES] PRIMARY KEY CLUSTERED ([C_OSZTALYCSOPORTID] ASC, [C_TANULOESEMENYID] ASC)
);


GO
PRINT N'Creating [dbo].[T_OSZTALYCSOPORT_TANULOESEMENY].[I_T_4027556_4025296]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027556_4025296]
    ON [dbo].[T_OSZTALYCSOPORT_TANULOESEMENY]([C_OSZTALYCSOPORTID] ASC);


GO
PRINT N'Creating [dbo].[T_OSZTALYCSOPORT_TANULOESEMENY].[I_T_4027557_4025699]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4027557_4025699]
    ON [dbo].[T_OSZTALYCSOPORT_TANULOESEMENY]([C_TANULOESEMENYID] ASC);


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


GO
ALTER TABLE [dbo].[T_TANULO_TANULOESEMENY]
    ADD CONSTRAINT [PK_T_T_TANULO_TANULOESEMENY] PRIMARY KEY CLUSTERED ([C_TANULOID] ASC, [C_TANULOESEMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULO_TANULOESEMENY].[I_T_4025711_4025178]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025711_4025178]
    ON [dbo].[T_TANULO_TANULOESEMENY]([C_TANULOID] ASC);


GO
PRINT N'Creating [dbo].[T_TANULO_TANULOESEMENY].[I_T_4025712_4025699]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025712_4025699]
    ON [dbo].[T_TANULO_TANULOESEMENY]([C_TANULOESEMENYID] ASC);


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_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].[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_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_4027556_4027555]...';


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


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


GO
ALTER TABLE [dbo].[T_OSZTALYCSOPORT_TANULOESEMENY] WITH NOCHECK
    ADD CONSTRAINT [FK_4027557_4027555] FOREIGN KEY ([C_TANULOESEMENYID]) REFERENCES [dbo].[T_TANULOESEMENY] ([ID]);


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


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


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


GO
ALTER TABLE [dbo].[T_TANULO_TANULOESEMENY] WITH NOCHECK
    ADD CONSTRAINT [FK_4025712_4025710] FOREIGN KEY ([C_TANULOESEMENYID]) REFERENCES [dbo].[T_TANULOESEMENY] ([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_TARGY AS nvarchar(MAX)) AS C_TARGY, CAST( C_FALIUJSAGMEGJELENES AS nvarchar(MAX)) AS C_FALIUJSAGMEGJELENES, CAST( C_MINDENKINEK AS nvarchar(MAX)) AS C_MINDENKINEK, CONVERT(nvarchar(MAX), C_VEGE, 121) AS C_VEGE, 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_TARGY, C_FALIUJSAGMEGJELENES, C_MINDENKINEK, C_VEGE, 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_TARGY AS nvarchar(MAX)) AS C_TARGY, CAST( C_FALIUJSAGMEGJELENES AS nvarchar(MAX)) AS C_FALIUJSAGMEGJELENES, CAST( C_MINDENKINEK AS nvarchar(MAX)) AS C_MINDENKINEK, CONVERT(nvarchar(MAX), C_VEGE, 121) AS C_VEGE, 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_TARGY, C_FALIUJSAGMEGJELENES, C_MINDENKINEK, C_VEGE, 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 [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_4025714_4025713];

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

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

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_4025699_4025708];

ALTER TABLE [dbo].[T_OSZTALYCSOPORT_TANULOESEMENY] WITH CHECK CHECK CONSTRAINT [FK_4027556_4027555];

ALTER TABLE [dbo].[T_OSZTALYCSOPORT_TANULOESEMENY] WITH CHECK CHECK CONSTRAINT [FK_4027557_4027555];

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

ALTER TABLE [dbo].[T_TANULO_TANULOESEMENY] WITH CHECK CHECK CONSTRAINT [FK_4025712_4025710];


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_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
