+ -
当前位置:首页 → 问答吧 → 请教临时表的问题?请给解释。

请教临时表的问题?请给解释。

时间:2011-11-29

来源:互联网

declare @i int

set @i=2

if @i=1 
begin
select * into #aa from bb 
end
else
begin
select * into #aa from cc
end 

drop table #aa



为什么执行时报错误?

数据库中已经存在名为#aa的对象

作者: sloven   发布时间: 2011-11-29

select * into 是不存在就插入 存在的话会报错。

作者: fredrickhu   发布时间: 2011-11-29

SQL code
你可以先创建临时表 再insert into..

if object_id('#tempdb..#aa') is not null
drop table #aa 
go
declare @i int

set @i=2

if @i=1  
begin
insert into #aa select * from bb  
end
else
begin
insert into #aa select * from cc 
end  


作者: fredrickhu   发布时间: 2011-11-29

你这样写sql在编译的时候,数据库认为前一个已经存在,后面同样的名字就会报错,所以编译都不通过

作者: ssp2009   发布时间: 2011-11-29

热门下载

更多