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


GO
ALTER TABLE [dbo].[T_TELEFON] DROP CONSTRAINT [FK_4025221_4025219];


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


GO
ALTER TABLE [dbo].[T_GONDVISELOERTESITES] DROP CONSTRAINT [FK_4025878_4025876];


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


GO
ALTER TABLE [dbo].[T_OLDALLATOGATOTTSAG] DROP CONSTRAINT [FK_4026047_4026046];


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


GO
ALTER TABLE [dbo].[T_JOGOSULTGONDVISELOK_HOZZAREN] DROP CONSTRAINT [FK_4026216_4026215];


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


GO
ALTER TABLE [dbo].[T_FALIUJSAGLATOGATTOTSAG] DROP CONSTRAINT [FK_4025824_4025822];


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


GO
ALTER TABLE [dbo].[T_CIM] DROP CONSTRAINT [FK_4025184_4025183];


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] DROP CONSTRAINT [FK_4026219_4026218];


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] DROP CONSTRAINT [FK_4026222_4026221];


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPES] DROP CONSTRAINT [FK_4025187_4025186];


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


GO
ALTER TABLE [dbo].[T_EMAIL] DROP CONSTRAINT [FK_4025224_4025222];


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] DROP CONSTRAINT [FK_4025181_4025180];


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] DROP CONSTRAINT [FK_402516902_402516900];


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] DROP CONSTRAINT [FK_402516905_402516903];


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] DROP CONSTRAINT [FK_4025169_4025177];


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


GO
ALTER TABLE [dbo].[T_DIAKIGAZOLVANYIGENYLES] DROP CONSTRAINT [FK_4025785_4025784];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_GONDVISELO] (
    [ID]                       INT            IDENTITY (1, 1) NOT NULL,
    [C_AKTIV]                  CHAR (1)       DEFAULT ('T') NULL,
    [C_GONDVISELESKEZDETE]     DATETIME       NULL,
    [C_GONDVISELESVEGE]        DATETIME       NULL,
    [C_GYERMEKETEGYEDULNEVELO] CHAR (1)       DEFAULT ('F') NULL,
    [C_NEV]                    NVARCHAR (200) NOT NULL,
    [C_NYUGDIJAS]              CHAR (1)       DEFAULT ('F') NULL,
    [C_ROKONSAGFOKA]           INT            NOT NULL,
    [C_ISEMAILERTESITES]       CHAR (1)       DEFAULT ('F') NULL,
    [C_ISTORVENYESKEPVISELO]   CHAR (1)       DEFAULT ('F') NOT NULL,
    [C_TANULOID]               INT            NOT 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_40251691] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_GONDVISELO])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_GONDVISELO] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_GONDVISELO] ([ID], [C_AKTIV], [C_GONDVISELESKEZDETE], [C_GONDVISELESVEGE], [C_GYERMEKETEGYEDULNEVELO], [C_NEV], [C_NYUGDIJAS], [C_ROKONSAGFOKA], [C_ISEMAILERTESITES], [C_TANULOID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_AKTIV],
                 [C_GONDVISELESKEZDETE],
                 [C_GONDVISELESVEGE],
                 [C_GYERMEKETEGYEDULNEVELO],
                 [C_NEV],
                 [C_NYUGDIJAS],
                 [C_ROKONSAGFOKA],
                 [C_ISEMAILERTESITES],
                 [C_TANULOID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_GONDVISELO]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_GONDVISELO] OFF;
    END

DROP TABLE [dbo].[T_GONDVISELO];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_GONDVISELO]', N'T_GONDVISELO';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40251691]', N'PK_T_4025169', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_GONDVISELO].[I_T_4025169_4025177]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025169_4025177]
    ON [dbo].[T_GONDVISELO]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_ROKONSAGFOKA] ASC);


GO
PRINT N'Creating [dbo].[T_GONDVISELO].[I_T_4025181_4025180]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025181_4025180]
    ON [dbo].[T_GONDVISELO]([C_TANULOID] ASC);


