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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026121];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026122];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026343];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026352];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4026365];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4027631];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026116_4027635];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026125_4026124];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_4026129_4026127];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_402611602_402611600];


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP CONSTRAINT [FK_402611605_402611603];


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


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK] (
    [ID]                            INT             IDENTITY (1, 1) NOT NULL,
    [C_ALKALMAZASKEZDETE]           DATETIME        NULL,
    [C_ALKALMAZASMEGSZUNESE]        DATETIME        NULL,
    [C_KOTELEZOORASZAM]             NUMERIC (10, 2) NOT NULL,
    [C_MUNKAIDOKEDVEZMENYORASZAM]   INT             NULL,
    [C_MUNKAKORTIPUSA]              INT             NULL,
    [C_MUNKAVISZONYTIPUSA]          INT             NULL,
    [C_BETOLTETLENALLASHELY]        CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_MUNKAIDOKEDVEZMENYOKASZOVEG] NVARCHAR (MAX)  NULL,
    [C_TARTOSHELYETTESITES]         CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_PEDAGOGUSSTATUSZA]           INT             NULL,
    [C_BESOROLASIFOKOZAT]           INT             NULL,
    [C_MUNKAIDOKEDVEZMENYOKA]       INT             NULL,
    [C_NYUGDIJAS]                   CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_RESZMUNKAIDOSZAZALEK]        INT             DEFAULT ((100)) NULL,
    [C_FOGLALKOZTATASTIPUS]         INT             NULL,
    [C_VEZETOIORASZAMOK]            INT             NULL,
    [C_UTAZOGYOGYPEDAGOGUS]         CHAR (1)        DEFAULT ('F') NULL,
    [C_CSOKKENTETTMUNKAIDOS]        CHAR (1)        DEFAULT ('F') NULL,
    [C_SZAKERTOMESTERPEDAGOGUS]     CHAR (1)        DEFAULT ('F') NULL,
    [C_SZABADALLASHELY]             CHAR (1)        DEFAULT ('F') NOT NULL,
    [C_FELADATELLATASIHELYID]       INT             NULL,
    [C_ALKALMAZOTTID]               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_40261161] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_MUNKAUGYIADATOK])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK] ([ID], [C_ALKALMAZASKEZDETE], [C_ALKALMAZASMEGSZUNESE], [C_KOTELEZOORASZAM], [C_MUNKAIDOKEDVEZMENYORASZAM], [C_MUNKAKORTIPUSA], [C_MUNKAVISZONYTIPUSA], [C_BETOLTETLENALLASHELY], [C_MUNKAIDOKEDVEZMENYOKASZOVEG], [C_TARTOSHELYETTESITES], [C_PEDAGOGUSSTATUSZA], [C_BESOROLASIFOKOZAT], [C_MUNKAIDOKEDVEZMENYOKA], [C_NYUGDIJAS], [C_RESZMUNKAIDOSZAZALEK], [C_FOGLALKOZTATASTIPUS], [C_VEZETOIORASZAMOK], [C_UTAZOGYOGYPEDAGOGUS], [C_CSOKKENTETTMUNKAIDOS], [C_SZAKERTOMESTERPEDAGOGUS], [C_FELADATELLATASIHELYID], [C_ALKALMAZOTTID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_ALKALMAZASKEZDETE],
                 [C_ALKALMAZASMEGSZUNESE],
                 [C_KOTELEZOORASZAM],
                 [C_MUNKAIDOKEDVEZMENYORASZAM],
                 [C_MUNKAKORTIPUSA],
                 [C_MUNKAVISZONYTIPUSA],
                 [C_BETOLTETLENALLASHELY],
                 [C_MUNKAIDOKEDVEZMENYOKASZOVEG],
                 [C_TARTOSHELYETTESITES],
                 [C_PEDAGOGUSSTATUSZA],
                 [C_BESOROLASIFOKOZAT],
                 [C_MUNKAIDOKEDVEZMENYOKA],
                 [C_NYUGDIJAS],
                 [C_RESZMUNKAIDOSZAZALEK],
                 [C_FOGLALKOZTATASTIPUS],
                 [C_VEZETOIORASZAMOK],
                 [C_UTAZOGYOGYPEDAGOGUS],
                 [C_CSOKKENTETTMUNKAIDOS],
                 [C_SZAKERTOMESTERPEDAGOGUS],
                 [C_FELADATELLATASIHELYID],
                 [C_ALKALMAZOTTID],
                 [C_INTEZMENYID],
                 [C_TANEVID],
                 [TOROLT],
                 [SERIAL],
                 [LASTCHANGED],
                 [CREATED],
                 [MODIFIER],
                 [CREATOR],
                 [ELOZOTANEVIREKORDID],
                 [NNID]
        FROM     [dbo].[T_MUNKAUGYIADATOK]
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK] OFF;
    END

