当前位置: 代码迷 >> Sql Server >> 这个update语句如何写
  详细解决方案

这个update语句如何写

热度:99   发布时间:2016-04-27 19:38:47.0
这个update语句怎么写?
create   table   #test(a   int,b   varchar(20))
insert   into   #test   select   1, 'aaa '
insert   into   #test   select   1,null
insert   into   #test   select   1,null
insert   into   #test   select   2,null
insert   into   #test   select   2, 'bbbb '
insert   into   #test   select   2,null
insert   into   #test   select   3, 'cccc '

把上表中b字段为null的update为同a字段中b的值
---------------------->
select   *   from   #test
1, 'aaa '
1, 'aaa '
1, 'aaa '
2, 'bbbb '
2, 'bbbb '
2, 'bbbb '
3, 'cccc '

这个update语句怎么写?

------解决方案--------------------
create table #test(a int,b varchar(20))
insert into #test select 1, 'aaa '
insert into #test select 1,null
insert into #test select 1,null
insert into #test select 2,null
insert into #test select 2, 'bbbb '
insert into #test select 2,null
insert into #test select 3, 'cccc '

--把上表中b字段为null的update为同a字段中b的值

update a set a.b=b.b from #test a join (select * from #test where b is not null) b on a.a=b.a

select * from #test

/*
1, 'aaa '
1, 'aaa '
1, 'aaa '
2, 'bbbb '
2, 'bbbb '
2, 'bbbb '
3, 'cccc '

--这个update语句怎么写?
*/

drop table #test
  相关解决方案