sqlserver把表里的所有为null的字段更新为空


if object_id('tempdb..#table') is not NULL--判断临时表是否存在
begin
    drop table #table
end

declare @tbname varchar(100),@sql varchar(400),@column varchar(100),@zflx varchar(40)
set @tbname='sypzspjl'
set @sql=''
set @column=''
set @zflx=''

select a.name as colnum,b.name as zflx,0 as bz into #table
from syscolumns a,systypes b
where a.xusertype=b.xusertype
and a.id=object_id(@tbname)

while exists(select 1 from #table where bz=0)
begin
    select top 1 @column=colnum,@zflx=zflx from #table where bz=0 
    set @sql = 'update '+@tbname+' set ' + @column + ' = '+case when @zflx in ('decimal','int','numeric','float','double','tinyint','smallint','mediumint') then '0' else '''''' end+
               ' where ' + @column + ' is null'
    PRINT @sql
    exec(@sql)
    if @@error<>0
    begin
        break;
    end
    update #table set bz=1 where colnum=@column
END

你可能感兴趣的:(sqlserver把表里的所有为null的字段更新为空)