日期:2014-05-18 浏览次数:20584 次
go
if OBJECT_ID('tbl')is not null
drop table tbl
go
create table tbl(
A varchar(3),
B varchar(3)
)
go
insert tbl
select 'aaa',null union all
select null,'bbb' union all
select null,null
--
declare @str varchar(3)
set @str='bbb'
update tbl
set A=case when A is null then @str
when A is not null and B is null then A else A end,
B=case when A is null then B
when A is not null and B is null then @STR else B end
select * from tbl
/*
A B
aaa bbb
bbb bbb
bbb NULL
*/
------解决方案--------------------
go
if OBJECT_ID('tbl')is not null
drop table tbl
go
create table tbl(
A varchar(3),
B varchar(3)
)
go
insert tbl
select 'aaa',null union all
select null,'bbb' union all
select null,null
--测试
declare @str varchar(3)
set @str='new'
update tbl
set A=case when A is null then @str
when A is not null and B is null then A else A end,
B=case when A is null then B
when A is not null and B is null then @STR else B end
select * from tbl
/*
A B
-------------
aaa new
new bbb
new NULL
*/
这样看得明显点,注意对比两个字段的值