判断MS SQLSERVER临时表是否存在-smeyou
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> ADP及SQL SERVER


判断MS SQLSERVER临时表是否存在

发表时间:2013/9/30 9:26:28 评论(0) 浏览(5780)  评论 | 加入收藏 | 复制
   
摘 要:判断MS SQLSERVER临时表是否存在
正 文:
drop table  #tempcitys
 select * into #tempcitys from hy_citys
 
上面的语句第一次运行的时候就肯定出错了,但第二次就不会。
 因为select * into #tempcitys from hy_citys自动创建了临时表#tempcitys ,第一次临时表不存在,drop table自然就出错了。
 刚开始没反应过来,select * into是会自动创建临时表的。
 
所以比较可靠的做法,还是先判断临时表是否存在,然后再drop table
 if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempcitys') and type='U')
    drop table #tempcitys
 
注意tempdb后面是两个. 不是一个的
 
考虑另外一种SQL的写法
 insert into #tempcitys(cityid) select cityid from hy_citys
 这种写法,#tempcitys则不会被自动创建,要使用#tempcitys,则需要先create table #tempcitys(cityid int)
 
原来我是搞混了。学艺不精的结果。:(

Access软件网交流QQ群(群号:198465573)
 
 相关文章
[access查询]库存计算的临时表不需设主键  【  2007/11/10】
access的真假:四、"SELECT * INTO 工作表 FR...  【ACMAIN_CHM  2009/9/11】
SQLServer中判断是否存在满足条件的记录  【金宇  2012/12/31】
【源码示例】生成Access表结构说明书至本地临时表  【金宇  2013/4/1】
关于临时表的回收处理  【Aaron  2013/7/10】
sqlserver中判断表或临时表是否存在  【平常心  2013/7/22】
常见问答
技术分类
相关资源
文章搜索
关于作者

smeyou

文章分类

文章存档

友情链接