Script to Fix all null values into database
/By Brian Alvarez Ayala/ email - [email protected] website - w w w . a s e s o r i a r e m o t a . c o m
Declare @COLUMN_NAME varchar(40) Declare @DATA_TYPE varchar(40) Declare @TABLES_NAMES varchar(40) Declare @TABLE_SCHEMA varchar(40) Declare @IS_NULLABLE varchar(3)
Declare @SqlCMD varchar(MAX) Declare tablas cursor for select t.TABLE_SCHEMA ,t.TABLE_NAME,c.COLUMN_NAME,c.DATA_TYPE,c.IS_NULLABLE from INFORMATION_SCHEMA.TABLES t inner join INFORMATION_SCHEMA.COLUMNS c on c.TABLE_NAME = t.TABLE_NAME and c.TABLE_SCHEMA = t.TABLE_SCHEMA open tablas fetch next from tablas into @TABLE_SCHEMA,@TABLES_NAMES,@COLUMN_NAME,@DATA_TYPE,@IS_NULLABLE while @@FETCH_STATUS =0 begin
if (@DATA_TYPE = 'numeric' or @DATA_TYPE = 'int') and @IS_NULLABLE='YES' begin set @SqlCMD ='Update ' + @TABLE_SCHEMA+'.'+@TABLES_NAMES + ' set ' + @COLUMN_NAME +' = 0 where ' + @COLUMN_NAME + ' is null' print @sqlcmd end
if (@DATA_TYPE = 'nvarchar' or @DATA_TYPE = 'varchar' or @DATA_TYPE = 'char') and @IS_NULLABLE='YES' begin set @SqlCMD ='Update ' + @TABLE_SCHEMA+'.'+@TABLES_NAMES + ' set ' + @COLUMN_NAME + ' = ' + ''' ''' + ' where ' + @COLUMN_NAME + ' is null' print @sqlcmd end if (@DATA_TYPE = 'bit') and @IS_NULLABLE='YES' begin set @SqlCMD ='Update ' + @TABLE_SCHEMA+'.'+@TABLES_NAMES + ' set ' + @COLUMN_NAME + ' = ' + '''False''' + ' where ' + @COLUMN_NAME + ' is null' print @sqlcmd end fetch next from tablas into @TABLE_SCHEMA,@TABLES_NAMES,@COLUMN_NAME,@DATA_TYPE,@IS_NULLABLE end
close tablas deallocate tablas