读书人

不使用游标怎么对一个数组的空值进行

发布时间: 2012-04-11 17:42:33 作者: rapoo

不使用游标,如何对一个数组的空值进行追加?
例子:
序号日期余额
120120101
220120102
320120103 10
420120104
520120105
620120106 20
720120107
820120108
920120109 30
1020120110
1120120111

某账号1月3号余额为10元,6号为20元,9号为30元。现需要对余额列为空的数据进行追加,结果为:1号、2号为10元,4号、5号为10元,7号、8号为20元,10号、11号为30元。
请问有什么好办法呢?

[解决办法]

SQL code
create table som(序号 int, 日期 varchar(12), 余额 int)insert into somselect 1, '20120101', null union all select 2, '20120102', null union all select 3, '20120103', 10 union allselect 4, '20120104', null union all select 5, '20120105', null union all select 6, '20120106', 20 union allselect 7, '20120107', null union all select 8, '20120108', null union all select 9, '20120109', 30 union allselect 10, '20120110', null union all select 11, '20120111', null select * from som序号          日期           余额----------- ------------ -----------1           20120101     NULL2           20120102     NULL3           20120103     104           20120104     NULL5           20120105     NULL6           20120106     207           20120107     NULL8           20120108     NULL9           20120109     3010          20120110     NULL11          20120111     nullupdate aset a.余额=isnull((select b.余额 from som b  where b.余额 is not null and b.序号= (select max(序号) from som c  where c.余额 is not null and c.序号<a.序号)),(select top 1 余额 from som where 余额 is not null))from som awhere a.余额 is nullselect * from som序号          日期           余额----------- ------------ -----------1           20120101     102           20120102     103           20120103     104           20120104     105           20120105     106           20120106     207           20120107     208           20120108     209           20120109     3010          20120110     3011          20120111     30(11 row(s) affected) 

读书人网 >SQL Server

热点推荐