怎样用一个表的数据只更新另一个表为空的字段(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语句判断出哪些字段为空,再更新它
谢谢
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语句判断出哪些字段为空,再更新它
谢谢
set a.id2=b.id2
from a,b
where a.id1=b.id1
and a.id2 is null
比如数据库中有两个表: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
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
*/
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
*/
比如一个用access更新sql server