日期:2014-05-17  浏览次数:20393 次

~~~~~~~~~SQL更新语句,马上给分!~~~~~~~
TableA
Col01         Col02
张三           80
张三           87
               ABC
李四           32
赵5            83
               kk
               TT


希望得到结果如下:
Col01         Col02
张三           80
张三           87
张三           ABC
李四           32
赵5            83
赵5            kk
赵5            dadfa

------解决方案--------------------

create table TableA
(Col01 varchar(10),Col02 varchar(10))

insert into TableA
 select '张三','80' union all
 select '张三','87' union all
 select '','ABC' union all
 select '李四','32' union all
 select '赵5','83' union all
 select '','kk' union all
 select '','TT'
 

-- 更新
with t as
(select Col01,Col02,
        row_number() over(order by getdate()) 'rn' 
 from TableA)
update a
 set a.Col01=(select top 1 b.Col01 from t b 
              where b.rn<a.rn and b.Col01<>'' 
              order by b.rn desc)
 from t a
 where a.Col01=''


-- 结果
select Col01,Col02 from TableA

/*
Col01      Col02
---------- ----------
张三         80
张三         87
张三         ABC
李四         32
赵5         83
赵5         kk
赵5         TT

(7 row(s) affected)
*/

------解决方案--------------------

create table TableA
(Col01 varchar(10),Col02 varchar