How to drop a table in SQL Server 2008 only if exists


Our company is migrating one of its products from SQL Server 2005 to 2008, and in the logs I've noticed excess of errors about deleting tables if it does not exist. In SQL Server 2005 we had this to delete tables

IF OBJECT_ID('dbo.Units', 'U') IS NOT NULL
   DROP TABLE dbo.Units

which doesn't seem to work anymore.

What would be the proper way of deleting a table only if it exists in SQL Server 2008?


Answers:


This should do it!

IF EXISTS 
        (SELECT 
             TABLE_NAME 
         FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TableToDrop') 
DROP TABLE TableToDrop