Transferred from: http://www.cnblogs.com/yugen/archive/2010/07/25/1784749.html
1. Determine if the data table exists
Method One:
Use Yourdb;
Go
If OBJECT_ID (n ' tablename ', n ' U ') is not null
print ' exists '
Else
print ' does not exist '
For example:
Use Fireweb;
Go
If OBJECT_ID (n ' temp_tbl ', n ' U ') is not null
print ' exists '
Else
print ' does not exist '
Method Two:
Use [instance name]
GO
IF EXISTS (SELECT * FROM dbo. SysObjects WHERE id = object_id (N ' [table name] ') and OBJECTPROPERTY (ID, ' istable ') = 1)
PRINT ' exists '
ELSE
PRINT ' does not exist '
For example:
Use Fireweb;
Go
IF EXISTS (SELECT * FROM dbo. SysObjects WHERE id = object_id (N ' temp_tbl ') and OBJECTPROPERTY (ID, ' istable ') = 1)
PRINT ' exists '
ELSE
PRINT ' does not exist '
2. Whether the temporary table exists:
Method One:
Use Fireweb;
Go
if exists (SELECT * from tempdb. sysobjects where id=object_id (' tempdb. # #TEMP_TBL '))
PRINT ' exists '
ELSE
PRINT ' does not exist '
Method Two:
Use Fireweb;
Go
if exists (SELECT * from tempdb.dbo.sysobjects where id = object_id (N ' tempdb): #TEMP_TBL ') and type= ' U ')
PRINT ' exists '
ELSE
PRINT ' does not exist '
Determine whether a table or temporary table exists in SQL Server