22,300
社区成员




declare @a varchar(4)
set @a='aa'
if @a='aa'
begin
select * into #tb from...
end
else
begin
select * into #tb from...
end
//结果
#tb已存在
--如果#tb的结构相同, 先创建表结构
select top 1 * into #tb from ... where 1=0
if @a='aa'
begin
...
end
else
begin
...
end
--如果#tb的结构不相同, 那么应该创建不同名称的临时表, 就不会有这样的问题发生
--另外创建一个数据表试试
create table tt(col nvarchar(100))
declare @a varchar(4)
set @a='aa'
if @a='aa'
begin
insert into tt select * from ..
end
else
begin
insert into tt select * from ..
end
select * from tt
drop table tt
declare @a varchar(4)
set @a='aa'
create table #tb (...)
if @a='aa'
begin
insert #tb(...)
select ... into #tb from...
end
else
begin
insert #tb(...)
select ... into #tb from...
end