Decrease Font Size
Increase Font Size
   BLOG

How to check if a table or view or temp table exists in SQL 2005

Here are the T-SQL to check if a table/view or temp table exists.

--View
IF OBJECT_ID('MyViewName','V') IS NOT NULL
	print 'view exists'

--with schema
IF OBJECT_ID('dbo.MyViewName','V') IS NOT NULL
	print 'view exists'

--Table
IF OBJECT_ID('MyTableName','U') IS NOT NULL
	print 'table exists'

--with schema
IF OBJECT_ID('dbo.MyTableName','U') IS NOT NULL
	print 'table exists'

--temp table
IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL
	print 'temp table exists'

Add comment

Loading