更新特定数字之间的空列的值 [英] update values of Null column between particular numbers

查看:89
本文介绍了更新特定数字之间的空列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的sql数据库中有一个名为S-No的列。

其中包含如下值:



I have column in my sql database named as S-No.
Which has values like following :

ID         S-No
____       ____
 1          1
____       ____
 2          2
____       ____
 3         Null
____       ____
 4         Null
____       ____
 5          3
____       ____
 6         Null
____       ____
 7         Null
____       ____
 8         Null
____       ____
 9         Null
____       ____
 10        Null
____       ____
 11         4
____       ____
 12        Null









现在,我想以这样的方式编写查询,其中S-No列包含Null在1到1之间2,将值更新为1 .....并且在2到3之间有Null,将值更新为2 ......依此类推......例如。 3到4 - 更新为3,4到5 ---更新为4,5到6 ......我希望你会建议我查询..我无法写...皱眉| :(我希望你guyz会帮忙。



结果应该是这样的:



.
.


Now, i want to write query in such a way that, which S-No column contains Null between 1 to 2, update there value to 1..... And which has Null between 2 To 3, update there value to 2... And so on... eg. 3 to 4 -- update as 3, 4 to 5 --- update as 4, 5 to 6...... I hope you will suggest me the query.. i am not able to write.. Frown | :( i hope you guyz will help.

Result Should be like this :

ID         S-No
____       ____
 1          1
____       ____
 2          2
____       ____
 3          2
____       ____
 4          2
____       ____
 5          3
____       ____
 6          3
____       ____
 7          3
____       ____
 8          3
____       ____
 9          3
____       ____
 10         3
____       ____
 11         4
____       ____
 12         4



.
.

推荐答案

你可以试试像

You can try something like
UPDATE MyTableName a
SET [S-No] = (SELECT MAX([S-No])
              FROM MyTableName b
              WHERE b.Id < a.Id
              AND   b.[S-No] IS NOT NULL)
WHERE [S-No] IS NULL


begin



设置nocount





创建表#temp



row_id int,

row_value int







插入#temp值(1,1)

插入#temp值(2,2)

插入#temp值( 3,null)

插入#temp值(4,null)

插入#temp值(5,3)

插入#temp values(6,null)

插入#temp值(7,null)

插入#temp值(8,null)

插入#temp值(9,null)

插入#temp值(10,null)

insert到#temp值(11,4)

插入#temp值(12,null)



select * from #temp



声明@row_id int = 0

声明@row_value int

声明@temp_row_value int



而(1 = 1)

开始



选择前1 @ row_id = row_id,

@ row_value = row_value
来自#temp的


其中row_id> @row_id

按行_id排序



if(@@ ROWCOUNT = 0)





开始

休息

结束



如果@row_value为空

开始



更新#temp

set row_value = @ temp_row_value

其中row_id = @ row_id



结束



其他

开始

选择@ temp_row_value = @ row_value

结束



end



select * from #temp

drop table #temp



end
begin

set nocount on


create table #temp
(
row_id int ,
row_value int

)

insert into #temp values(1,1)
insert into #temp values(2,2)
insert into #temp values(3,null)
insert into #temp values(4,null)
insert into #temp values(5,3)
insert into #temp values(6,null)
insert into #temp values(7,null)
insert into #temp values(8,null)
insert into #temp values(9,null)
insert into #temp values(10,null)
insert into #temp values(11,4)
insert into #temp values(12,null)

select * from #temp

declare @row_id int=0
declare @row_value int
declare @temp_row_value int

while(1=1)
begin

select top 1 @row_id=row_id,
@row_value=row_value
from #temp
where row_id>@row_id
order by row_id

if(@@ROWCOUNT=0)


begin
break
end

if @row_value is null
begin

update #temp
set row_value=@temp_row_value
where row_id=@row_id

end

else
begin
select @temp_row_value=@row_value
end

end

select * from #temp
drop table #temp

end


这篇关于更新特定数字之间的空列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