我正在嘗試解決t-sql練習 我需要使用id加入來更新第二個表的值。如果我不能參加,然後使用值從默認的ID(默認ID是空的Id)有趣的t-sql練習
請運行它,看看
declare @t as table (
[id] INT
,val int
)
insert into @t values (null, null)
insert into @t values (2, null)
insert into @t values (3, null)
insert into @t values (4, null)
declare @t2 as table (
[id] INT
,val int
)
insert into @t2 values (null, 11)
insert into @t2 values (2, 22)
insert into @t2 values (3, 33)
select * from @t
select * from @t2
update t
set t.val = t2.val
from @t as t join @t2 as t2
on t.id = t2.id
or
(
(t.id is null or t.id not in (select id from @t2))
and t2.id is null
)
select * from @t
這裏是導致它
[email protected]
id val
---------------
NULL NULL
2 NULL
3 NULL
4 NULL
[email protected]
id val
---------------
NULL 11
2 22
3 33
[email protected] after update
id val
---------------
NULL 11
2 22
3 33
4 NULL
如何使最後一行中的val等於11?
4 11
你說的默認的'val'是具有空'id'的行? – dotjoe 2012-04-16 21:22:00