我需要将10个字段里面为NULL的记录更新为0,这10个字段名是sy1d1,sy1d2,sy1d3,sy1d4,sy1d5,sy2d1,sy2d2,sy2d3,sy2d4,sy2d5,我不想写10条update语句,所以做了一个循环。
问题出来,我发现下面的代码执行后,任何一个字段里面为NULL的记录都没有更新!
想问问各位是什么原因?declare @i int,@j int
declare @col varchar(5)
select @i=1
while @i<=2
  begin
    select @j=1
    
    while @j<=5
      begin
       select @col='sy'+convert(varchar(5),@i)+'d'+convert(varchar(5),@j)
       update split4
       set @col=0
       where @col is NULL
       select @j=@j+1
      end
      
    select @i=@i+1
   end

解决方案 »

  1.   

    Update split4
    set sy1d1=case when sy1d1 is null then 0 else sy1d1 end,
    sy1d2=case when sy1d2 is null then 0 else sy1d2 end,
    sy1d3=case when sy1d3 is null then 0 else sy1d3 end,
    sy1d4=case when sy1d4 is null then 0 else sy1d4 end,
    sy1d5=case when sy1d5 is null then 0 else sy1d5 end...类似这样写就可以了
      

  2.   


    呵呵这样也想当于写十次UPDATE 了,建议楼主就直接写十次好了!!写循环判断也很长
      

  3.   

    ---试一下 
    declare @i int,@j int
    declare @col varchar(5)
    select @i=1
    while @i<=2
      begin
        select @j=1
        
        while @j<=5
          begin
           select @col='sy'+convert(varchar(5),@i)+'d'+convert(varchar(5),@j)
           update split4
           set @col=case when @col is null then 0 else @col end,
           select @j=@j+1
          end
          
        select @i=@i+1
       end
      

  4.   

    呵呵 以前对于类似的问题 我都是老老实实写10次 今天想尝试新的方法 所以决定一定要求不写10次update
      

  5.   


     declare @i int,@j int
    declare @col varchar(5)
    select @i=1
    while @i<=2
      begin
        select @j=1
        
        while @j<=5
          begin
           select @col='sy'+convert(varchar(5),@i)+'d'+convert(varchar(5),@j)
           exec ('update split4 set '+@col+'=0
           where '+@col+' is NULL')
           select @j=@j+1
          end
          
        select @i=@i+1
       end
      

  6.   


    declare @i int,@j int
    declare @col varchar(5)
    declare @sql varchar(1000)
    select @i=1
    while @i<=2
      begin
        select @j=1
        
        while @j<=5
          begin
          set @sql=''
           select @col='sy'+convert(varchar(5),@i)+'d'+convert(varchar(5),@j)
           
           set @sql='update split4 set'+ @col+'=case when'+ @col+' is null then 0 else'+ @col+' end'
          exec(@sql)
           set @j=@j+1
          end
          
        select @i=@i+1
       end
      

  7.   

    有错误:
    消息 102,级别 15,状态 1,第 1 行
    'setsy1d1' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy1d2' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy1d3' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy1d4' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy1d5' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy2d1' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy2d2' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy2d3' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy2d4' 附近有语法错误。
    消息 102,级别 15,状态 1,第 1 行
    'setsy2d5' 附近有语法错误。
      

  8.   

    有错误:
    消息 245,级别 16,状态 1,第 11 行
    在将 varchar 值 'sy1d1' 转换成数据类型 int 时失败。
      

  9.   


    declare @i int,@j int
    declare @col varchar(5)
    declare @sql varchar(1000)
    select @i=1
    while @i<=2
      begin
        select @j=1
        
        while @j<=5
          begin
          set @sql=''
           select @col='sy'+convert(varchar(5),@i)+'d'+convert(varchar(5),@j)
           
           set @sql='update split4 set  '+   @col+'=case when  '+   @col+' is null then 0 else  '+   @col+' end'
          print(@sql)
         ----exec(@sql)
           set @j=@j+1
          end
          
        select @i=@i+1
       end
    ------------------------------------------
    update split4 set  sy1d1=case when  sy1d1 is null then 0 else  sy1d1 end
    update split4 set  sy1d2=case when  sy1d2 is null then 0 else  sy1d2 end
    update split4 set  sy1d3=case when  sy1d3 is null then 0 else  sy1d3 end
    update split4 set  sy1d4=case when  sy1d4 is null then 0 else  sy1d4 end
    update split4 set  sy1d5=case when  sy1d5 is null then 0 else  sy1d5 end
    update split4 set  sy2d1=case when  sy2d1 is null then 0 else  sy2d1 end
    update split4 set  sy2d2=case when  sy2d2 is null then 0 else  sy2d2 end
    update split4 set  sy2d3=case when  sy2d3 is null then 0 else  sy2d3 end
    update split4 set  sy2d4=case when  sy2d4 is null then 0 else  sy2d4 end
    update split4 set  sy2d5=case when  sy2d5 is null then 0 else  sy2d5 end刚才少设置空格了。
      

  10.   


    update split4 set  sy1d1=case when  sy1d1 is null then 0 else  sy1d1 end
    update split4 set  sy1d2=case when  sy1d2 is null then 0 else  sy1d2 end
    update split4 set  sy1d3=case when  sy1d3 is null then 0 else  sy1d3 end
    update split4 set  sy1d4=case when  sy1d4 is null then 0 else  sy1d4 end
    update split4 set  sy1d5=case when  sy1d5 is null then 0 else  sy1d5 end
    update split4 set  sy2d1=case when  sy2d1 is null then 0 else  sy2d1 end
    update split4 set  sy2d2=case when  sy2d2 is null then 0 else  sy2d2 end
    update split4 set  sy2d3=case when  sy2d3 is null then 0 else  sy2d3 end
    update split4 set  sy2d4=case when  sy2d4 is null then 0 else  sy2d4 end
    update split4 set  sy2d5=case when  sy2d5 is null then 0 else  sy2d5 end如果上面的语句执行不了,那就是表结构的问题了。
      

  11.   

    能够运行过去真是佩服呀
    update...Set 后面应该是实际字段名, 而不是变量
    如果用变更的话, 只能用动态执行的方法了, exec('update ...set ' + @col + '..省略')
      

  12.   

    你的方法得出来的print,应该是只是动态执行的问题
    declare @i int,@j int
    declare @SQL varchar(max)
    declare @col varchar(5)
    select @i=1
    while @i<=2
      begin
        select @j=1
        
        while @j<=5
          begin
           select @col='sy'+convert(varchar(5),@i)+'d'+convert(varchar(5),@j)
           set @SQL = 'update split4 set '+@col+'=0 where '+@col+' is NULL'
    print @SQL
           --exec(@SQL)
           select @j=@j+1
          end
          
        select @i=@i+1
       end
    /*
    update split4 set sy1d1=0 where sy1d1 is NULL
    update split4 set sy1d2=0 where sy1d2 is NULL
    update split4 set sy1d3=0 where sy1d3 is NULL
    update split4 set sy1d4=0 where sy1d4 is NULL
    update split4 set sy1d5=0 where sy1d5 is NULL
    update split4 set sy2d1=0 where sy2d1 is NULL
    update split4 set sy2d2=0 where sy2d2 is NULL
    update split4 set sy2d3=0 where sy2d3 is NULL
    update split4 set sy2d4=0 where sy2d4 is NULL
    update split4 set sy2d5=0 where sy2d5 is NULL
    */
      

  13.   


    不明白这样的用意了。
    一条update 难道不比循环来的好么
      

  14.   

    不用那么麻烦地判断吧Update split4 
    set sy1d1=isnull(sy1d1,0),sy1d2=isnull(sy1d2,0),...
      

  15.   

    对 我就是想搞清楚这个 SQL里面哪些地方必须是实际的字段名 哪些地方可以是变量?