﻿DROP PROCEDURE IF EXISTS dev.uspCloneIntezmenyMigrationList
GO

CREATE PROCEDURE dev.uspCloneIntezmenyMigrationList
   @pSourceIntezmenyId nvarchar(255)
  ,@pSourceDatabase nvarchar(60)
  ,@pEntityHistoryMigration bit = 1
  ,@pChangeIds bit = 1
  ,@pDebugMode bit = 0
AS
BEGIN
  DECLARE 
     @tableName nvarchar(50)
    ,@intezmenyIdColumn nvarchar(50)
    
    ,@isIdentity bit
    ,@columnList nvarchar(max)
    ,@sql nvarchar(max)
    ,@newIntezmenyId int
    ,@fenntartoAzonosito int
    ,@firstParentCol nvarchar(50)
    ,@firstRefObj nvarchar(50)
    ,@secondParentCol nvarchar(50)
    ,@secondRefObj nvarchar(50)
    ,@message nvarchar(max)
    ,@rowCount int
  

  RAISERROR ('DATA MIGRATION... ', 10, 1) WITH NOWAIT

  SELECT @newIntezmenyId = t1.[newId] FROM MappingTable t1 WHERE t1.tableName = 'T_INTEZMENY'
  SELECT @fenntartoAzonosito = '000'

  IF NOT EXISTS (SELECT 1 FROM T_FENNTARTO WHERE ID = 0) BEGIN
  SET IDENTITY_INSERT T_FENNTARTO ON
  	INSERT INTO T_FENNTARTO
  	(ID, C_NEV, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, ELOZOTANEVIREKORDID, NNID) VALUES
  	(0, N'Technikai', 'F', 0, '2018-01-18 00:22:30', '2018-01-18 00:22:30', NULL, NULL, NULL, NULL)
  SET IDENTITY_INSERT T_FENNTARTO OFF
  END

  -- ============================================================== --
  -- Az IDENTITY()-s táblák adatainak áttöltése                     --
  -- ============================================================== --
  DECLARE tbl_cur CURSOR LOCAL FOR
    SELECT DISTINCT TABLE_NAME, COLUMN_NAME--, COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), 'ID', 'IsIdentity')
    FROM INFORMATION_SCHEMA.COLUMNS c
    WHERE COLUMN_NAME = N'C_INTEZMENYID'
      AND TABLE_NAME IN (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES  WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'dbo')
      AND c.TABLE_SCHEMA = 'dbo'
      AND TABLE_NAME NOT IN (N'T_ENTITYATTRIBUTEHISTORY', N'T_DICTIONARYTYPE', 'T_ENTITYHISTORY')
      AND COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), 'ID', 'IsIdentity') = 1
    ORDER BY TABLE_NAME

  OPEN tbl_cur
  FETCH NEXT FROM tbl_cur
  INTO @tableName, @intezmenyIdColumn

  WHILE @@FETCH_STATUS = 0 BEGIN
    --PRINT @tableName
    SELECT @columnList = ISNULL(@columnList + ', ', '') + c.COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS c
    WHERE c.TABLE_NAME = @tableName AND c.TABLE_SCHEMA = 'dbo' AND c.COLUMN_NAME NOT IN (N'ID', @intezmenyIdColumn)
      
    SET @sql = N'
      INSERT INTO ' + @tableName + ' (ID, ' + @intezmenyIdColumn + ', ' + @columnList + ')
      SELECT ' + IIF(@pChangeIds = 1, 't.[newId], @newIntezmenyId', 'ID, ' + @intezmenyIdColumn) + ', ' +  @columnList + '
      FROM ' + @pSourceDatabase + '.dbo.' + @tableName + ' x
        ' + IIF(@pChangeIds = 1, 'INNER JOIN MappingTable t ON t.oldId = x.ID AND t.tableName = ''' + @tableName + '''', '') + '
      WHERE ' + @intezmenyIdColumn + ' IN (' + @pSourceIntezmenyId + ')'
    

      SET @sql = N'
      SET IDENTITY_INSERT ' + @tableName + ' ON' + @sql + '
      
      SET @rowCount = @@ROWCOUNT
      SET IDENTITY_INSERT ' + @tableName + ' OFF
      '
    
    IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
    EXEC sp_executesql @sql, N'@intId nvarchar(255), @newIntezmenyId int, @rowCount int output', @pSourceIntezmenyId, @newIntezmenyId, @rowCount output
    SET @message = '    CPA ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - ' + @tableName + ' - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
    RAISERROR (@message, 10, 1) WITH NOWAIT 
    
    SET @columnList = NULL
    
    FETCH NEXT FROM tbl_cur
    INTO @tableName, @intezmenyIdColumn
  END

  CLOSE tbl_cur
  DEALLOCATE tbl_cur


  -- ============================================================== --
  -- Az Intézmény adatainak áttöltése                               --
  -- ============================================================== --
  SET @sql = '
    SET IDENTITY_INSERT T_INTEZMENY ON
    INSERT INTO T_INTEZMENY (ID, C_AZONOSITO, C_FENNTARTOAZONOSITO, C_NEPTUNNAPLOLINK, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, C_FENNTARTOID)
    SELECT ' + IIF(@pChangeIds = 1, '@newIntezmenyId','ID') + ', C_AZONOSITO, C_FENNTARTOAZONOSITO, C_NEPTUNNAPLOLINK, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, C_FENNTARTOID
    FROM ' + @pSourceDatabase + '.dbo.T_INTEZMENY x
    WHERE ID IN (' + @pSourceIntezmenyId + ')
    
    SET @rowCount = @@ROWCOUNT

    SET IDENTITY_INSERT T_INTEZMENY OFF              
  '

  IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
  EXEC sp_executesql @sql, N'@newIntezmenyId int, @intId nvarchar(255), @rowCount int output', @newIntezmenyId, @pSourceIntezmenyId, @rowCount output
  SET @message = '    CPB ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_INTEZMENY - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
  RAISERROR (@message, 10, 1) WITH NOWAIT 

  -- ============================================================== --
  -- A nem IDENTITY()-s táblák adatainak áttöltése                  --
  -- ============================================================== --    
  DECLARE tbl_cur CURSOR LOCAL FOR
    SELECT DISTINCT TABLE_NAME, COLUMN_NAME--, COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), 'ID', 'IsIdentity')
    FROM INFORMATION_SCHEMA.COLUMNS c
    WHERE COLUMN_NAME = N'C_ALINTEZMENYID'
      AND TABLE_NAME IN (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES  WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'dbo')
      AND TABLE_NAME NOT IN (N'T_ENTITYATTRIBUTEHISTORY', N'T_ENTITYHISTORY', N'T_DICTIONARYTYPE')    
      AND TABLE_SCHEMA = 'dbo'
      AND COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), 'ID', 'IsIdentity') = 0
    ORDER BY TABLE_NAME
    
  OPEN tbl_cur
  FETCH NEXT FROM tbl_cur
  INTO @tableName, @intezmenyIdColumn

  WHILE @@FETCH_STATUS = 0 BEGIN
    
    --PRINT @tableName
    
    SELECT @columnList = ISNULL(@columnList + ', ', '') + c.COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS c
    WHERE c.TABLE_NAME = @tableName AND c.TABLE_SCHEMA = 'dbo' AND c.COLUMN_NAME NOT IN (N'ID', @intezmenyIdColumn)
      
    SET @sql = N'
      INSERT INTO ' + @tableName + ' (ID, ' + @intezmenyIdColumn + ', ' + @columnList + ')
      SELECT ' + IIF(@pChangeIds = 1, 't.[newId], @newIntezmenyId', 'ID, ' + @intezmenyIdColumn) + ', ' +  @columnList + '
      FROM ' + @pSourceDatabase + '.dbo.' + @tableName + ' x
        ' + IIF(@pChangeIds = 1, 'INNER JOIN MappingTable t ON t.oldId = x.ID AND t.tableName = ''' + @tableName + '''', '') + '
      WHERE ' + @intezmenyIdColumn + ' IN (' + @pSourceIntezmenyId + ')

      SET @rowCount = @@ROWCOUNT
      '
    
    IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
    EXEC sp_executesql @sql, N'@intId nvarchar(255), @newIntezmenyId int, @rowCount int output', @pSourceIntezmenyId, @newIntezmenyId, @rowCount output
    SET @message = '    CPC ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - ' + @tableName + ' - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
    RAISERROR (@message, 10, 1) WITH NOWAIT 

    SET @columnList = NULL
    
    FETCH NEXT FROM tbl_cur
    INTO @tableName, @intezmenyIdColumn
  END
    
  CLOSE tbl_cur
  DEALLOCATE tbl_cur

  -- ================================================================================ --
  --  Kapcsolótáblák migrálása, ezt külön kell, mert nincsen nekik C_INTEZMENYID-juk  --
  -- ================================================================================ --
  DECLARE tbl_cur CURSOR LOCAL FOR
    SELECT TABLE_NAME 
    FROM INFORMATION_SCHEMA.TABLES t
    WHERE t.TABLE_NAME NOT IN (
        SELECT TABLE_NAME 
        FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE COLUMN_NAME = 'ID'
      ) 
      AND t.TABLE_TYPE = 'BASE TABLE' 
      AND t.TABLE_NAME LIKE 'T[_]%' 
      AND t.TABLE_SCHEMA = 'dbo'
      AND t.TABLE_NAME NOT IN ('T_EVFOLYAMTIPUS_OKTATASINEVELE', 'T_AMIFOTARGY_AMIALTARGY') 
    ORDER BY TABLE_NAME

  OPEN tbl_cur
  FETCH NEXT FROM tbl_cur
  INTO @tableName

  WHILE @@FETCH_STATUS = 0 BEGIN
    SELECT TOP(1) 
       @firstParentCol = pc.[name]-- AS parent_column_name
      ,@firstRefObj = OBJECT_NAME(rc.[object_id]) --as referenced_object_name
    FROM sys.foreign_key_columns f 
      INNER JOIN sys.columns pc ON pc.[object_id] = f.parent_object_id AND pc.column_id = f.parent_column_id
      INNER JOIN sys.columns rc ON rc.[object_id] = f.referenced_object_id AND rc.column_id = f.referenced_column_id
    WHERE parent_object_id = OBJECT_ID(@tableName)
    ORDER BY f.constraint_object_id ASC

    SELECT TOP(1)
       @secondParentCol = pc.[name]-- AS parent_column_name
      ,@secondRefObj = OBJECT_NAME(rc.[object_id]) --as referenced_object_name
    FROM sys.foreign_key_columns f 
      INNER JOIN sys.columns pc ON pc.[object_id] = f.parent_object_id AND pc.column_id = f.parent_column_id
      INNER JOIN sys.columns rc ON rc.[object_id] = f.referenced_object_id AND rc.column_id = f.referenced_column_id
    WHERE parent_object_id = OBJECT_ID(@tableName)
    ORDER BY f.constraint_object_id DESC

    SET @sql = N'
      INSERT INTO ' + @tableName + ' (' + @firstParentCol + ', ' + @secondParentCol + ')
      SELECT ' + IIF(@pChangeIds = 1, 'DISTINCT t1.[newId] AS ' + @firstParentCol + ', t2.[newId] AS ' + @secondParentCol, @firstParentCol + ', ' + @secondParentCol) + '
      FROM ' + @pSourceDatabase + '.dbo.' + @tableName + ' x
        ' + IIF(@pChangeIds = 1, 'INNER JOIN MappingTable t1 ON t1.tableName = ''' + @firstRefObj  + ''' AND ' + @firstParentCol + ' = t1.oldId', '
        INNER JOIN ' + @pSourceDatabase + '.dbo.' + @firstRefObj + ' s ON s.ID  = x.' + @firstParentCol + ' AND s.C_' + IIF(@firstRefObj IN ('T_TANULO', 'T_ALKALMAZOTT', 'T_OSZTALY', 'T_CSOPORT'), 'AL', '') + 'INTEZMENYID IN (' + @pSourceIntezmenyId + ')') + '
        ' + IIF(@pChangeIds = 1, 'INNER JOIN MappingTable t2 ON t2.tableName = ''' + @secondRefObj + ''' AND ' + @secondParentCol + ' = t2.oldId', '
        INNER JOIN ' + @pSourceDatabase + '.dbo.' + @secondRefObj + ' z ON z.ID  = x.' + @secondParentCol + ' AND z.C_' + IIF(@secondRefObj IN ('T_TANULO', 'T_ALKALMAZOTT', 'T_OSZTALY', 'T_CSOPORT'), 'AL', '') + 'INTEZMENYID IN (' + @pSourceIntezmenyId + ')') + '
        
      SET @rowCount = @@ROWCOUNT
      '
    
    IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
    EXEC sp_executesql @sql, N'@intId nvarchar(255), @rowCount int output', @pSourceIntezmenyId, @rowCount output
    SET @message = '    CPD ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - ' + @tableName + ' - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
    RAISERROR (@message, 10, 1) WITH NOWAIT 
    
    FETCH NEXT FROM tbl_cur
    INTO @tableName
  END

  CLOSE tbl_cur
  DEALLOCATE tbl_cur

  -- ============================================================== --
  -- A T_DICTIONARYITEMBASE altáblák miglálása (100k alatt)         --
  -- ============================================================== --
  IF @pChangeIds = 1 BEGIN

    DECLARE tbl_cur CURSOR LOCAL FOR
      SELECT OBJECT_NAME(parent_object_id) 
      FROM sys.foreign_keys 
      WHERE referenced_object_id = OBJECT_ID('T_DICTIONARYITEMBASE')
    
    --SELECT @newIntezmenyId = t1.[newId] FROM MappingTable t1 WHERE t1.tableName = 'T_INTEZMENY'

    OPEN tbl_cur
    FETCH NEXT FROM tbl_cur
    INTO @tableName

    WHILE @@FETCH_STATUS = 0 BEGIN
      SELECT @columnList = ISNULL(@columnList + ', ', '') + c.COLUMN_NAME
      FROM INFORMATION_SCHEMA.COLUMNS c
      WHERE c.TABLE_NAME = @tableName AND c.TABLE_SCHEMA = 'dbo' AND c.COLUMN_NAME NOT IN (N'ID', N'C_ALINTEZMENYID')
      ORDER BY TABLE_NAME

      SET @sql = N'
        INSERT INTO ' + @tableName + ' (ID, C_ALINTEZMENYID, ' + @columnList + ')
        SELECT ID, ' + IIF(@pChangeIds = 1, '@newIntezmenyId', 'C_ALINTEZMENYID') + ', ' +  @columnList + '
        FROM ' + @pSourceDatabase + '.dbo.' + @tableName + ' x
        WHERE C_ALINTEZMENYID IN (' + @pSourceIntezmenyId + ') AND ID < 100000
        
        SET @rowCount = @@ROWCOUNT
        '
    
      IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
      EXEC sp_executesql @sql, N'@intId nvarchar(255), @newIntezmenyId int, @rowCount int output', @pSourceIntezmenyId, @newIntezmenyId, @rowCount output
      SET @message = '    CPE ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - ' + @tableName + ' - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
      RAISERROR (@message, 10, 1) WITH NOWAIT 
    
      SET @columnList = NULL
      FETCH NEXT FROM tbl_cur
      INTO @tableName
    END
    
    CLOSE tbl_cur
    DEALLOCATE tbl_cur

    -- ============================================================== --
    -- A T_DICTIONARYITEMBASE miglálása (100k alatt)                  --
    -- ============================================================== --
    SELECT @newIntezmenyId = t1.[newId] 
    FROM MappingTable t1 
    WHERE t1.tableName = 'T_INTEZMENY'

    SET @sql = '
    SET IDENTITY_INSERT T_DICTIONARYITEMBASE ON
      INSERT INTO T_DICTIONARYITEMBASE (ID, C_VALUE, C_NAME, C_NAME_1, C_NAME_2, C_NAME_3, C_NAME_4, C_VISIBLE, C_TYPE, C_PROTECTED, C_INTEZMENYID, C_TANEVID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, C_ORDER, C_DICTIONARYTYPEID)
      SELECT ID, C_VALUE, C_NAME, C_NAME_1, C_NAME_2, C_NAME_3, C_NAME_4, C_VISIBLE, C_TYPE, C_PROTECTED, ' + IIF(@pChangeIds = 1, '@newIntezmenyId', 'C_ALINTEZMENYID') + ', C_TANEVID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, C_ORDER, C_DICTIONARYTYPEID
      FROM ' + @pSourceDatabase + '.dbo.T_DICTIONARYITEMBASE x
      WHERE C_INTEZMENYID =IN (' + @pSourceIntezmenyId + ') AND ID < 100000

      SET @rowCount = @@ROWCOUNT
    
    SET IDENTITY_INSERT T_DICTIONARYITEMBASE OFF
    
    
    '

    IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
    EXEC sp_executesql @sql, N'@IntId nvarchar(255), @newIntezmenyId int, @rowCount int output', @pSourceIntezmenyId, @newIntezmenyId, @rowCount output
    SET @message = '    CPF ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_DICTIONARYITEMBASE - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
    RAISERROR (@message, 10, 1) WITH NOWAIT 


  END

  -- ============================================================== --
  -- DICTIONARYTYPE külön kell áttölteni                            --
  -- ============================================================== --
      
  SET @sql = N'
    SET IDENTITY_INSERT T_DICTIONARYTYPE ON
      INSERT INTO T_DICTIONARYTYPE (ID, C_CODENAME, C_DESCRIPTION, C_ISCOLOREDITABLE, C_ISCUSTOMITEMALLOWED, C_NAME, C_INTEZMENYID, C_TANEVID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, ELOZOTANEVIREKORDID, NNID)
      SELECT ID, C_CODENAME, C_DESCRIPTION, C_ISCOLOREDITABLE, C_ISCUSTOMITEMALLOWED, C_NAME, ' + IIF(@pChangeIds = 1, '@newIntezmenyId', 'C_INTEZMENYID') + ', C_TANEVID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, ELOZOTANEVIREKORDID, NNID
      FROM ' + @pSourceDatabase + '.dbo.T_DICTIONARYTYPE
      WHERE C_INTEZMENYID IN (' + @pSourceIntezmenyId + ')
    
      SET @rowCount = @@ROWCOUNT 
    SET IDENTITY_INSERT T_DICTIONARYTYPE OFF
       
    '
 
  IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT

  EXEC sp_executesql @sql, N'@intId nvarchar(255), @newIntezmenyId int, @rowCount int output', @pSourceIntezmenyId, @newIntezmenyId, @rowCount output
  SET @message = '    CPG ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_DICTIONARYTYPE - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
  RAISERROR (@message, 10, 1) WITH NOWAIT 

  -- ================================================================================ --
  --  T_EVFOLYAMTIPUS_OKTATASINEVELE táblát külön kell kezelni                        --
  -- ================================================================================ --
  SET @sql = '
    INSERT INTO T_EVFOLYAMTIPUS_OKTATASINEVELE (C_EVFOLYAMTIPUSID, C_OKTATASINEVELESIFELADATID, C_INTEZMENYID, C_TANEVID)
    SELECT ' + IIF(@pChangeIds = 1, 'ISNULL(eft.[newId], eo.C_EVFOLYAMTIPUSID), ISNULL(onf.[newId], eo.C_OKTATASINEVELESIFELADATID), @newIntezmenyId, t.[newId]', 'C_EVFOLYAMTIPUSID, C_OKTATASINEVELESIFELADATID, C_INTEZMENYID, C_TANEVID') + ' 
    FROM ' + @pSourceDatabase + '.dbo.T_EVFOLYAMTIPUS_OKTATASINEVELE eo
      ' + IIF(@pChangeIds = 1, 'INNER JOIN MappingTable t ON eo.C_TANEVID = t.oldId AND t.tableName = ''T_TANEV''
      LEFT JOIN MappingTable onf ON eo.C_OKTATASINEVELESIFELADATID = onf.oldId AND onf.tableName = ''T_OKTATASINEVELESIFELADAT''
      LEFT JOIN MappingTable eft ON eo.C_EVFOLYAMTIPUSID = eft.oldId AND eft.tableName = ''T_EVFOLYAMTIPUS''', '') + '
    WHERE eo.C_INTEZMENYID IN (' + @pSourceIntezmenyId + ')
    
    SET @rowCount = @@ROWCOUNT
    '
  
  IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
  EXEC sp_executesql @sql, N'@intId nvarchar(255), @newIntezmenyId int, @rowCount int output', @pSourceIntezmenyId, @newIntezmenyId, @rowCount output
  SET @message = '    CPH ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_EVFOLYAMTIPUS_OKTATASINEVELE - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
  RAISERROR (@message, 10, 1) WITH NOWAIT 

  -- ================================================================================ --
  --  DASHBOARDUZENETEK áttöltése                                                     --
  -- ================================================================================ --
  IF @pChangeIds = 0 BEGIN
    SET @sql = N'
      INSERT INTO T_DASHBOARDUZENET (C_CIM, C_EGYEDIAZONOSITO, C_ERVENYESSEGKEZDETE, C_ERVENYESSEGVEGE, C_KIEMELTIDOSZAKVEGE, C_SORREND, C_TARTALOM, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, ELOZOTANEVIREKORDID, NNID, C_DASHBOARDUZENETKATEGORIAID, C_ISPOPUP, C_POPUPGYAKORISAGAPERCBEN, C_ISADMIN, C_ISTANAR)
      SELECT C_CIM, C_EGYEDIAZONOSITO, C_ERVENYESSEGKEZDETE, C_ERVENYESSEGVEGE, C_KIEMELTIDOSZAKVEGE, C_SORREND, C_TARTALOM, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR, ELOZOTANEVIREKORDID, NNID, C_DASHBOARDUZENETKATEGORIAID, C_ISPOPUP, C_POPUPGYAKORISAGAPERCBEN, C_ISADMIN, C_ISTANAR
      FROM ' + @pSourceDatabase + '.dbo.T_DASHBOARDUZENET d
      WHERE NOT EXISTS (SELECT 1 FROM T_DASHBOARDUZENET x WHERE d.C_EGYEDIAZONOSITO = x.C_EGYEDIAZONOSITO)
      
      SET @rowCount = @@ROWCOUNT
      '

    IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
    EXEC sp_executesql @sql, N'@rowCount int output', @rowCount output
    SET @message = '    CPI ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_DASHBOARDUZENET - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
    RAISERROR (@message, 10, 1) WITH NOWAIT 
  END

  -- ================================================================================ --
  --  Az ENTITY HISTORY áttöltése                                                     --
  -- ================================================================================ --
  IF @pEntityHistoryMigration = 1 BEGIN
    SET @sql = N'
    SET IDENTITY_INSERT T_ENTITYHISTORY ON 
      INSERT INTO T_ENTITYHISTORY (ID, C_ALTERATIONDATE, C_ENTITYID, C_ENTITYNAME, C_REASON, C_FELHASZNALOID, C_INTEZMENYID, C_TANEVID, CREATED)
      SELECT ' + IIF(@pChangeIds = 1, 't.[newId]', 'ID') + ', x.C_ALTERATIONDATE, x.C_ENTITYID, x.C_ENTITYNAME, x.C_REASON, x.C_FELHASZNALOID, ' + IIF(@pChangeIds = 1, '@newIntezmenyId', 'C_INTEZMENYID') + ', x.C_TANEVID, x.CREATED
      FROM ' + @pSourceDatabase + '.dbo.T_ENTITYHISTORY x
      ' + IIF(@pChangeIds = 1, 'INNER JOIN MappingTable t ON t.oldId = x.ID AND t.tableName = ''T_ENTITYHISTORY''', '') + '
      WHERE x.C_INTEZMENYID IN (' + @pSourceIntezmenyId + ')
    
      SET @rowCount = @@ROWCOUNT
    SET IDENTITY_INSERT T_ENTITYHISTORY OFF
    
    
    '

    IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
    EXEC sp_executesql @sql, N'@IntId nvarchar(255), @newIntezmenyId int, @rowCount int output', @pSourceIntezmenyId, @newIntezmenyId, @rowCount output
    SET @message = '    CPJ ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYHISTORY - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
    RAISERROR (@message, 10, 1) WITH NOWAIT 

    
    SET @sql = N'
    ' + IIF(@pChangeIds = 1, '', 'SET IDENTITY_INSERT T_ENTITYATTRIBUTEHISTORY ON') + '
    INSERT INTO T_ENTITYATTRIBUTEHISTORY (' + IIF(@pChangeIds = 1, '', 'ID, ') + 'C_CURRENTVALUE, C_ORIGINALVALUE, C_PROPERTYNAME, C_ENTITYHISTORYID)
    SELECT ' + IIF(@pChangeIds = 1, '', 'e.ID, ') + 'C_CURRENTVALUE, C_ORIGINALVALUE, C_PROPERTYNAME, ' + IIF(@pChangeIds = 1, 't.[newId]', 'C_ENTITYHISTORYID') + '
    FROM ' + @pSourceDatabase + '.dbo.T_ENTITYATTRIBUTEHISTORY e
    ' + IIF(@pChangeIds = 1, 
        'INNER JOIN MappingTable t ON e.C_ENTITYHISTORYID = t.oldId AND t.tableName = ''T_ENTITYHISTORY''', 
        'INNER JOIN ' + @pSourceDatabase + '.dbo.T_ENTITYHISTORY x ON x.ID  = e.C_ENTITYHISTORYID AND x.C_INTEZMENYID IN (' + @pSourceIntezmenyId + ')') + ';
    SET @rowCount = @@ROWCOUNT
    ' + IIF(@pChangeIds = 1, '', 'SET IDENTITY_INSERT T_ENTITYATTRIBUTEHISTORY OFF') + '
    '

    IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
    EXEC sp_executesql @sql, N'@IntId nvarchar(255), @newIntezmenyId int, @rowCount int output', @pSourceIntezmenyId, @newIntezmenyId, @rowCount output
    SET @message = '    CPK ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYATTRIBUTEHISTORY - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
    RAISERROR (@message, 10, 1) WITH NOWAIT 
  END

END
GO

