kreta/Kreta.DataAccess.Migrations/Scripts/Archive/20200220183744_KRETA2_2985/uspCloneIntezmenyChangeIds.sql
2024-03-13 00:33:46 +01:00

458 lines
20 KiB
Transact-SQL

DROP PROCEDURE IF EXISTS dev.uspCloneIntezmenyChangeIds
GO
CREATE PROCEDURE dev.uspCloneIntezmenyChangeIds
@pSourceIntezmenyId int
,@pSourceDatabase nvarchar(60)
,@pEntityHistoryMigration int
,@pDebugMode bit = 0
-- ,@pChangeInTargetDB bit /*1 - Target DB-ben, 0 - SourceDB-ben */
AS
BEGIN
DECLARE
@parentObjName nvarchar(50)
,@parentColName nvarchar(50)
,@refedObjName nvarchar(50)
,@intezmenyColumnName nvarchar(50)
,@sql nvarchar(max)
,@intezmenyId int
,@databaseToUse nvarchar(60)
,@tableName nvarchar(50)
,@columnName nvarchar(50)
,@message nvarchar(max)
,@rowCount int
SET @databaseToUse = @pSourceDatabase + '.dbo.'
SET @intezmenyId = @pSourceIntezmenyId
SET @message = 'CHANGE IDS... - ' + FORMAT(GETDATE(), 'hh:mm:ss.ff')
RAISERROR (@message, 10, 1) WITH NOWAIT
-- ================================================================================ --
-- Az ID-k átírása a Foreign key-k alapján --
-- ================================================================================ --
DECLARE tbl_cur CURSOR LOCAL 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
,cc.COLUMN_NAME as intezmeny_column_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
INNER JOIN (SELECT DISTINCT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN (N'C_ALINTEZMENYID', N'C_INTEZMENYID') ) cc ON cc.TABLE_NAME = OBJECT_NAME(pc.[object_id])
WHERE pc.[name] NOT IN (N'C_ALINTEZMENYID', N'C_INTEZMENYID', 'ID') --rc.[name] <> 'ID' AND OBJECT_NAME(rc.[object_id]) <> 'T_INTEZMENY'
AND rc.[name] NOT IN (N'C_ALTANEVID', N'C_TANEVID')
AND OBJECT_NAME(pc.[object_id]) <> N'T_ENTITYATTRIBUTEHISTORY'
OPEN tbl_cur
FETCH NEXT FROM tbl_cur
INTO @parentObjName, @parentColName, @refedObjName, @intezmenyColumnName
WHILE @@FETCH_STATUS = 0 BEGIN
SET @sql = N'
UPDATE x
SET ' + @parentColName + ' = t.[newId]
FROM ' + @databaseToUse + @parentObjName + ' x
INNER JOIN MappingTable t ON t.oldId = x.' + @parentColName + ' AND t.tableName = ''' + @refedObjName + ''';
SET @rowCount = @@ROWCOUNT
'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - ' + @parentObjName + '.' + @parentColName + ' - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
FETCH NEXT FROM tbl_cur
INTO @parentObjName, @parentColName, @refedObjName, @intezmenyColumnName
END
CLOSE tbl_cur
DEALLOCATE tbl_cur
-- ================================================================================ --
-- A ELOZOTANEVIREKORDID átírása --
-- ================================================================================ --
DECLARE tbl_cur CURSOR LOCAL FOR
SELECT c.TABLE_NAME, c.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS c
INNER JOIN INFORMATION_SCHEMA.TABLES t ON t.TABLE_NAME = c.TABLE_NAME AND c.TABLE_CATALOG = t.TABLE_CATALOG AND c.TABLE_SCHEMA = t.TABLE_SCHEMA
WHERE COLUMN_NAME = 'ELOZOTANEVIREKORDID' AND t.TABLE_SCHEMA = 'dbo' AND t.TABLE_TYPE = 'BASE TABLE'
AND c.TABLE_NAME IN (SELECT tableName FROM MappingTable)
OPEN tbl_cur
FETCH NEXT FROM tbl_cur
INTO @tableName, @columnName
WHILE @@FETCH_STATUS = 0 BEGIN
SET @sql = N'
UPDATE x SET
ELOZOTANEVIREKORDID = t.[newId]
FROM ' + @databaseToUse + @tableName + ' x
INNER JOIN MappingTable t ON t.oldId = x.ELOZOTANEVIREKORDID AND t.tableName = ''' + @tableName + ''';
SET @rowCount = @@ROWCOUNT
'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@rowCount int output', @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - ' + @tableName + '.ELOZOTANEVIREKORDID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
FETCH NEXT FROM tbl_cur
INTO @tableName, @columnName
END
CLOSE tbl_cur
DEALLOCATE tbl_cur
-- ================================================================================ --
-- A CREATOR és a MODIFIER átírása --
-- ================================================================================ --
DECLARE tbl_cur CURSOR LOCAL FOR
SELECT c.TABLE_NAME, c.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS c
INNER JOIN INFORMATION_SCHEMA.TABLES t ON t.TABLE_NAME = c.TABLE_NAME AND c.TABLE_CATALOG = t.TABLE_CATALOG AND c.TABLE_SCHEMA = t.TABLE_SCHEMA
WHERE COLUMN_NAME IN ('CREATOR', 'MODIFIER') AND t.TABLE_SCHEMA = 'dbo' AND t.TABLE_TYPE = 'BASE TABLE'
AND c.TABLE_NAME IN (SELECT tableName FROM MappingTable)
ORDER BY c.TABLE_NAME
OPEN tbl_cur
FETCH NEXT FROM tbl_cur
INTO @tableName, @columnName
WHILE @@FETCH_STATUS = 0 BEGIN
SET @sql = N'
UPDATE x
SET ' + @columnName + ' = t.[newId]
FROM ' + @databaseToUse + @tableName + ' x
INNER JOIN MappingTable t ON t.oldId = x.' + @columnName + ' AND t.tableName = ''T_FELHASZNALO'';
SET @rowCount = @@ROWCOUNT
'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@rowCount int output', @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - ' + @tableName + '.' + @columnName + ' - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
FETCH NEXT FROM tbl_cur
INTO @tableName, @columnName
END
CLOSE tbl_cur
DEALLOCATE tbl_cur
-- ================================================================================ --
-- FK nincs, de át kell írni az ID-kat --
-- ================================================================================ --
SET @sql = N'
UPDATE o SET
o.C_CSENGETESIRENDID = newId
FROM ' + @databaseToUse + 'T_ORARENDIORA o
INNER JOIN MappingTable t ON t.oldId = o.C_CSENGETESIRENDID
WHERE o.C_INTEZMENYID = @intezmenyId
AND t.tableName = ''T_CSENGETESIREND''
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ORARENDIORA.C_CSENGETESIRENDID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE o SET
o.C_CSENGETESIRENDORAID = newId
FROM ' + @databaseToUse + 'T_ORARENDIORA o
INNER JOIN MappingTable t ON t.oldId = o.C_CSENGETESIRENDORAID
WHERE o.C_INTEZMENYID = @intezmenyId
AND t.tableName = ''T_CSENGETESIRENDORA''
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ORARENDIORA.C_CSENGETESIRENDORAID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE o SET
o.C_CSENGETESIRENDID = newId
FROM ' + @databaseToUse + 'T_TANITASIORA o
INNER JOIN MappingTable t ON t.oldId = o.C_CSENGETESIRENDID
WHERE o.C_INTEZMENYID = @intezmenyId
AND t.tableName = ''T_CSENGETESIREND''
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_TANITASIORA.C_CSENGETESIRENDID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE o SET
o.C_CSENGETESIRENDORAID = newId
FROM ' + @databaseToUse + 'T_TANITASIORA o
INNER JOIN MappingTable t ON t.oldId = o.C_CSENGETESIRENDORAID
WHERE o.C_INTEZMENYID = @intezmenyId
AND t.tableName = ''T_CSENGETESIRENDORA''
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_TANITASIORA.C_CSENGETESIRENDORAID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE o SET
o.C_ORARENDIORAGROUPID = newId
FROM ' + @databaseToUse + 'T_ORARENDIORA o
INNER JOIN MappingTable t ON t.oldId = o.C_ORARENDIORAGROUPID
WHERE o.C_INTEZMENYID = @intezmenyId
AND t.tableName = ''T_ORARENDIORA''
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ORARENDIORA.C_ORARENDIORAGROUPID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE o SET
o.C_ORARENDIORAGROUPID = newId
FROM ' + @databaseToUse + 'T_TANITASIORA o
INNER JOIN MappingTable t ON t.oldId = o.C_ORARENDIORAGROUPID
WHERE o.C_INTEZMENYID = @intezmenyId
AND t.tableName = ''T_ORARENDIORA''
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_TANITASIORA.C_ORARENDIORAGROUPID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE o SET
o.C_FOGLALKOZASID = newId
FROM ' + @databaseToUse + 'T_ORARENDIORA o
INNER JOIN MappingTable t ON t.oldId = o.C_FOGLALKOZASID
WHERE o.C_INTEZMENYID = @intezmenyId
AND t.tableName = ''T_FOGLALKOZAS''
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ORARENDIORA.C_FOGLALKOZASID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE o SET
o.C_FOGLALKOZASID = newId
FROM ' + @databaseToUse + 'T_TANITASIORA o
INNER JOIN MappingTable t ON t.oldId = o.C_FOGLALKOZASID
WHERE o.C_INTEZMENYID = @intezmenyId
AND t.tableName = ''T_FOGLALKOZAS''
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_TANITASIORA.C_FOGLALKOZASID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
-- ================================================================================ --
-- T_DASHBOARDUZENETFELHASZNALO-k átírása --
-- ================================================================================ --
SET @sql = N'
UPDATE o SET
o.C_DASHBOARDUZENETID = trgd.ID
FROM ' + @databaseToUse + 'T_DASHBOARDUZENETFELHASZNALO o
INNER JOIN ' + @databaseToUse + 'T_DASHBOARDUZENET srcd ON o.C_DASHBOARDUZENETID = srcd.ID
INNER JOIN T_DASHBOARDUZENET trgd ON srcd.C_EGYEDIAZONOSITO = trgd.C_EGYEDIAZONOSITO'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@rowCount int output',@rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_DASHBOARDUZENETFELHASZNALO.C_DASHBOARDUZENETID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
-- ================================================================================ --
-- ENTITY HISTORY ID-k átírása --
-- ================================================================================ --
IF @pEntityHistoryMigration = 1 BEGIN
SET @sql = N'
UPDATE e
SET e.C_TANEVID = t.newId
FROM ' + @databaseToUse + 'T_ENTITYHISTORY e
INNER JOIN MappingTable t ON t.oldId = e.C_TANEVID AND t.tableName = ''T_TANEV''
--WHERE e.C_INTEZMENYID = @intezmenyId
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYHISTORY.C_TANEVID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE e
SET e.C_ENTITYID = t.newId
FROM ' + @databaseToUse + 'T_ENTITYHISTORY e
INNER JOIN MappingTable t ON t.oldId = e.C_ENTITYID AND t.tableName = e.C_ENTITYNAME
--WHERE e.C_INTEZMENYID = @intezmenyId
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYHISTORY.C_ENTITYID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE e
SET e.C_FELHASZNALOID = t.newId
FROM ' + @databaseToUse + 'T_ENTITYHISTORY e
INNER JOIN MappingTable t ON t.oldId = e.C_FELHASZNALOID AND t.tableName = ''T_FELHASZNALO''
--WHERE e.C_INTEZMENYID = @intezmenyId
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYHISTORY.C_FELHASZNALOID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE ea
SET ea.C_ENTITYHISTORYID = t.newId
FROM ' + @databaseToUse + 'T_ENTITYATTRIBUTEHISTORY ea
INNER JOIN MappingTable t ON t.oldId = ea.C_ENTITYHISTORYID AND t.tableName = ''T_ENTITYHISTORY''
--WHERE e.C_INTEZMENYID = @intezmenyId
SET @rowCount = @@ROWCOUNT'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N'@intezmenyId int, @rowCount int output', @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYATTRIBUTEHISTORY.C_ENTITYHISTORYID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
END
IF @pEntityHistoryMigration = 1 BEGIN
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE [name] = 'IX_TMP_001') BEGIN
SET @sql = N'
CREATE INDEX IX_TMP_001 ON ' + @databaseToUse + 'T_ENTITYHISTORY (C_ENTITYNAME, C_INTEZMENYID)
WHERE C_INTEZMENYID = ' + CAST(@intezmenyId AS NVARCHAR(10)) + ';
CREATE INDEX IX_TMP_002 ON ' + @databaseToUse + 'T_ENTITYATTRIBUTEHISTORY (C_ENTITYHISTORYID, C_PROPERTYNAME)
INCLUDE (C_CURRENTVALUE, C_ORIGINALVALUE)
'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql
END
DECLARE tbl_cur CURSOR LOCAL 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
-- ,rc.[name] AS referenced_column_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 pc.[name] NOT IN (N'C_ALINTEZMENYID', N'C_INTEZMENYID', 'ID') --rc.[name] <> 'ID' AND OBJECT_NAME(rc.[object_id]) <> 'T_INTEZMENY'
AND rc.[name] NOT IN (N'C_ALTANEVID', N'C_TANEVID')
AND NOT pc.[name] = 'C_NAME' -- Tábla - Oszlop kivételek
AND OBJECT_NAME(rc.[object_id]) IN (SELECT tableName FROM MappingTable)
ORDER BY OBJECT_NAME(pc.[object_id])
OPEN tbl_cur
FETCH NEXT FROM tbl_cur
INTO @parentObjName, @parentColName, @refedObjName
WHILE @@FETCH_STATUS = 0 BEGIN
--PRINT @parentColName + ' - ' + @parentObjName
SET @sql = N'
UPDATE a
SET a.C_CURRENTVALUE = CAST(t.[newId] AS varchar(30))
FROM ' + @databaseToUse + 'T_ENTITYATTRIBUTEHISTORY a
INNER JOIN ' + @databaseToUse + 'T_ENTITYHISTORY e ON a.C_ENTITYHISTORYID = e.ID
INNER JOIN MappingTable t ON t.oldId = TRY_CAST(NULLIF(a.C_CURRENTVALUE, N''#NULL#'') AS int)
AND t.tableName = @refedObjName
AND a.C_PROPERTYNAME = @parentColName
AND e.C_ENTITYNAME = @parentObjName
AND e.C_INTEZMENYID = @pIntezmenyId
SET @rowCount = @@ROWCOUNT
'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql,
N'@parentObjName nvarchar(50), @parentColName nvarchar(50), @refedObjName nvarchar(50), @pIntezmenyId int, @rowCount int output',
@parentObjName, @parentColName, @refedObjName, @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYATTRIBUTEHISTORY.C_CURRENTVALUE - ' + @parentObjName + '.' + @parentColName + ' - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
SET @sql = N'
UPDATE a
SET a.C_ORIGINALVALUE = CAST(t.[newId] AS varchar(30))
FROM ' + @databaseToUse + 'T_ENTITYATTRIBUTEHISTORY a
INNER JOIN ' + @databaseToUse + 'T_ENTITYHISTORY e ON a.C_ENTITYHISTORYID = e.ID
INNER JOIN MappingTable t ON t.oldId = TRY_CAST(NULLIF(a.C_ORIGINALVALUE, N''#NULL#'') AS int)
AND t.tableName = @refedObjName
AND a.C_PROPERTYNAME = @parentColName
AND e.C_ENTITYNAME = @parentObjName
AND e.C_INTEZMENYID = @pIntezmenyId'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql,
N'@parentObjName nvarchar(50), @parentColName nvarchar(50), @refedObjName nvarchar(50), @pIntezmenyId int, @rowCount int output',
@parentObjName, @parentColName, @refedObjName, @intezmenyId, @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYATTRIBUTEHISTORY.C_ORIGINALVALUE - ' + @parentObjName + '.' + @parentColName + ' - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
FETCH NEXT FROM tbl_cur
INTO @parentObjName, @parentColName, @refedObjName
END
CLOSE tbl_cur
DEALLOCATE tbl_cur
SET @sql = N'
UPDATE e
SET e.C_TANEVID = t.newId
FROM ' + @databaseToUse + 'T_ENTITYHISTORY e
INNER JOIN MappingTable t ON t.oldId = e.C_TANEVID AND t.tableName = ''T_TANEV''
--WHERE e.C_INTEZMENYID = @intezmenyId;'
IF @pDebugMode = 1 RAISERROR (@sql, 10, 1) WITH NOWAIT
EXEC sp_executesql @sql, N' @rowCount int output', @rowCount output
SET @message = ' ID ' + FORMAT(GETDATE(), 'HH:mm:ss.ff') + ' - T_ENTITYHISTORY.C_TANEVID - ' + CAST(@rowCount AS nvarchar(20)) + ' sor'
RAISERROR (@message, 10, 1) WITH NOWAIT
DROP INDEX IF EXISTS IX_TMP_001 ON T_ENTITYHISTORY
DROP INDEX IF EXISTS IX_TMP_002 ON T_ENTITYATTRIBUTEHISTORY
END
SET @message = 'CHANGE IDS DONE - ' + FORMAT(GETDATE(), 'HH:mm:ss.ff')
RAISERROR (@message, 10, 1) WITH NOWAIT
END
GO