1、
例如表:
id name sex tel qq time
1 李先生 null 15088888888 null 2013-4-10
更新数据 null 男 150666666666 123456 null
得到结果
id name sex tel qq time
1 李先生 男 15088888888 123456 2013-4-10------------------------------------------------------------------------------------------------2、
例如表:
id name sex tel qq time
1 李先生 null 15088888888 null 2013-4-10
更新数据 null 男 111111111 123456 null得到结果
id name sex tel qq time
1 李先生 男 111111111 123456 2013-4-10
例如表:
id name sex tel qq time
1 李先生 null 15088888888 null 2013-4-10
更新数据 null 男 150666666666 123456 null
得到结果
id name sex tel qq time
1 李先生 男 15088888888 123456 2013-4-10------------------------------------------------------------------------------------------------2、
例如表:
id name sex tel qq time
1 李先生 null 15088888888 null 2013-4-10
更新数据 null 男 111111111 123456 null得到结果
id name sex tel qq time
1 李先生 男 111111111 123456 2013-4-10
set a.字段1= case when a.字段1 is null then b.字段1 else a.字段1 end ,把其他字段写上
from tb a inner join 用于更新的表,假设为tb2 b on a.id= b.id
2、
例如表:
id name sex tel qq time
1 李先生 null 15088888888 null 2013-4-10
更新数据 在表中这里是否会有name和上面的对应?或者说这行的id和上面那个是一样的 null 男 111111111 123456 null
-------------------------------------------------------------------------------------
表名([text].[dbo].[case])
例如表:
id name sex tel qq time
1 李先生 null 15088888888 null 2013-4-10
UPDATE [text].[dbo].[case]
SET [name] ='',[sex] = '男',[tel] = '150666666666',[qq] = '123456',[time] = ''
WHERE id=1
得到结果:id name sex tel qq time
1 李先生 男 15088888888 123456 2013-4-10
2、
select id,name,max(sex)sex,max(tel)tel,max(qq)qq,max(time)time into #t
from tb
group by id,name
3、truncate tale tb
4、insert into tb select * from #t
(
id int primary key,
name nvarchar(20),
sex nvarchar(20),
tel nvarchar(20),
qq nvarchar(20),
time datetime
)go
insert into myuser values(1,'李先生',null,'15088888888',null,'2013-4-10')go
select * from myuser
select * from myuser1
go
--修改不是空值的数据
--begin transactionupdate a
set a.name= case when a.name is null then b.name else a.name end,
a.sex= case when a.sex is null then b.sex else a.sex end,
a.tel= case when a.tel is null then b.tel else a.tel end,
a.qq= case when a.qq is null then b.qq else a.qq end,
a.time= case when a.time is null then b.time else a.time end
from myuser a inner join myuser1 b on a.id= b.id--rollback transaction
select * from myuser
select * from myuser1
--只有不是空就更新
--begin transaction
update a
set a.name= case when b.name is not null then b.name else a.name end,
a.sex= case when b.sex is not null then b.sex else a.sex end,
a.tel= case when b.tel is not null then b.tel else a.tel end,
a.qq= case when b.qq is not null then b.qq else a.qq end,
a.time= case when b.time is not null then b.time else a.time end
from myuser a inner join myuser1 b on a.id= b.id
id name addr
----------- -------------------------------------------------- ----------
1 a,b,c,d NULL
2 NULL 234declare @name nvarchar(10);
declare @addr nvarchar(10);
set @name = 'wokaoa';
set @addr = 'xiamen';
update test set name=ISNULL(name,@name),addr=ISNULL(addr,@addr);select * from test
id name addr
----------- -------------------------------------------------- ----------
1 a,b,c,d xiamen
2 wokao 234
(1)只更新字段是NULL的,非空的不更新。
(2)字段是NULL要更新,有的字段比如TEL,虽然不为空,但是也要更新了。
是这样吗?
上一个是对的哦,敢问这种情况怎么实现的
表名([text].[dbo].[case])
例如表:
id name sex tel qq time
1 李先生 null 15088888888 null 2013-4-10
UPDATE [text].[dbo].[case]
SET [name] ='',[sex] = '男',[tel] = '1111111',[qq] = '123456',[time] = ''
WHERE id=1
得到结果:id name sex tel qq time
1 李先生 男 1111111 123456 2013-4-10