表1
A   B
1   中国
2   美国
3
4   英国
5   
6   法国
7结果表2
A   B
1   中国
2   美国
3   美国
4   英国
5   英国
6   法国
7   法国就是将表1中B列中所有是空值的记录变成是它上一行的值。

解决方案 »

  1.   

    update a
    set b = tp.b
    from table1 a,table1 tp
    where a.a = tp.a -1 and a.b is null
      

  2.   


    update 表1 t 
    set 
        B=(select top 1 B from 表1 where A<t.A and B is not null order by A desc) 
    where 
        t.B is null
      

  3.   

    declare @t1 table(A int,B varchar(10))
    insert @t1 select 
    1   ,'中国' union select
    2   ,'美国'  union select
    3 ,null union select
    4   ,'英国'  union select
    5   ,null union select
    6   ,'法国'  union select
    7 ,null
    update a 
    set b = tp.b 
    from @t1 a,@t1 tp 
    where a.a = tp.a +1 and a.b is nullselect * from @t1
    /*
    A           B          
    ----------- ---------- 
    1           中国
    2           美国
    3           美国
    4           英国
    5           英国
    6           法国
    7           法国(所影响的行数为 7 行)*/
      

  4.   

    --> 测试数据: #1
    if object_id('tempdb.dbo.#1') is not null drop table #1
    create table #1 (A int,B varchar(4))
    insert into #1
    select 1,'中国' union all
    select 2,'美国' union all
    select 3,null union all
    select 4,'英国' union all
    select 5,null union all
    select 6,'法国' union all
    select 7,nullselect A, B=
    case
    when B is not null then B
    else (select top 1 B from #1 where A <= a.A and B is not null order by A desc)
    end
    from #1 a/*
    A   B
    1   中国
    2   美国
    3   美国
    4   英国
    5   英国
    6   法国
    7   法国
    */
      

  5.   

    select 
    A,B=isnull((select top 1 B from T where A<a.A order by ID desc),B)--为null时
    from 
    T aselect 
    A,B=isnull(nullif((select top 1 B from T where A<a.A order by ID desc),''),B)--为''时
    from 
    T a
      

  6.   

    declare @t1 table(A int,B varchar(10))
    insert @t1 select 
    1   ,'中国' union select
    2   ,'美国'  union select
    3 ,null union select
    4   ,'英国'  union select
    5   ,null union select
    6   ,'法国'  union select
    7 ,null
    ---1
    update a 
    set b = isnull(a.b,tp.b )
    from @t1 a,@t1 tp 
    where a.a = tp.a +1 
    --2
    update a 
    set b = tp.b 
    from @t1 a,@t1 tp 
    where a.a = tp.a +1 and a.b is null--3
    update a 
    set b = COALESCE(a.b,tp.b )
    from @t1 a,@t1 tp 
    where a.a = tp.a +1 
    select * from @t1
    /*
    A           B          
    ----------- ---------- 
    1           中国
    2           美国
    3           美国
    4           英国
    5           英国
    6           法国
    7           法国(所影响的行数为 7 行)*/
      

  7.   

    在where条件上约束以下就行了,你有id列。
      

  8.   


    create table #(id int,cName nvarchar(10))
    insert # select 
    1   ,'中国' union select
    2   ,'美国'  union select
    3 ,null union select
    4   ,'英国'  union select
    5   ,null union select
    6   ,'法国'  union select
    7 ,null
    select * from #update # set cName= 
     (
    select top 1 Last.cName from # Last 
    where Last.id<#.id
    and (Last.cName is not null or len(Last.cName)<>0) 
    order by Last.id desc 
     ) 
    where cName is null or len(cName)=0 select * from #
    drop table #