我需要将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
问题出来,我发现下面的代码执行后,任何一个字段里面为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
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...类似这样写就可以了
呵呵这样也想当于写十次UPDATE 了,建议楼主就直接写十次好了!!写循环判断也很长
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
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
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
消息 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' 附近有语法错误。
消息 245,级别 16,状态 1,第 11 行
在将 varchar 值 'sy1d1' 转换成数据类型 int 时失败。
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刚才少设置空格了。
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如果上面的语句执行不了,那就是表结构的问题了。
update...Set 后面应该是实际字段名, 而不是变量
如果用变更的话, 只能用动态执行的方法了, exec('update ...set ' + @col + '..省略')
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
*/
不明白这样的用意了。
一条update 难道不比循环来的好么
set sy1d1=isnull(sy1d1,0),sy1d2=isnull(sy1d2,0),...