I create a Job that runs the query below for all databases in a server in order to update empty values with null in all columns for all tables. The problem I'm facing is this query literally killed the sql server, was in a dev enviroment.
I notice the log for the first DataBase grew up from 6gb to 80gb and left the server without space.
Is there a better way to achieve update all empty columns with null in all Tables without crashing the server?
This query generate around 5,000 update statements for each database
I plane to run this kind of cleanup once a week.
SELECT
ROW_NUMBER ( )
OVER (ORDER BY [object_id] DESC) AS iRow
,name,[object_id]
INTO #temp
FROM sys.Tables
WHERE
name NOT LIKE 'old%'
AND name NOT LIKE '%old'
AND name NOT LIKE '%copy'
AND name NOT LIKE 'copy%'
AND name <> 'sysdiagrams'
--SELECT * FROM #temp
DECLARE @intFlag INT,@endFlag INT
SELECT @intFlag= MIN(iRow) FROM #temp
SELECT @endFlag= MAX(iRow) FROM #temp
CREATE TABLE #temp2(name VARCHAR(250),tablename VARCHAR(250))
--SELECT * FROM #temp
WHILE (@intFlag<=@endFlag)
BEGIN
INSERT INTO #temp2
SELECT c.name,(SELECT name FROM #temp WHERE iRow=@intFlag) AS TableName
FROM syscolumns c
INNER JOIN systypes t ON c.xtype=t.xtype
WHERE id =(SELECT [object_id] FROM #temp WHERE iRow=@intFlag)
AND isnullable = 1 AND c.xtype IN(99,167,175,231,239)
order by c.name
SET @intFlag = @intFlag + 1;
END
DECLARE @SQLquery AS VARCHAR(MAX) = ''
SELECT @SQLquery +='UPDATE ['+tablename+'] SET ['+ name+'] = NULL WHERE LTRIM(RTRIM(['+name+']))='''';'
FROM #temp2
--SELECT @SQLQuery
EXECUTE (@SQLquery)
DROP TABLE #temp
DROP TABLE #temp2
LTRIM(RTRIM(...))
to a column before comparing it to an empty string is probably redundant,WHERE columnname = ''
is usually enough, because trailing spaces are ignored (thus, a string of spaces would match an empty string without explicit trimming).