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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容