表变量与临时表,更新语句在语法上为何有这样的区别呢?

chouqin680 2007-12-01 04:51:42
表变量与临时表,更新语句在语法上为何有这样的区别呢?

declare @a table (
id int,
num int
)

insert into @a
select 1,100
union all select 2,200
union all select 3,400

declare @b table (
id int,
num2 int
)

insert into @b
select 1,50
union all select 2,80
union all select 3,110

--下面写法就会报错
update @a set @a.num=@a.num-@b.num2
from @a inner join @b on (@a.[id]=@b.[id])

--写面下列写法就正确,不知为什么,必须另外指定别名才行
update a set a.num=a.num-b.num2
from @a a inner join @b b on (a.[id]=b.[id])

...全文
79 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Limpire 2007-12-02
  • 打赏
  • 举报
回复
不知为什么
---------
不为什么,MS不让你这么干
cxmcxm 2007-12-01
  • 打赏
  • 举报
回复
加上别名用于减少二义性,也简化了sql server的编译难度
netcup 2007-12-01
  • 打赏
  • 举报
回复
楼上说的有道理!
hb_gx 2007-12-01
  • 打赏
  • 举报
回复
这是语法问题

毕竟 @a 是一个变量,试想能用变量当别名吗?



chouqin680 2007-12-01
  • 打赏
  • 举报
回复
表变量与临时表,更新语句在语法上为何有这样的区别呢?

declare @a table (
id int,
num int
)

insert into @a
select 1,100
union all select 2,200
union all select 3,400

declare @b table (
id int,
num2 int
)

insert into @b
select 1,50
union all select 2,80
union all select 3,110

--下面写法就会报错
update @a set @a.num=@a.num-@b.num2
from @a inner join @b on (@a.[id]=@b.[id])

--写面下列写法就正确,不知为什么,必须另外指定别名才行
update a set a.num=a.num-b.num2
from @a a inner join @b b on (a.[id]=b.[id])


chouqin680 2007-12-01
  • 打赏
  • 举报
回复
表变量与临时表,更新语句在语法上为何有这样的区别呢?

declare @a table (
id int,
num int
)

insert into @a
select 1,100
union all select 2,200
union all select 3,400

declare @b table (
id int,
num2 int
)

insert into @b
select 1,50
union all select 2,80
union all select 3,110


--下面写法就会报错
update @a set @a.num=@a.num-@b.num2
from @a inner join @b on (@a.[id]=@b.[id])

--写面下列写法就正确,不知为什么,必须另外指定别名才行
update a set a.num=a.num-b.num2
from @a a inner join @b b on (a.[id]=b.[id])



chouqin680 2007-12-01
  • 打赏
  • 举报
回复
表变量与临时表,更新语句在语法上为何有这样的区别呢?

declare @a table (
id int,
num int
)

insert into @a
select 1,100
union all select 2,200
union all select 3,400

declare @b table (
id int,
num2 int
)

insert into @b
select 1,50
union all select 2,80
union all select 3,110



--下面写法就会报错
update @a set @a.num=@a.num-@b.num2
from @a inner join @b on (@a.[id]=@b.[id])

--写面下列写法就正确,不知为什么,必须另外指定别名才行
update a set a.num=a.num-b.num2
from @a a inner join @b b on (a.[id]=b.[id])




34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