SQL判断临时表是否存在 - TOMMYHU - 专注互联网开发及运营技术,提供相关资料及软件下载,奇趣网络时事评论!
Jun 26

SQL判断临时表是否存在 不指定

tommyhu , 20:42 , DBA , Comments(0) , Trackbacks(0) , Reads(3329) , Via Original Large | Medium | Small
判断临时表是否存在
Way 1
if(exists(select name from tempdb..sysobjects where name like'%temptab%' and type='U'))
   drop table #temptab
Way 2
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempcitys') and type='U')
   drop table #tempcitys
Way 3
IF OBJECT_ID('tempdb..#') IS NOT NULL
   DROP TABLE #
OBJECT_ID此函数返回数据库对象标识号

判断数据库里有没有存在PerPersonData这样一张表
if exists (select * from sysobjects where objectproperty(object_id('PerPersonData'),'istable') = 1)
OBJECTPROPERTY:返回当前数据库中对象的有关信息。1表“真”。同样可以写成OBJECTPROPERTY(id, isUserTable) = 1
if exists (select * from sysobjects where id = object_id(N'PerPersonData') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table 'PerPersonData'

判断试图是否存在
if exists (select * from sysobjects where id = object_id(N‘[dbo].[ESTMP]‘)
and OBJECTPROPERTY(id, N‘IsView‘) = 1)
  drop view ESTMP

更多:http://blog.csdn.net/leamonjxl/article/category/831506
▲返回顶部
Last modified by tommyhu on2012/06/26 20:43

Add a comment

Nickname

emotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemot