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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_EMAIL] (
    [ID]                  INT            IDENTITY (1, 1) NOT NULL,
    [C_EMAILTIPUSA]       INT            NULL,
    [C_EMAILCIM]          NVARCHAR (200) NOT NULL,
    [C_ALAPERTELMEZETT]   CHAR (1)       DEFAULT ('T') NOT NULL,
    [C_ISPUBLIC]          CHAR (1)       DEFAULT ('F') NULL,
    [C_GONDVISELOID]      INT            NULL,
    [C_FELHASZNALOID]     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_98761] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_EMAIL])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_EMAIL] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_EMAIL] ([ID], [C_EMAILTIPUSA], [C_EMAILCIM], [C_ALAPERTELMEZETT], [C_GONDVISELOID], [C_FELHASZNALOID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_EMAILTIPUSA],
                 [C_EMAILCIM],
                 [C_ALAPERTELMEZETT],
                 [C_GONDVISELOID],
                 [C_FELHASZNALOID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_EMAIL]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_EMAIL] OFF;
    END

DROP TABLE [dbo].[T_EMAIL];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_EMAIL]', N'T_EMAIL';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_98761]', N'PK_T_9876', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_EMAIL].[I_T_9876_24312]...';


GO
CREATE NONCLUSTERED INDEX [I_T_9876_24312]
    ON [dbo].[T_EMAIL]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_EMAILTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_EMAIL].[I_T_37640]...';


GO
CREATE NONCLUSTERED INDEX [I_T_37640]
    ON [dbo].[T_EMAIL]([C_EMAILCIM] ASC);


GO
PRINT N'Creating [dbo].[T_EMAIL].[I_T_4025224_4025222]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025224_4025222]
    ON [dbo].[T_EMAIL]([C_GONDVISELOID] ASC);


GO
PRINT N'Creating [dbo].[T_EMAIL].[I_T_4025239_4025237]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025239_4025237]
    ON [dbo].[T_EMAIL]([C_FELHASZNALOID] ASC);


GO
PRINT N'Creating [dbo].[T_EMAIL].[I_T_987602_987600]...';


GO
CREATE NONCLUSTERED INDEX [I_T_987602_987600]
    ON [dbo].[T_EMAIL]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_EMAIL].[I_T_987605_987603]...';


GO
CREATE NONCLUSTERED INDEX [I_T_987605_987603]
    ON [dbo].[T_EMAIL]([C_TANEVID] ASC);


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_TELEFON] (
    [ID]                  INT            IDENTITY (1, 1) NOT NULL,
    [C_TELEFONTIPUSA]     INT            NOT NULL,
    [C_TELEFONSZAM]       NVARCHAR (100) NOT NULL,
    [C_LEIRAS]            NVARCHAR (50)  NULL,
    [C_ALAPERTELMEZETT]   CHAR (1)       DEFAULT ('F') NOT NULL,
    [C_ISPUBLIC]          CHAR (1)       DEFAULT ('F') NULL,
    [C_GONDVISELOID]      INT            NULL,
    [C_FELHASZNALOID]     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_207751] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_TELEFON])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TELEFON] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_TELEFON] ([ID], [C_TELEFONTIPUSA], [C_TELEFONSZAM], [C_LEIRAS], [C_ALAPERTELMEZETT], [C_GONDVISELOID], [C_FELHASZNALOID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_TELEFONTIPUSA],
                 [C_TELEFONSZAM],
                 [C_LEIRAS],
                 [C_ALAPERTELMEZETT],
                 [C_GONDVISELOID],
                 [C_FELHASZNALOID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_TELEFON]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_TELEFON] OFF;
    END

DROP TABLE [dbo].[T_TELEFON];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_TELEFON]', N'T_TELEFON';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_207751]', N'PK_T_20775', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_TELEFON].[I_T_20775_24305]...';


GO
CREATE NONCLUSTERED INDEX [I_T_20775_24305]
    ON [dbo].[T_TELEFON]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_TELEFONTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_TELEFON].[I_T_1546382]...';


GO
CREATE NONCLUSTERED INDEX [I_T_1546382]
    ON [dbo].[T_TELEFON]([C_TELEFONSZAM] ASC);


GO
PRINT N'Creating [dbo].[T_TELEFON].[I_T_4025221_4025219]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025221_4025219]
    ON [dbo].[T_TELEFON]([C_GONDVISELOID] ASC);


GO
PRINT N'Creating [dbo].[T_TELEFON].[I_T_4025236_4025234]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025236_4025234]
    ON [dbo].[T_TELEFON]([C_FELHASZNALOID] ASC);


GO
PRINT N'Creating [dbo].[T_TELEFON].[I_T_2077502_2077500]...';


GO
CREATE NONCLUSTERED INDEX [I_T_2077502_2077500]
    ON [dbo].[T_TELEFON]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_TELEFON].[I_T_2077505_2077503]...';


GO
CREATE NONCLUSTERED INDEX [I_T_2077505_2077503]
    ON [dbo].[T_TELEFON]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_EMAIL] WITH NOCHECK
    ADD CONSTRAINT [FK_4025239_4025237] FOREIGN KEY ([C_FELHASZNALOID]) REFERENCES [dbo].[T_FELHASZNALO] ([ID]);


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


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


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


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


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


