~~~~~~~~~SQL更新语句,马上给分!~~~~~~~

hxm20003 2013-10-21 11:25:50
TableA
Col01 Col02
张三 80
张三 87
ABC
李四 32
赵5 83
kk
TT


希望得到结果如下:
Col01 Col02
张三 80
张三 87
张三 ABC
李四 32
赵5 83
赵5 kk
赵5 dadfa
...全文
156 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
如果数据中的空是null的话,可以这样:


drop table tableA

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

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

;with t
as
(
select col01,
       col02,
       row_number() over(order by @@servername) as rownum
from tableA
)

update t
set col01 = case when t.col01 is not null
                      then t.col01
                 else (select top 1 col01
                       from t t1 
                       where t1.rownum < t.rownum and t1.col01 is not null
                       order by t1.rownum desc)
            end



select *
from tableA
/*
Col01	Col02
张三	80
张三	87
张三	ABC
李四	32
赵5	83
赵5	kk
赵5	TT
*/
-Tracy-McGrady- 2013-10-21
  • 打赏
  • 举报
回复

--update就自己写吧

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 cte as(
select ROW_NUMBER()over(order by getdate()) rn,* from TableA
)
select Col01=case when isnull(Col01,'')='' 
                  then (select max(Col01) from cte b where b.rn<a.rn and isnull(b.Col01,'')>'') 
             else Col01 end
,Col02 from cte a

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

(7 行受影响)

drop table TableA
  • 打赏
  • 举报
回复
我上面写的有点问题,修改一下:

drop table tableA

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 @@servername) as rownum
from tableA
)

update t
set col01 = case when t.col01 <> ''
                      then t.col01
                 else (select top 1 col01
                       from t t1 
                       where t1.rownum < t.rownum and t1.col01 <> ''
                       order by t1.rownum desc)
            end



select *
from tableA
/*
Col01	Col02
张三	80
张三	87
张三	ABC
李四	32
赵5	83
赵5	kk
赵5	TT
*/
Andy__Huang 2013-10-21
  • 打赏
  • 举报
回复

--忘记帖结果了

create table #tb(Col01 varchar(10),Col02 varchar(10))
insert into #tb
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 cte as(
select ROW_NUMBER()over(order by getdate()) rn,* from #tb
)
select case when isnull(Col01,'')='' 
	then (select top 1 Col01 from cte b where b.rn<a.rn and b.Col01<>'' Order by rn desc) 
	else Col01 end as Col02,Col02 
from cte a

/*
Col02	Col02
张三	80
张三	87
张三	ABC
李四	32
赵5	83
赵5	kk
赵5	TT
*/
Andy__Huang 2013-10-21
  • 打赏
  • 举报
回复

create table #tb(Col01 varchar(10),Col02 varchar(10))
insert into #tb
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 cte as(
select ROW_NUMBER()over(order by getdate()) rn,* from #tb
)
select case when isnull(Col01,'')='' then (select top 1 Col01 from cte b where b.rn<a.rn and b.Col01<>'' Order by rn desc) else Col01 end,Col02 
from cte a
唐诗三百首 2013-10-21
  • 打赏
  • 举报
回复
引用 7 楼 bfofyellow 的回复:
这里不好用order by getdate()吧,如果不是按时间顺序插入呢?这个只适用于你这个新表吧?
这样用跟时间顺序没有关系,只是借此产生顺序号,其值与原表记录顺序相同. 这样写也可以的.

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 (select 0)) '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)
*/
bfofyellow 2013-10-21
  • 打赏
  • 举报
回复
引用 2 楼 ap0405140 的回复:

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)
*/
这里不好用order by getdate()吧,如果不是按时间顺序插入呢?这个只适用于你这个新表吧?
xxfvba 2013-10-21
  • 打赏
  • 举报
回复
呵呵,学习了。
  • 打赏
  • 举报
回复

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 @@servername) as rownum
from tableA
)

update t
set col01 = t2.col01
from t t1
inner join t t2
        on t1.rownum > t2.rownum + 1

select *
from tableA
/*
Col01	Col02
张三	80
赵5	87
张三	ABC
赵5	32
张三	83
赵5	kk
张三	TT
*/
-Tracy-McGrady- 2013-10-21
  • 打赏
  • 举报
回复
吃饭了,等下回来改
-Tracy-McGrady- 2013-10-21
  • 打赏
  • 举报
回复

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 cte as(
select ROW_NUMBER()over(order by getdate()) rn,* from TableA
)
select case when isnull(Col01,'')='' then (select Col01 from cte b where b.rn+2=a.rn) else Col01 end,Col02 from cte a
drop table TableA
唐诗三百首 2013-10-21
  • 打赏
  • 举报
回复

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)
*/
bfofyellow 2013-10-21
  • 打赏
  • 举报
回复
这个null值是怎么出来的?
bfofyellow 2013-10-21
  • 打赏
  • 举报
回复
引用 8 楼 ap0405140 的回复:
[quote=引用 7 楼 bfofyellow 的回复:] 这里不好用order by getdate()吧,如果不是按时间顺序插入呢?这个只适用于你这个新表吧?
这样用跟时间顺序没有关系,只是借此产生顺序号,其值与原表记录顺序相同. 这样写也可以的.

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 (select 0)) '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)
*/
[/quote] 产生顺序号这个明白。我的意思是讲你第一个代码用getdate()产生顺序号,貌似有点儿不妥当。 selec 0 就感觉好多了 因为可能钻牛角尖了 多谢斑斑回复了

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