Check table exists MS SQL

This query is useful to check the table exists or not. as best practices before deleting or dropping a table you must check the table already exists or not.

Syntax:

Best Way


IF OBJECT_ID (N'[table-name]', N'U') IS NOT NULL 
BEGIN
	-- action
END

Second Way


IF EXISTS(SELECT TOP 1 1 FROM [table-name]) 
BEGIN
	-- action
END

 

Example:

Best Way


IF OBJECT_ID (N'tblCustomers', N'U') IS NOT NULL 
BEGIN
	SELECT 1 AS Res
END

Second Way


IF EXISTS(SELECT TOP 1 1 FROM tblCustomers) 
BEGIN
	SELECT 1 AS Res
END

Summary: