判断MS SQLSERVER临时表是否存在
发布时间:2020-12-12 14:55:45 所属栏目:MsSql教程 来源:网络整理
导读:drop table? #tempcitys select * into #tempcitys from hy_citys 上面的语句第一次运行的时候就肯定出错了,但第二次就不会。 因为select * into #tempcitys from hy_citys自动创建了临时表#tempcitys ,第一次临时表不存在,drop table自然就出错了。 刚开
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) 原来我是搞混了。学艺不精的结果。:(
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- SQL Server 收缩后对数据库的使用有影响吗?
- sql-server – 有人可以解释DBCC DROPCLEANBUFFERS吗?
- sql – 选择每个的最大值(Col1,Col2)
- sql-server – 无论如何为“SELECT”语句创建SQL Server DD
- sql-server – 如何保持对表的更改的审计/历史记录
- 理解SQLSERVER中的排序规则,解决无法正确显示中文字符的问
- [ASPNET2.0]Membership类+SQLServer2005,AspNet_regsql.ex
- SQLServer 新增数据时触发器自动计算并更新相关值
- SQLSERVER 本地查询更新操作远程数据库的代码
- SqlServer删除所有存储过程和所有表、查询表是否存在指定的