﻿GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


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


GO
CREATE TABLE [dbo].[T_AGAZATTIPUS] (
    [ID]              INT      NOT NULL,
    [C_ALINTEZMENYID] INT      NOT NULL,
    [C_ALTANEVID]     INT      NOT NULL,
    [TOROLT]          CHAR (1) NULL,
    [SERIAL]          INT      NULL,
    [LASTCHANGED]     DATETIME NULL,
    [CREATED]         DATETIME NULL,
    [MODIFIER]        INT      NULL,
    [CREATOR]         INT      NULL,
    [NNID]            INT      NULL,
    CONSTRAINT [PK_T_4027549] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);


GO
PRINT N'Creating [dbo].[T_AGAZATTIPUS].[I_T_402754902_402754900]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402754902_402754900]
    ON [dbo].[T_AGAZATTIPUS]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_AGAZATTIPUS].[I_T_402754905_402754903]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402754905_402754903]
    ON [dbo].[T_AGAZATTIPUS]([C_ALTANEVID] ASC);


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


GO
CREATE TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS] (
    [ID]              INT      NOT NULL,
    [C_ALINTEZMENYID] INT      NOT NULL,
    [C_ALTANEVID]     INT      NOT NULL,
    [TOROLT]          CHAR (1) NULL,
    [SERIAL]          INT      NULL,
    [LASTCHANGED]     DATETIME NULL,
    [CREATED]         DATETIME NULL,
    [MODIFIER]        INT      NULL,
    [CREATOR]         INT      NULL,
    [NNID]            INT      NULL,
    CONSTRAINT [PK_T_4027550] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);


GO
PRINT N'Creating [dbo].[T_RESZSZAKKEPESITESTIPUS].[I_T_402755002_402755000]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402755002_402755000]
    ON [dbo].[T_RESZSZAKKEPESITESTIPUS]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_RESZSZAKKEPESITESTIPUS].[I_T_402755005_402755003]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402755005_402755003]
    ON [dbo].[T_RESZSZAKKEPESITESTIPUS]([C_ALTANEVID] ASC);


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


GO
CREATE TABLE [dbo].[T_SZAKKEPESITESTIPUS] (
    [ID]              INT      NOT NULL,
    [C_ALINTEZMENYID] INT      NOT NULL,
    [C_ALTANEVID]     INT      NOT NULL,
    [TOROLT]          CHAR (1) NULL,
    [SERIAL]          INT      NULL,
    [LASTCHANGED]     DATETIME NULL,
    [CREATED]         DATETIME NULL,
    [MODIFIER]        INT      NULL,
    [CREATOR]         INT      NULL,
    [NNID]            INT      NULL,
    CONSTRAINT [PK_T_4027539] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);


GO
PRINT N'Creating [dbo].[T_SZAKKEPESITESTIPUS].[I_T_402753902_402753900]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402753902_402753900]
    ON [dbo].[T_SZAKKEPESITESTIPUS]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_SZAKKEPESITESTIPUS].[I_T_402753905_402753903]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402753905_402753903]
    ON [dbo].[T_SZAKKEPESITESTIPUS]([C_ALTANEVID] ASC);


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


GO
CREATE TABLE [dbo].[T_SZAKMACSOPORTTIPUS] (
    [ID]              INT      NOT NULL,
    [C_ALINTEZMENYID] INT      NOT NULL,
    [C_ALTANEVID]     INT      NOT NULL,
    [TOROLT]          CHAR (1) NULL,
    [SERIAL]          INT      NULL,
    [LASTCHANGED]     DATETIME NULL,
    [CREATED]         DATETIME NULL,
    [MODIFIER]        INT      NULL,
    [CREATOR]         INT      NULL,
    [NNID]            INT      NULL,
    CONSTRAINT [PK_T_4027548] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);


GO
PRINT N'Creating [dbo].[T_SZAKMACSOPORTTIPUS].[I_T_402754802_402754800]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402754802_402754800]
    ON [dbo].[T_SZAKMACSOPORTTIPUS]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_SZAKMACSOPORTTIPUS].[I_T_402754805_402754803]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402754805_402754803]
    ON [dbo].[T_SZAKMACSOPORTTIPUS]([C_ALTANEVID] ASC);


GO
PRINT N'Creating unnamed constraint on [dbo].[T_AGAZATTIPUS]...';


GO
ALTER TABLE [dbo].[T_AGAZATTIPUS]
    ADD DEFAULT ('F') FOR [TOROLT];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_AGAZATTIPUS]...';


GO
ALTER TABLE [dbo].[T_AGAZATTIPUS]
    ADD DEFAULT (getdate()) FOR [LASTCHANGED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_AGAZATTIPUS]...';


GO
ALTER TABLE [dbo].[T_AGAZATTIPUS]
    ADD DEFAULT (getdate()) FOR [CREATED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_RESZSZAKKEPESITESTIPUS]...';


GO
ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS]
    ADD DEFAULT ('F') FOR [TOROLT];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_RESZSZAKKEPESITESTIPUS]...';


GO
ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS]
    ADD DEFAULT (getdate()) FOR [LASTCHANGED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_RESZSZAKKEPESITESTIPUS]...';


