﻿SELECT id, c_type, id + 120000 AS idNew
INTO dev.CUSTOMDICT
FROM T_DICTIONARYITEMBASE d
WHERE id < 100000
GROUP BY id, c_type
HAVING count(1) < 2
ORDER BY c_type

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_CODE
  ,C_TYPE
  ,C_PROTECTED
  ,C_COLOR
  ,C_ORDER
  ,C_INTEZMENYID
  ,C_TANEVID
  ,TOROLT
  ,SERIAL
  ,LASTCHANGED
  ,CREATED
  ,MODIFIER
  ,CREATOR
  ,ELOZOTANEVIREKORDID
  ,NNID
) SELECT
   d.ID + 120000
  ,C_VALUE
  ,'€' + C_NAME
  ,C_NAME_1
  ,C_NAME_2
  ,C_NAME_3
  ,C_NAME_4
  ,C_VISIBLE
  ,C_CODE
  ,d.C_TYPE
  ,C_PROTECTED
  ,C_COLOR
  ,C_ORDER
  ,C_INTEZMENYID
  ,C_TANEVID
  ,TOROLT
  ,SERIAL
  ,LASTCHANGED
  ,CREATED
  ,MODIFIER
  ,CREATOR
  ,ELOZOTANEVIREKORDID
  ,NNID
FROM T_DICTIONARYITEMBASE d
  INNER JOIN dev.CUSTOMDICT c ON d.ID = c.id AND d.C_TYPE = c.c_type

SET IDENTITY_INSERT T_DICTIONARYITEMBASE OFF
GO

DECLARE tbl_cursor CURSOR FOR 
  SELECT DISTINCT 'T_' + UPPER(C_TYPE)
  FROM dev.CUSTOMDICT

DECLARE @tableName nvarchar(80),
  @sql nvarchar(MAX)

OPEN tbl_cursor
FETCH NEXT FROM tbl_cursor INTO @tableName 
WHILE @@FETCH_STATUS = 0 BEGIN
	SET @sql = '
    INSERT INTO ' + @tableName + ' (
       ID
      ,C_ALINTEZMENYID
      ,C_ALTANEVID
      ,TOROLT
      ,SERIAL
      ,LASTCHANGED
      ,CREATED
      ,MODIFIER
      ,CREATOR
      ,NNID
      ,ELOZOTANEVIREKORDIDA
      ,ELOZOTANEVIREKORDID
    ) SELECT 
       ID + 120000
      ,C_ALINTEZMENYID
      ,C_ALTANEVID
      ,TOROLT
      ,SERIAL
      ,LASTCHANGED
      ,CREATED
      ,MODIFIER
      ,CREATOR
      ,NNID
      ,ELOZOTANEVIREKORDIDA
      ,ELOZOTANEVIREKORDID
    FROM ' + @tableName + '
    WHERE ID IN (SELECT ID FROM dev.CUSTOMDICT WHERE C_TYPE = SUBSTRING(''' + @tableName + ''', 3, 255))'

  EXEC sys.sp_executesql @sql 
  
  FETCH NEXT FROM tbl_cursor INTO @tableName 
END

CLOSE tbl_cursor
DEALLOCATE tbl_cursor
GO


DECLARE upd_cursor CURSOR FOR 
SELECT 
   OBJECT_NAME(pc.[object_id]) AS parent_object_name
  ,pc.[name] AS parent_column_name
  ,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 OBJECT_NAME(rc.[object_id]) IN (SELECT 'T_' + C_TYPE FROM dev.CUSTOMDICT)
  AND pc.[name] NOT IN (N'C_ALTANEVID', N'C_TANEVID', N'C_ALINTEZMENYID', N'C_INTEZMENYID')
  AND OBJECT_NAME(pc.[object_id]) <> 'T_EVFOLYAMTIPUS_OKTATASINEVELE'
  
DECLARE @pObj nvarchar(255), @pCol nvarchar(255), @rObj nvarchar(255), @sql nvarchar(max)

