Below is the script used to update values in multiple tables. This is the script I used to update according to my requirement. Kindly modify it as needed.
DECLARE @oldCompanyid INT, @newcompanyid INT, @oldCompanyCode VARCHAR(50)
DECLARE @t Table(Name VARCHAR(100), MaxId INT)
INSERT INTO @t(Name, MaxId)
SELECT TableName ,maxAuditTaskId FROM dbo.AuditTaskSummary WHERE TableName = 'SourceCode' and PackageName = 'pkg_Sourcecode'
SELECT @oldCompanyid = @CompanyIdOld --7
SELECT @newcompanyid = 3
CREATE TABLE #t1(RowNum INT, TableName VARCHAR(100), QueryData VARCHAR(1000))
DECLARE @maxRow int, @minRow int
INSERT INTO #t1 (RowNum, TableName, QueryData)
SELECT ROW_NUMBER () OVER(ORDER BY T.name DESC) RowNum,T.name AS tableName,
'UPDATE ' + SCHEMA_NAME(t.schema_id) + '.'
+ T.NAME + ' SET CompanyID = '+CAST (@newcompanyid AS VARCHAR(10)) + ','
+ 'Audittaskid =' +CAST (ATS.maxAuditTaskId+1 AS VARCHAR(10))
+ ' WHERE CompanyID='+ CAST(@oldCompanyid AS VARCHAR(10)) +';' AS QueryData
FROM sys.tables as T
INNER JOIN sys.COLUMNS C ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN (SELECT TableName ,maxAuditTaskId FROM dbo.AuditTaskSummary WHERE TableName NOT IN ('SourceCode')
UNION
SELECT Name, maxid FROM @t) ATS ON ATS.TableName = T.name
WHERE c.name ='CompanyID' AND SCHEMA_NAME(t.schema_id) = 'dbo'
AND t.name NOT IN ('Company','Company_backup')
ORDER BY SCHEMA_NAME(t.schema_id), t.name;
SELECT * FROM #t1
SELECT @maxRow = (SELECT MAX(RowNum) FROM #t1), @minRow = (SELECT MIN(RowNum) FROM #t1)
SELECT @maxRow, @minRow
WHILE @maxRow >= @minRow
BEGIN
DECLARE @tablevalue VARCHAR(1000), @tablename VARCHAR(100)
SELECT @tablevalue = (SELECT QueryData from #t1 WHERE RowNum = @minRow), @tablename = (SELECT tablename FROM #t1 WHERE RowNum = @minRow)
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @tablename
AND COLUMN_NAME = 'CompanyId')
EXEC (@tablevalue);
SET @minRow = @minRow +1
END
DROP TABLE #t1;
DECLARE @oldCompanyid INT, @newcompanyid INT, @oldCompanyCode VARCHAR(50)
DECLARE @t Table(Name VARCHAR(100), MaxId INT)
INSERT INTO @t(Name, MaxId)
SELECT TableName ,maxAuditTaskId FROM dbo.AuditTaskSummary WHERE TableName = 'SourceCode' and PackageName = 'pkg_Sourcecode'
SELECT @oldCompanyid = @CompanyIdOld --7
SELECT @newcompanyid = 3
CREATE TABLE #t1(RowNum INT, TableName VARCHAR(100), QueryData VARCHAR(1000))
DECLARE @maxRow int, @minRow int
INSERT INTO #t1 (RowNum, TableName, QueryData)
SELECT ROW_NUMBER () OVER(ORDER BY T.name DESC) RowNum,T.name AS tableName,
'UPDATE ' + SCHEMA_NAME(t.schema_id) + '.'
+ T.NAME + ' SET CompanyID = '+CAST (@newcompanyid AS VARCHAR(10)) + ','
+ 'Audittaskid =' +CAST (ATS.maxAuditTaskId+1 AS VARCHAR(10))
+ ' WHERE CompanyID='+ CAST(@oldCompanyid AS VARCHAR(10)) +';' AS QueryData
FROM sys.tables as T
INNER JOIN sys.COLUMNS C ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN (SELECT TableName ,maxAuditTaskId FROM dbo.AuditTaskSummary WHERE TableName NOT IN ('SourceCode')
UNION
SELECT Name, maxid FROM @t) ATS ON ATS.TableName = T.name
WHERE c.name ='CompanyID' AND SCHEMA_NAME(t.schema_id) = 'dbo'
AND t.name NOT IN ('Company','Company_backup')
ORDER BY SCHEMA_NAME(t.schema_id), t.name;
SELECT * FROM #t1
SELECT @maxRow = (SELECT MAX(RowNum) FROM #t1), @minRow = (SELECT MIN(RowNum) FROM #t1)
SELECT @maxRow, @minRow
WHILE @maxRow >= @minRow
BEGIN
DECLARE @tablevalue VARCHAR(1000), @tablename VARCHAR(100)
SELECT @tablevalue = (SELECT QueryData from #t1 WHERE RowNum = @minRow), @tablename = (SELECT tablename FROM #t1 WHERE RowNum = @minRow)
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @tablename
AND COLUMN_NAME = 'CompanyId')
EXEC (@tablevalue);
SET @minRow = @minRow +1
END
DROP TABLE #t1;
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.