临时表添加一列后,不能使用该列的列名?奇怪!

YiYanXiYin 2004-07-27 12:57:30
select 2 a ,3 b ,'abcc' c into #tmp
alter table #tmp add [d] int
insert into #tmp(a,b,c) select 55,22,'aabb' --怎样给d字段添加数据
select * from #tmp --怎样才能select d from #tmp
drop table #tmp
...全文
196 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
evlon 2004-07-27
  • 打赏
  • 举报
回复
学习 UP
netcoder 2004-07-27
  • 打赏
  • 举报
回复
alter table #tmp add [d] int
在上面这一句之后加GO
和使用EXEC的作用是一样的,都会产生一个"批"(batch)
一个批,会作为一个 execution plan来处理,所以查询处理器在处理select d from #tmp的时候,表#temp里并没有d这个字段,所以会报错,这就是原因
1老玉米1 2004-07-27
  • 打赏
  • 举报
回复
是要用exec 否则那一句是不执行的
yesterday2000 2004-07-27
  • 打赏
  • 举报
回复
UP
zilang 2004-07-27
  • 打赏
  • 举报
回复
在alter后加go 就行了,
YiYanXiYin 2004-07-27
  • 打赏
  • 举报
回复
为什么要这样呢?奇怪!
zjcxc 元老 2004-07-27
  • 打赏
  • 举报
回复
select 2 a ,3 b ,'abcc' c into #tmp
exec('alter table #tmp add [d] int') --这里用exec
insert into #tmp(a,b,c) select 55,22,'aabb' --怎样给d字段添加数据
select * from #tmp --怎样才能select d from #tmp
drop table #tmp
pjy 2004-07-27
  • 打赏
  • 举报
回复
用动态SQL语句,EXEC吧!
friendliu 2004-07-27
  • 打赏
  • 举报
回复
insert into #tmp(a,b,c,d) select 55,22,'aabb','11'
这样不就可以了吗??

select d from @#tmp
都可以呀
WangZWang 2004-07-27
  • 打赏
  • 举报
回复
select 2 a ,3 b ,'abcc' c into #tmp
alter table #tmp add [d] int
exec('insert into #tmp(a,b,c,d) select 55,22,''aabb'',33') --怎样给d字段添加数据
exec('select d from #tmp ')--怎样才能select d from #tmp
drop table #tmp
aierong 2004-07-27
  • 打赏
  • 举报
回复
增加列后请用

GO

34,590

社区成员

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

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