GO
ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS]
    ADD DEFAULT (getdate()) FOR [CREATED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_SZAKKEPESITESTIPUS]...';


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS]
    ADD DEFAULT ('F') FOR [TOROLT];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_SZAKKEPESITESTIPUS]...';


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS]
    ADD DEFAULT (getdate()) FOR [LASTCHANGED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_SZAKKEPESITESTIPUS]...';


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS]
    ADD DEFAULT (getdate()) FOR [CREATED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_SZAKMACSOPORTTIPUS]...';


GO
ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS]
    ADD DEFAULT ('F') FOR [TOROLT];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_SZAKMACSOPORTTIPUS]...';


GO
ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS]
    ADD DEFAULT (getdate()) FOR [LASTCHANGED];


GO
PRINT N'Creating unnamed constraint on [dbo].[T_SZAKMACSOPORTTIPUS]...';


GO
ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS]
    ADD DEFAULT (getdate()) FOR [CREATED];


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


GO
ALTER TABLE [dbo].[T_AGAZATTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_T_21038_4027554] FOREIGN KEY ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_DICTIONARYITEMBASE] ([ID], [C_INTEZMENYID], [C_TANEVID]);


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


GO
ALTER TABLE [dbo].[T_AGAZATTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402754902_402754900] FOREIGN KEY ([C_ALINTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_AGAZATTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402754905_402754903] FOREIGN KEY ([C_ALTANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO
ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402755005_402755003] FOREIGN KEY ([C_ALTANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO
ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_T_21038_4027552] FOREIGN KEY ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_DICTIONARYITEMBASE] ([ID], [C_INTEZMENYID], [C_TANEVID]);


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


GO
ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402755002_402755000] FOREIGN KEY ([C_ALINTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_T_21038_4027551] FOREIGN KEY ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_DICTIONARYITEMBASE] ([ID], [C_INTEZMENYID], [C_TANEVID]);


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


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402753902_402753900] FOREIGN KEY ([C_ALINTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402753905_402753903] FOREIGN KEY ([C_ALTANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO
ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_T_21038_4027553] FOREIGN KEY ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_DICTIONARYITEMBASE] ([ID], [C_INTEZMENYID], [C_TANEVID]);


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


GO
ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402754802_402754800] FOREIGN KEY ([C_ALINTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_402754805_402754803] FOREIGN KEY ([C_ALTANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO

  CREATE TRIGGER tr_agazattipusLog ON T_AGAZATTIPUS 
  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_ALINTEZMENYID AS C_INTEZMENYID, C_ALTANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (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_ALINTEZMENYID AS C_INTEZMENYID, C_ALTANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (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_AGAZATTIPUS', '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_AGAZATTIPUS', '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_AGAZATTIPUS', 
        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_reszszakkepesitestipusLog]...';


GO

  CREATE TRIGGER tr_reszszakkepesitestipusLog ON T_RESZSZAKKEPESITESTIPUS 
  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_ALINTEZMENYID AS C_INTEZMENYID, C_ALTANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (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_ALINTEZMENYID AS C_INTEZMENYID, C_ALTANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (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_RESZSZAKKEPESITESTIPUS', '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_RESZSZAKKEPESITESTIPUS', '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_RESZSZAKKEPESITESTIPUS', 
        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_szakkepesitestipusLog]...';


GO

  CREATE TRIGGER tr_szakkepesitestipusLog ON T_SZAKKEPESITESTIPUS 
  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_ALINTEZMENYID AS C_INTEZMENYID, C_ALTANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (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_ALINTEZMENYID AS C_INTEZMENYID, C_ALTANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (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_SZAKKEPESITESTIPUS', '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_SZAKKEPESITESTIPUS', '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_SZAKKEPESITESTIPUS', 
        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_szakmacsoporttipusLog]...';


GO

  CREATE TRIGGER tr_szakmacsoporttipusLog ON T_SZAKMACSOPORTTIPUS 
  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_ALINTEZMENYID AS C_INTEZMENYID, C_ALTANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM INSERTED) p
       UNPIVOT (val FOR col IN (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_ALINTEZMENYID AS C_INTEZMENYID, C_ALTANEVID AS C_TANEVID, CREATOR, MODIFIER, CAST( TOROLT AS nvarchar(MAX)) AS TOROLT, CAST( NNID AS nvarchar(MAX)) AS NNID
       FROM DELETED) p
       UNPIVOT (val FOR col IN (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_SZAKMACSOPORTTIPUS', '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_SZAKMACSOPORTTIPUS', '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_SZAKMACSOPORTTIPUS', 
        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'Checking existing data against newly created constraints';


GO
ALTER TABLE [dbo].[T_AGAZATTIPUS] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4027554];

ALTER TABLE [dbo].[T_AGAZATTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402754902_402754900];

ALTER TABLE [dbo].[T_AGAZATTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402754905_402754903];

ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402755005_402755003];

ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4027552];

ALTER TABLE [dbo].[T_RESZSZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402755002_402755000];

ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4027551];

ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402753902_402753900];

ALTER TABLE [dbo].[T_SZAKKEPESITESTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402753905_402753903];

ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS] WITH CHECK CHECK CONSTRAINT [FK_T_21038_4027553];

ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402754802_402754800];

ALTER TABLE [dbo].[T_SZAKMACSOPORTTIPUS] WITH CHECK CHECK CONSTRAINT [FK_402754805_402754803];


GO
PRINT N'Update complete.';


GO

