谁帮我看看这个语句哪里不对?

qq_37753824 搬砖  2019-05-07 02:42:17
Create table #tabl
(
Id int,
Amount float,
PId int
)
insert into #tabl values(1,100,null)
insert into #tabl values(2,200,null)
insert into #tabl values(3,null,1)
insert into #tabl values(4,null,2)

update #tabl set Amount=a.Amount
from #tabl a
where a.Id=PId
and Amount is null

select * from #tabl
drop table #tabl
为什么我这个update语句没作用?
...全文
19 点赞 收藏 3
写回复
3 条回复
qq_37753824 2019年05月07日
引用 2 楼 Dear SQL 的回复:
Create table #tabl
(
Id int,
Amount float,
PId int
)
insert into #tabl values(1,100,null)
insert into #tabl values(2,200,null)
insert into #tabl values(3,null,1)
insert into #tabl values(4,null,2)

update b set Amount=a.Amount
from #tabl a
,#tabl b
where a.Id=b.PId
and b.Amount is null

select * from #tabl
drop table #tabl
谢谢哥们 原来是这样啊。。
回复 点赞
Dear SQL(燊) 2019年05月07日
Create table #tabl
(
Id int,
Amount float,
PId int
)
insert into #tabl values(1,100,null)
insert into #tabl values(2,200,null)
insert into #tabl values(3,null,1)
insert into #tabl values(4,null,2)

update b set Amount=a.Amount
from #tabl a
,#tabl b
where a.Id=b.PId
and b.Amount is null

select * from #tabl
drop table #tabl
回复 点赞
Dear SQL(燊) 2019年05月07日


Create table #tabl
(
Id int,
Amount float,
PId int
)
insert into #tabl values(1,100,null)
insert into #tabl values(2,200,null)
insert into #tabl values(3,null,1)
insert into #tabl values(4,null,2)

update #tabl set Amount=a.Amount
from #tabl a
where a.Id=PId
and Amount is null

update b set Amount=a.Amount
from #tabl a
,#tabl b
where a.Id=b.PId
and b.Amount is null

select * from #tabl
drop table #tabl

/*
Id          Amount                 PId
----------- ---------------------- -----------
1           100                    NULL
2           200                    NULL
3           100                    1
4           200                    2

(4 行受影响)
*/
回复 点赞
发动态
发帖子
疑难问题
创建于2007-09-28

9310

社区成员

12.1w+

社区内容

MS-SQL Server 疑难问题
社区公告
暂无公告