帮忙看看为什么sql2000老提示我已存在临时表对象呢?

liquidcn 2009-03-18 04:11:04
存储过程:

if .....
select * into #t from tab where....
else if ....
select * into #t from tab where...
else if ...
select * into #t from tab where...

select .... from #t where ...

drop table #t


我想根据一些条件创建不同的临时表(名字都一样),最后再对临时表进行查询,但数据库老是报:there is already an object named "#t" in the database

我已经无解了,望高人指教
...全文
74 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2009-03-18
  • 打赏
  • 举报
回复
declare @s varchar(8000)
if .....
set @s = '...'
else if ....
set @s = '...'
else if ...
set @s = '...'
exec('select * into tmp from .. where '+@s)

select .... from tmp where ...

drop table tmp
liquidcn 2009-03-18
  • 打赏
  • 举报
回复
ok,thanks
水族杰纶 2009-03-18
  • 打赏
  • 举报
回复
if   object_id( 'tempdb..#t ')   is   not   null 
drop table #t
GO
CREATE TABLE #T (ID INT ,.....)
if .....
INSERT #t SELECT * from tab where....
else if ....
INSERT #t SELECT * from tab where...
else if ...
INSERT #t SELECT * from tab where...

select .... from #t where ...

drop table #t
liquidcn 2009-03-18
  • 打赏
  • 举报
回复
拜托楼上讲详细点,急啊
playwarcraft 2009-03-18
  • 打赏
  • 举报
回复
这样编译过不了的,还是用create #t...再insert吧。。
liquidcn 2009-03-18
  • 打赏
  • 举报
回复
就是这样的:

if object_id('tempdb..#t') is not null
drop table #t

if .....
select * into #t from tab where....
else if ....
select * into #t from tab where...
else if ...
select * into #t from tab where...

select .... from #t where ...

drop table #t

仍然报错啊……
dawugui 2009-03-18
  • 打赏
  • 举报
回复
if object_id('tempdb..#t') is not null drop table #t
playwarcraft 2009-03-18
  • 打赏
  • 举报
回复
create table #t(...)
if ..
insert into #t select ..
htl258_Tony 2009-03-18
  • 打赏
  • 举报
回复
已经有一个对象命名为“ # T ”型的数据库中
if object_id('tempdb..#t') is not null drop #t
if .....
select * into #t from tab where....
else if ....
select * into #t from tab where...
else if ...
select * into #t from tab where...

select .... from #t where ...

drop table #t
肥龙上天 2009-03-18
  • 打赏
  • 举报
回复

更改为
select * into #1 from tab where 1 =2

if ...
insert #1 select * from tab where
else if ...
insert #1 select * from tab where


drop table #1

水族杰纶 2009-03-18
  • 打赏
  • 举报
回复
IF OBJECT_ID('TEMPDB..#t')IS NOT NULL DROP TABLE #T

22,181

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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