SQL判斷臨時表是否存在
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