怎样用一个表的数据只更新另一个表为空的字段(NULL),字段不为空的不必更新
SQL语句表示,ADO技术比如数据库中有两个表:a,b
a表如下:
id1 id2   id3
1    1     1
2    null  2
3    4     5b表如下:
id1 id2   id3
1    2    3
2    3    4
3    6    7如果用b表更新a表
则要达到这样的效果:
id1 id2   id3
1    1     1
2    3     2
3    4     5也就是只更新字段为空的字段,其余均不更新也就是首先用SQL语句判断出哪些字段为空,再更新它 
谢谢 

解决方案 »

  1.   

    update a
    set a.id2=b.id2
    from a,b
    where a.id1=b.id1
    and a.id2 is null
      

  2.   

    如果表是这样呢
    比如数据库中有两个表:a,b
    a表如下:
    id1 id2   id3
    1    1     null
    2    null  2
    3    4     5b表如下:
    id1 id2   id3
    1    2    3
    2    3    4
    3    6    7如果用b表更新a表
    则要达到这样的效果:
    id1 id2   id3
    1    1    3
    2    3     2
    3    4     5
      

  3.   

    --楼主又穷,要求又高^^set nocount on
    declare @a table
    (
    id1 int,
    id2 int,
    id3 int
    )
    insert into @a
    select 1,1,null union all
    select 2,null,2 union all
    select 3,4,5
    declare @b table
    (
    id1 int,
    id2 int,
    id3 int
    )
    insert into @b
    select 1,2,3 union all
    select 2,3,4 union all
    select 3,6,7
    select * from @a
    select * from @b
    update a
    set 
    id2=isnull(a.id2,b.id2),
    id3=isnull(a.id2,b.id2)
    from @a a inner join @b b
    on a.id1=b.id1select * from @a
    set nocount off/*
    id1         id2         id3         
    ----------- ----------- ----------- 
    1           1           NULL
    2           NULL        2
    3           4           5id1         id2         id3         
    ----------- ----------- ----------- 
    1           2           3
    2           3           4
    3           6           7id1         id2         id3         
    ----------- ----------- ----------- 
    1           1           1
    2           3           3
    3           4           4
    */
      

  4.   

    --手误,打错一个地方,重发-_-set nocount on
    declare @a table
    (
    id1 int,
    id2 int,
    id3 int
    )
    insert into @a
    select 1,1,null union all
    select 2,null,2 union all
    select 3,4,5
    declare @b table
    (
    id1 int,
    id2 int,
    id3 int
    )
    insert into @b
    select 1,2,3 union all
    select 2,3,4 union all
    select 3,6,7
    select * from @a
    select * from @b
    update a
    set 
    id2=isnull(a.id2,b.id2),
    id3=isnull(a.id3,b.id3)
    from @a a inner join @b b
    on a.id1=b.id1select * from @a
    set nocount off/*
    id1         id2         id3         
    ----------- ----------- ----------- 
    1           1           NULL
    2           NULL        2
    3           4           5id1         id2         id3         
    ----------- ----------- ----------- 
    1           2           3
    2           3           4
    3           6           7id1         id2         id3         
    ----------- ----------- ----------- 
    1           1           3
    2           3           2
    3           4           5
    */
      

  5.   

    不是同一个数据库的表更新呢
    比如一个用access更新sql server
      

  6.   

    -_-自己察看Online的Opendatasource