OPEN upd_cursor
FETCH NEXT FROM upd_cursor INTO @pObj, @pCol, @rObj
WHILE @@FETCH_STATUS = 0
BEGIN
	SET @sql = '
    DISABLE TRIGGER tr_' + SUBSTRING(@pObj, 3, 255) + 'log ON ' + @pObj + '; 
    UPDATE x
      SET ' + @pCol + ' += 120000
    FROM ' + @pObj + ' x
      INNER JOIN dev.CUSTOMDICT cd ON x.' + @pCol + ' = cd.ID
    WHERE cd.C_TYPE = SUBSTRING(''' + @rObj + ''', 3, 255);
    ENABLE TRIGGER tr_' + SUBSTRING(@pObj, 3, 255) + 'log ON ' + @pObj + ';'
  
  
  EXEC sys.sp_executesql @sql
  
	FETCH NEXT FROM upd_cursor INTO @pObj, @pCol, @rObj
END

CLOSE upd_cursor
DEALLOCATE upd_cursor
GO

DECLARE tbl_cursor CURSOR FOR 
SELECT DISTINCT 'T_' + UPPER(C_TYPE) FROM dev.CUSTOMDICT

DECLARE @tableName nvarchar(80)
DECLARE @sql nvarchar(MAX)

OPEN tbl_cursor
FETCH NEXT FROM tbl_cursor INTO @tableName 
WHILE @@FETCH_STATUS = 0 BEGIN
	SET @sql = '
    DELETE FROM ' + @tableName + '
    WHERE ID IN (SELECT ID FROM dev.CUSTOMDICT WHERE C_TYPE = SUBSTRING(''' + @tableName + ''', 3, 255))
  '
  
  EXEC sys.sp_executesql @sql 
  
  FETCH NEXT FROM tbl_cursor INTO @tableName 
END

CLOSE tbl_cursor
DEALLOCATE tbl_cursor
GO


declare @sql nvarchar(max) = ''

SELECT @sql += 'ALTER TABLE ' + OBJECT_NAME(parent_object_id) + ' NOCHECK CONSTRAINT ' +  fk.name + char(10) + char(13)
  FROM sys.foreign_keys fk
  WHERE referenced_object_id = OBJECT_ID('T_DICTIONARYITEMBASE')

 exec sp_executesql @sql

DELETE d 
FROM T_DICTIONARYITEMBASE d
  INNER JOIN dev.CUSTOMDICT c ON c.ID = d.ID AND d.C_TYPE = c.C_TYPE

  DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT C_INTEZMENYID, C_TANEVID
		FROM T_INTEZMENYADATOK

DECLARE @intezmenyId int, @tanevId int

OPEN intezmenytanevekCursor  
FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId

WHILE @@FETCH_STATUS = 0 
BEGIN  
	PRINT N'INTÉZMÉNY ID: ' + CAST(@intezmenyId AS NVARCHAR(10)) + N', TANÉV ID: ' + CAST(@tanevId AS NVARCHAR(10))

	SET IDENTITY_INSERT [dbo].[T_DICTIONARYITEMBASE] ON

  exec sp_Global_CreateDictionaryItems @intezmenyId, @tanevId

    SET IDENTITY_INSERT [dbo].[T_DICTIONARYITEMBASE] OFF

	FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId
END  

CLOSE intezmenytanevekCursor  
DEALLOCATE intezmenytanevekCursor


SET @sql = ''

SELECT @sql += 'ALTER TABLE ' + OBJECT_NAME(parent_object_id) + ' WITH CHECK CHECK CONSTRAINT ' +  fk.name + char(10) + char(13)
  FROM sys.foreign_keys fk
  WHERE referenced_object_id = OBJECT_ID('T_DICTIONARYITEMBASE')

 exec sp_executesql @sql

 go

UPDATE T_DICTIONARYITEMBASE SET C_NAME = SUBSTRING(C_NAME, 2, 1024) WHERE C_NAME like '€%'

DROP TABLE dev.CUSTOMDICT