DROP TABLE [dbo].[T_MUNKAUGYIADATOK];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_MUNKAUGYIADATOK]', N'T_MUNKAUGYIADATOK';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40261161]', N'PK_T_4026116', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026121]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026121]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MUNKAKORTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026122]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026122]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MUNKAVISZONYTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026343]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026343]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_PEDAGOGUSSTATUSZA] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026352]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026352]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_BESOROLASIFOKOZAT] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4026365]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4026365]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_MUNKAIDOKEDVEZMENYOKA] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4027631]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4027631]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_FOGLALKOZTATASTIPUS] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026116_4027635]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026116_4027635]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_VEZETOIORASZAMOK] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026125_4026124]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026125_4026124]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_FELADATELLATASIHELYID] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_4026129_4026127]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4026129_4026127]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_ALKALMAZOTTID] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_402611602_402611600]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402611602_402611600]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_MUNKAUGYIADATOK].[I_T_402611605_402611603]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402611605_402611603]
    ON [dbo].[T_MUNKAUGYIADATOK]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026121] FOREIGN KEY ([C_MUNKAKORTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_MUNKAKORTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026122] FOREIGN KEY ([C_MUNKAVISZONYTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_MUNKAVISZONYTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026343] FOREIGN KEY ([C_PEDAGOGUSSTATUSZA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_PEDAGOGUSSTATUSZ] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026352] FOREIGN KEY ([C_BESOROLASIFOKOZAT], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_BESOROLASIFOKOZATTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4026365] FOREIGN KEY ([C_MUNKAIDOKEDVEZMENYOKA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_MUNKAIDOKEDVEZMENYOKA] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4027631] FOREIGN KEY ([C_FOGLALKOZTATASTIPUS], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_FOGLALKOZTATASTIPUSA] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026116_4027635] FOREIGN KEY ([C_VEZETOIORASZAMOK], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_VEZETOIORASZAMOKTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026125_4026124] FOREIGN KEY ([C_FELADATELLATASIHELYID]) REFERENCES [dbo].[T_FELADATELLATASIHELY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH NOCHECK
    ADD CONSTRAINT [FK_4026129_4026127] FOREIGN KEY ([C_ALKALMAZOTTID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


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


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


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


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


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


GO

  CREATE TRIGGER tr_munkaugyiadatokLog ON T_MUNKAUGYIADATOK 
  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_ALKALMAZASKEZDETE, 121) AS C_ALKALMAZASKEZDETE, CONVERT(nvarchar(MAX), C_ALKALMAZASMEGSZUNESE, 121) AS C_ALKALMAZASMEGSZUNESE, CAST( C_KOTELEZOORASZAM AS nvarchar(MAX)) AS C_KOTELEZOORASZAM, CAST( C_MUNKAIDOKEDVEZMENYORASZAM AS nvarchar(MAX)) AS C_MUNKAIDOKEDVEZMENYORASZAM, CAST( C_MUNKAKORTIPUSA AS nvarchar(MAX)) AS C_MUNKAKORTIPUSA, CAST( C_MUNKAVISZONYTIPUSA AS nvarchar(MAX)) AS C_MUNKAVISZONYTIPUSA, CAST( C_BETOLTETLENALLASHELY AS nvarchar(MAX)) AS C_BETOLTETLENALLASHELY, CAST( C_MUNKAIDOKEDVEZMENYOKASZOVEG AS nvarchar(MAX)) AS C_MUNKAIDOKEDVEZMENYOKASZOVEG, CAST( C_TARTOSHELYETTESITES AS nvarchar(MAX)) AS C_TARTOSHELYETTESITES, CAST( C_PEDAGOGUSSTATUSZA AS nvarchar(MAX)) AS C_PEDAGOGUSSTATUSZA, CAST( C_BESOROLASIFOKOZAT AS nvarchar(MAX)) AS C_BESOROLASIFOKOZAT, CAST( C_MUNKAIDOKEDVEZMENYOKA AS nvarchar(MAX)) AS C_MUNKAIDOKEDVEZMENYOKA, CAST( C_NYUGDIJAS AS nvarchar(MAX)) AS C_NYUGDIJAS, CAST( C_RESZMUNKAIDOSZAZALEK AS nvarchar(MAX)) AS C_RESZMUNKAIDOSZAZALEK, CAST( C_FOGLALKOZTATASTIPUS AS nvarchar(MAX)) AS C_FOGLALKOZTATASTIPUS, CAST( C_VEZETOIORASZAMOK AS nvarchar(MAX)) AS C_VEZETOIORASZAMOK, CAST( C_UTAZOGYOGYPEDAGOGUS AS nvarchar(MAX)) AS C_UTAZOGYOGYPEDAGOGUS, CAST( C_CSOKKENTETTMUNKAIDOS AS nvarchar(MAX)) AS C_CSOKKENTETTMUNKAIDOS, CAST( C_SZAKERTOMESTERPEDAGOGUS AS nvarchar(MAX)) AS C_SZAKERTOMESTERPEDAGOGUS, CAST( C_SZABADALLASHELY AS nvarchar(MAX)) AS C_SZABADALLASHELY, CAST( C_FELADATELLATASIHELYID AS nvarchar(MAX)) AS C_FELADATELLATASIHELYID, CAST( C_ALKALMAZOTTID AS nvarchar(MAX)) AS C_ALKALMAZOTTID, 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_ALKALMAZASKEZDETE, C_ALKALMAZASMEGSZUNESE, C_KOTELEZOORASZAM, C_MUNKAIDOKEDVEZMENYORASZAM, C_MUNKAKORTIPUSA, C_MUNKAVISZONYTIPUSA, C_BETOLTETLENALLASHELY, C_MUNKAIDOKEDVEZMENYOKASZOVEG, C_TARTOSHELYETTESITES, C_PEDAGOGUSSTATUSZA, C_BESOROLASIFOKOZAT, C_MUNKAIDOKEDVEZMENYOKA, C_NYUGDIJAS, C_RESZMUNKAIDOSZAZALEK, C_FOGLALKOZTATASTIPUS, C_VEZETOIORASZAMOK, C_UTAZOGYOGYPEDAGOGUS, C_CSOKKENTETTMUNKAIDOS, C_SZAKERTOMESTERPEDAGOGUS, C_SZABADALLASHELY, C_FELADATELLATASIHELYID, C_ALKALMAZOTTID, 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_ALKALMAZASKEZDETE, 121) AS C_ALKALMAZASKEZDETE, CONVERT(nvarchar(MAX), C_ALKALMAZASMEGSZUNESE, 121) AS C_ALKALMAZASMEGSZUNESE, CAST( C_KOTELEZOORASZAM AS nvarchar(MAX)) AS C_KOTELEZOORASZAM, CAST( C_MUNKAIDOKEDVEZMENYORASZAM AS nvarchar(MAX)) AS C_MUNKAIDOKEDVEZMENYORASZAM, CAST( C_MUNKAKORTIPUSA AS nvarchar(MAX)) AS C_MUNKAKORTIPUSA, CAST( C_MUNKAVISZONYTIPUSA AS nvarchar(MAX)) AS C_MUNKAVISZONYTIPUSA, CAST( C_BETOLTETLENALLASHELY AS nvarchar(MAX)) AS C_BETOLTETLENALLASHELY, CAST( C_MUNKAIDOKEDVEZMENYOKASZOVEG AS nvarchar(MAX)) AS C_MUNKAIDOKEDVEZMENYOKASZOVEG, CAST( C_TARTOSHELYETTESITES AS nvarchar(MAX)) AS C_TARTOSHELYETTESITES, CAST( C_PEDAGOGUSSTATUSZA AS nvarchar(MAX)) AS C_PEDAGOGUSSTATUSZA, CAST( C_BESOROLASIFOKOZAT AS nvarchar(MAX)) AS C_BESOROLASIFOKOZAT, CAST( C_MUNKAIDOKEDVEZMENYOKA AS nvarchar(MAX)) AS C_MUNKAIDOKEDVEZMENYOKA, CAST( C_NYUGDIJAS AS nvarchar(MAX)) AS C_NYUGDIJAS, CAST( C_RESZMUNKAIDOSZAZALEK AS nvarchar(MAX)) AS C_RESZMUNKAIDOSZAZALEK, CAST( C_FOGLALKOZTATASTIPUS AS nvarchar(MAX)) AS C_FOGLALKOZTATASTIPUS, CAST( C_VEZETOIORASZAMOK AS nvarchar(MAX)) AS C_VEZETOIORASZAMOK, CAST( C_UTAZOGYOGYPEDAGOGUS AS nvarchar(MAX)) AS C_UTAZOGYOGYPEDAGOGUS, CAST( C_CSOKKENTETTMUNKAIDOS AS nvarchar(MAX)) AS C_CSOKKENTETTMUNKAIDOS, CAST( C_SZAKERTOMESTERPEDAGOGUS AS nvarchar(MAX)) AS C_SZAKERTOMESTERPEDAGOGUS, CAST( C_SZABADALLASHELY AS nvarchar(MAX)) AS C_SZABADALLASHELY, CAST( C_FELADATELLATASIHELYID AS nvarchar(MAX)) AS C_FELADATELLATASIHELYID, CAST( C_ALKALMAZOTTID AS nvarchar(MAX)) AS C_ALKALMAZOTTID, 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_ALKALMAZASKEZDETE, C_ALKALMAZASMEGSZUNESE, C_KOTELEZOORASZAM, C_MUNKAIDOKEDVEZMENYORASZAM, C_MUNKAKORTIPUSA, C_MUNKAVISZONYTIPUSA, C_BETOLTETLENALLASHELY, C_MUNKAIDOKEDVEZMENYOKASZOVEG, C_TARTOSHELYETTESITES, C_PEDAGOGUSSTATUSZA, C_BESOROLASIFOKOZAT, C_MUNKAIDOKEDVEZMENYOKA, C_NYUGDIJAS, C_RESZMUNKAIDOSZAZALEK, C_FOGLALKOZTATASTIPUS, C_VEZETOIORASZAMOK, C_UTAZOGYOGYPEDAGOGUS, C_CSOKKENTETTMUNKAIDOS, C_SZAKERTOMESTERPEDAGOGUS, C_SZABADALLASHELY, C_FELADATELLATASIHELYID, C_ALKALMAZOTTID, 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_MUNKAUGYIADATOK', '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_MUNKAUGYIADATOK', '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_MUNKAUGYIADATOK', 
        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_MUNKAUGYIADATOK_OSSZES]...';


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


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


GO
ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026121];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026122];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026343];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026352];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4026365];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4027631];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026116_4027635];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026125_4026124];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_4026129_4026127];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_402611602_402611600];

ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] WITH CHECK CHECK CONSTRAINT [FK_402611605_402611603];


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_MUNKAUGYIADATOK', 'T_MUNKAUGYIADATOK_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
