Check whether a Table exists in SQL Server database Example

Let us assume we have a database name “SQL_DBA” and we need to create a new table “geek_demo” –

Query:

USE [SQL_DBA]
GO
IF OBJECT_ID('geek_demo', 'U') IS NOT NULL
BEGIN
PRINT 'Table exists.'
END
ELSE
BEGIN
PRINT 'Table does not exist.'
END

Output:

Table does not exists.

Let us create the table.

Query

CREATE TABLE geek_demo (id int, name varchar(200));

Now, let us check whether the table is created or not –

Query

USE [DB_NAME]
GO
IF EXISTS(SELECT 1 FROM sys.Objects
WHERE Object_id = OBJECT_ID(N'table_name')
AND Type = N'U')
BEGIN
PRINT 'Table exists.'
END
ELSE
BEGIN
PRINT 'Table does not exist.'
END

Output

Table exists.

Check whether a Table exists in SQL Server database or not

Before creating a table, it is always advisable to check whether the table exists in the SQL Server database or not. Checking for table existence before creation helps in avoiding duplication errors, ensures data integrity, and enables efficient database management.

There are multiple methods in SQL Server to check if a table already exists in a database. Here, we will discuss these methods and learn the .

Similar Reads

How to Check if a Table Already Exists in SQL Server

To check if a table already exists in the SQL Server database, use these methods:...

Check whether a Table exists in SQL Server database Example

Let us assume we have a database name “SQL_DBA” and we need to create a new table “geek_demo” –...

Contact Us