Check if table exists in SQL Server Check if table exists in SQL Server sql-server sql-server

Check if table exists in SQL Server


For queries like this it is always best to use an INFORMATION_SCHEMA view. These views are (mostly) standard across many different databases and rarely change from version to version.

To check if a table exists use:

IF (EXISTS (SELECT *                  FROM INFORMATION_SCHEMA.TABLES                  WHERE TABLE_SCHEMA = 'TheSchema'                  AND  TABLE_NAME = 'TheTable'))BEGIN    --Do StuffEND


Also note that if for any reason you need to check for a temporary table you can do this:

if OBJECT_ID('tempdb..#test') is not null --- temp table exists


We always use the OBJECT_ID style for as long as I remember

IF OBJECT_ID('*objectName*', 'U') IS NOT NULL