怎么样判断一个临时表已经存在???

beggy 2003-09-24 01:58:33
在一个存储过程中这样写:

if Exists(tempTbl)
insert into #tempTbl select * from 表1 where 字段1='a'
else
select * into #tempTbl from 表1 where 字段1='b'

为什么提示说 tempTbl 已经存在
把tempTbl 全部替换为 mytempTbl 也是一样提示 mytempTbl 已经存在

...全文
59 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
beggy 2003-09-25
  • 打赏
  • 举报
回复
上面的做法是正确的,可是当表中存在一个标识列时,

insert #tempTbl select * from 表1 where 字段1='a'

提示:
仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#temptbl' 中为标识列 指定显式值。
LoveSQL 2003-09-25
  • 打赏
  • 举报
回复
if object_id('tempdb..#tempTbl') is not null
print '存在'
else
print '不存在'
nboys 2003-09-25
  • 打赏
  • 举报
回复
if object_id('tempdb..#tempTbl') is not null
insert #tempTbl select * from 表1 where 字段1='a'
else
select * into #tempTbl from 表1 where 字段1='b'
aierong 2003-09-25
  • 打赏
  • 举报
回复
if object_id('tempdb..temptablename') is not null
print 'exists'
railgunman 2003-09-25
  • 打赏
  • 举报
回复
pengdali(大力 V3.0)

厉害!
pengdali 2003-09-24
  • 打赏
  • 举报
回复
if object_id('tempdb..#tempTbl') is not null
insert #tempTbl select * from 表1 where 字段1='a'
else
select * into #tempTbl from 表1 where 字段1='b'
yujohny 2003-09-24
  • 打赏
  • 举报
回复
if object_id(tempTbl ) is not null
insert into #tempTbl select * from 表1 where 字段1='a'
else
select * into #tempTbl from 表1 where 字段1='b'
txlicenhe 2003-09-24
  • 打赏
  • 举报
回复
if Exists(select * from tempdb..sysobjects where name = '#tempTbl1')
print '存在'
else
print '不存在'

22,209

社区成员

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

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