GO
PRINT N'Creating [dbo].[T_GONDVISELO].[I_T_402516902_402516900]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402516902_402516900]
    ON [dbo].[T_GONDVISELO]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_GONDVISELO].[I_T_402516905_402516903]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402516905_402516903]
    ON [dbo].[T_GONDVISELO]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_TELEFON] WITH NOCHECK
    ADD CONSTRAINT [FK_4025221_4025219] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_GONDVISELOERTESITES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025878_4025876] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_OLDALLATOGATOTTSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_4026047_4026046] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_JOGOSULTGONDVISELOK_HOZZAREN] WITH NOCHECK
    ADD CONSTRAINT [FK_4026216_4026215] FOREIGN KEY ([C_JOGOSULTGONDVISELOKID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FALIUJSAGLATOGATTOTSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_4025824_4025822] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_CIM] WITH NOCHECK
    ADD CONSTRAINT [FK_4025184_4025183] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026219_4026218] FOREIGN KEY ([C_DOKPARTNERGONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026222_4026221] FOREIGN KEY ([C_FELTOLTOGONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FELHASZNALOBELEPES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025187_4025186] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO
ALTER TABLE [dbo].[T_EMAIL] WITH NOCHECK
    ADD CONSTRAINT [FK_4025224_4025222] FOREIGN KEY ([C_GONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


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


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_GONDVISELO] WITH NOCHECK
    ADD CONSTRAINT [FK_4025169_4025177] FOREIGN KEY ([C_ROKONSAGFOKA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ROKONSAGIFOK] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_DIAKIGAZOLVANYIGENYLES] WITH NOCHECK
    ADD CONSTRAINT [FK_4025785_4025784] FOREIGN KEY ([C_IGENYLOGONDVISELOID]) REFERENCES [dbo].[T_GONDVISELO] ([ID]);


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


GO

  CREATE TRIGGER tr_gondviseloLog ON T_GONDVISELO 
  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_AKTIV AS nvarchar(MAX)) AS C_AKTIV, CONVERT(nvarchar(MAX), C_GONDVISELESKEZDETE, 121) AS C_GONDVISELESKEZDETE, CONVERT(nvarchar(MAX), C_GONDVISELESVEGE, 121) AS C_GONDVISELESVEGE, CAST( C_GYERMEKETEGYEDULNEVELO AS nvarchar(MAX)) AS C_GYERMEKETEGYEDULNEVELO, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_NYUGDIJAS AS nvarchar(MAX)) AS C_NYUGDIJAS, CAST( C_ROKONSAGFOKA AS nvarchar(MAX)) AS C_ROKONSAGFOKA, CAST( C_ISEMAILERTESITES AS nvarchar(MAX)) AS C_ISEMAILERTESITES, CAST( C_ISTORVENYESKEPVISELO AS nvarchar(MAX)) AS C_ISTORVENYESKEPVISELO, CAST( C_TANULOID AS nvarchar(MAX)) AS C_TANULOID, 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_AKTIV, C_GONDVISELESKEZDETE, C_GONDVISELESVEGE, C_GYERMEKETEGYEDULNEVELO, C_NEV, C_NYUGDIJAS, C_ROKONSAGFOKA, C_ISEMAILERTESITES, C_ISTORVENYESKEPVISELO, C_TANULOID, 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, CAST( C_AKTIV AS nvarchar(MAX)) AS C_AKTIV, CONVERT(nvarchar(MAX), C_GONDVISELESKEZDETE, 121) AS C_GONDVISELESKEZDETE, CONVERT(nvarchar(MAX), C_GONDVISELESVEGE, 121) AS C_GONDVISELESVEGE, CAST( C_GYERMEKETEGYEDULNEVELO AS nvarchar(MAX)) AS C_GYERMEKETEGYEDULNEVELO, CAST( C_NEV AS nvarchar(MAX)) AS C_NEV, CAST( C_NYUGDIJAS AS nvarchar(MAX)) AS C_NYUGDIJAS, CAST( C_ROKONSAGFOKA AS nvarchar(MAX)) AS C_ROKONSAGFOKA, CAST( C_ISEMAILERTESITES AS nvarchar(MAX)) AS C_ISEMAILERTESITES, CAST( C_ISTORVENYESKEPVISELO AS nvarchar(MAX)) AS C_ISTORVENYESKEPVISELO, CAST( C_TANULOID AS nvarchar(MAX)) AS C_TANULOID, 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_AKTIV, C_GONDVISELESKEZDETE, C_GONDVISELESVEGE, C_GYERMEKETEGYEDULNEVELO, C_NEV, C_NYUGDIJAS, C_ROKONSAGFOKA, C_ISEMAILERTESITES, C_ISTORVENYESKEPVISELO, C_TANULOID, 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_GONDVISELO', '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_GONDVISELO', '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_GONDVISELO', 
        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_GONDVISELO_OSSZES]...';


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


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


GO
ALTER TABLE [dbo].[T_TELEFON] WITH CHECK CHECK CONSTRAINT [FK_4025221_4025219];

ALTER TABLE [dbo].[T_GONDVISELOERTESITES] WITH CHECK CHECK CONSTRAINT [FK_4025878_4025876];

ALTER TABLE [dbo].[T_OLDALLATOGATOTTSAG] WITH CHECK CHECK CONSTRAINT [FK_4026047_4026046];

ALTER TABLE [dbo].[T_JOGOSULTGONDVISELOK_HOZZAREN] WITH CHECK CHECK CONSTRAINT [FK_4026216_4026215];

ALTER TABLE [dbo].[T_FALIUJSAGLATOGATTOTSAG] WITH CHECK CHECK CONSTRAINT [FK_4025824_4025822];

ALTER TABLE [dbo].[T_CIM] WITH CHECK CHECK CONSTRAINT [FK_4025184_4025183];

ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] WITH CHECK CHECK CONSTRAINT [FK_4026219_4026218];

ALTER TABLE [dbo].[T_IKTATOTTDOKUMENTUMOK] WITH CHECK CHECK CONSTRAINT [FK_4026222_4026221];

ALTER TABLE [dbo].[T_FELHASZNALOBELEPES] WITH CHECK CHECK CONSTRAINT [FK_4025187_4025186];

ALTER TABLE [dbo].[T_EMAIL] WITH CHECK CHECK CONSTRAINT [FK_4025224_4025222];

ALTER TABLE [dbo].[T_GONDVISELO] WITH CHECK CHECK CONSTRAINT [FK_4025181_4025180];

ALTER TABLE [dbo].[T_GONDVISELO] WITH CHECK CHECK CONSTRAINT [FK_402516902_402516900];

ALTER TABLE [dbo].[T_GONDVISELO] WITH CHECK CHECK CONSTRAINT [FK_402516905_402516903];

ALTER TABLE [dbo].[T_GONDVISELO] WITH CHECK CHECK CONSTRAINT [FK_4025169_4025177];

ALTER TABLE [dbo].[T_DIAKIGAZOLVANYIGENYLES] WITH CHECK CHECK CONSTRAINT [FK_4025785_4025784];


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_GONDVISELO', 'T_GONDVISELO_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
