美文网首页
sqlserver把表里的所有为null的字段更新为空

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

作者: 橘子皮的皮 | 来源:发表于2019-03-11 15:30 被阅读0次

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的字段更新为空

      本文链接:https://www.haomeiwen.com/subject/fprbpqtx.html