GO
ALTER TABLE [dbo].[T_EMAIL] WITH NOCHECK
    ADD CONSTRAINT [FK_9876_24312] FOREIGN KEY ([C_EMAILTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_EMAILTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_TELEFON] WITH NOCHECK
    ADD CONSTRAINT [FK_20775_24305] FOREIGN KEY ([C_TELEFONTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_TELEFONTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_TELEFON] WITH NOCHECK
    ADD CONSTRAINT [FK_4025236_4025234] FOREIGN KEY ([C_FELHASZNALOID]) REFERENCES [dbo].[T_FELHASZNALO] ([ID]);


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


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


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


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


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


GO

  CREATE TRIGGER tr_emailLog ON T_EMAIL 
  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_EMAILTIPUSA AS nvarchar(MAX)) AS C_EMAILTIPUSA, CAST( C_EMAILCIM AS nvarchar(MAX)) AS C_EMAILCIM, CAST( C_ALAPERTELMEZETT AS nvarchar(MAX)) AS C_ALAPERTELMEZETT, CAST( C_ISPUBLIC AS nvarchar(MAX)) AS C_ISPUBLIC, CAST( C_GONDVISELOID AS nvarchar(MAX)) AS C_GONDVISELOID, CAST( C_FELHASZNALOID AS nvarchar(MAX)) AS C_FELHASZNALOID, 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_EMAILTIPUSA, C_EMAILCIM, C_ALAPERTELMEZETT, C_ISPUBLIC, C_GONDVISELOID, C_FELHASZNALOID, 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_EMAILTIPUSA AS nvarchar(MAX)) AS C_EMAILTIPUSA, CAST( C_EMAILCIM AS nvarchar(MAX)) AS C_EMAILCIM, CAST( C_ALAPERTELMEZETT AS nvarchar(MAX)) AS C_ALAPERTELMEZETT, CAST( C_ISPUBLIC AS nvarchar(MAX)) AS C_ISPUBLIC, CAST( C_GONDVISELOID AS nvarchar(MAX)) AS C_GONDVISELOID, CAST( C_FELHASZNALOID AS nvarchar(MAX)) AS C_FELHASZNALOID, 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_EMAILTIPUSA, C_EMAILCIM, C_ALAPERTELMEZETT, C_ISPUBLIC, C_GONDVISELOID, C_FELHASZNALOID, 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_EMAIL', '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_EMAIL', '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_EMAIL', 
        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'Creating [dbo].[tr_telefonLog]...';


GO

  CREATE TRIGGER tr_telefonLog ON T_TELEFON 
  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_TELEFONTIPUSA AS nvarchar(MAX)) AS C_TELEFONTIPUSA, CAST( C_TELEFONSZAM AS nvarchar(MAX)) AS C_TELEFONSZAM, CAST( C_LEIRAS AS nvarchar(MAX)) AS C_LEIRAS, CAST( C_ALAPERTELMEZETT AS nvarchar(MAX)) AS C_ALAPERTELMEZETT, CAST( C_ISPUBLIC AS nvarchar(MAX)) AS C_ISPUBLIC, CAST( C_GONDVISELOID AS nvarchar(MAX)) AS C_GONDVISELOID, CAST( C_FELHASZNALOID AS nvarchar(MAX)) AS C_FELHASZNALOID, 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_TELEFONTIPUSA, C_TELEFONSZAM, C_LEIRAS, C_ALAPERTELMEZETT, C_ISPUBLIC, C_GONDVISELOID, C_FELHASZNALOID, 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_TELEFONTIPUSA AS nvarchar(MAX)) AS C_TELEFONTIPUSA, CAST( C_TELEFONSZAM AS nvarchar(MAX)) AS C_TELEFONSZAM, CAST( C_LEIRAS AS nvarchar(MAX)) AS C_LEIRAS, CAST( C_ALAPERTELMEZETT AS nvarchar(MAX)) AS C_ALAPERTELMEZETT, CAST( C_ISPUBLIC AS nvarchar(MAX)) AS C_ISPUBLIC, CAST( C_GONDVISELOID AS nvarchar(MAX)) AS C_GONDVISELOID, CAST( C_FELHASZNALOID AS nvarchar(MAX)) AS C_FELHASZNALOID, 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_TELEFONTIPUSA, C_TELEFONSZAM, C_LEIRAS, C_ALAPERTELMEZETT, C_ISPUBLIC, C_GONDVISELOID, C_FELHASZNALOID, 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_TELEFON', '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_TELEFON', '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_TELEFON', 
        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_EMAIL_OSSZES]...';


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


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


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


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


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

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

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

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

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

ALTER TABLE [dbo].[T_TELEFON] WITH CHECK CHECK CONSTRAINT [FK_20775_24305];

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

ALTER TABLE [dbo].[T_TELEFON] WITH CHECK CHECK CONSTRAINT [FK_4025236_4025234];

ALTER TABLE [dbo].[T_TELEFON] WITH CHECK CHECK CONSTRAINT [FK_2077502_2077500];

ALTER TABLE [dbo].[T_TELEFON] WITH CHECK CHECK CONSTRAINT [FK_2077505_2077503];


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_EMAIL', 'T_EMAIL_OSSZES',
					 'T_TELEFON', 'T_TELEFON_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 
