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
...全文
19611打赏收藏
临时表添加一列后,不能使用该列的列名?奇怪!
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
alter table #tmp add [d] int
在上面这一句之后加GO
和使用EXEC的作用是一样的,都会产生一个"批"(batch)
一个批,会作为一个 execution plan来处理,所以查询处理器在处理select d from #tmp的时候,表#temp里并没有d这个字段,所以会报错,这就是原因
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
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