How can I update all NULL fields in a table to empty strings SQL SERVER 2008 -
i have table called dbo.authors , update null fields in table empty string. (basically exact opposite of this: how can update empty string fields in table null?)
i've tried , 'command succesful' message doesn't update null field empty string.
create proc resetnullfields (@tablename nvarchar(100)) create table #fieldnames ( pk int identity(1, 1) , field nvarchar(1000) null ); insert #fieldnames select column_name information_schema.columns table_name = @tablename declare @maxpk int; select @maxpk = max(pk) #fieldnames declare @pk int; set @pk = 1 declare @dynsql nvarchar(1000) while @pk <= @maxpk begin declare @currfieldname nvarchar(100); set @currfieldname = (select field #fieldnames pk = @pk) -- update field null empty string here: set @dynsql = 'update ' + @tablename + ' set ' + @currfieldname + ' ='' '' where' + @currfieldname + '= null' exec (@dynsql) select @pk = @pk + 1 end exec resetnullfields authors; go
at end of query string have this:
' ='' '' where' + @currfieldname + '= null'
there's (at least) 4 problems in little section. 1 you're missing whitespace around field name. fails account problematic column names. can't null comparison that. finally, code add single space, rather empty string. instead:
' ='''' [' + @currfieldname + '] null'
looking through code, doubt that's problem, though. @ minimum, want sure you're looking @ string-type columns (varchar, char, nvarchar, nchar, etc). won't able number (numeric, int, float, etc) , temporal (date, datetime, etc) columns. if want rid of null values in kinds of columns, you'll need pick other defaults.
i question wisdom of doing @ all. it's not idea (though there small number of scenarios can make sense).
Comments
Post a Comment