Check whether a Table exists in SQL Server database or not
Last Updated :
28 May, 2024
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 .
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:
- Using the OBJECT_ID and the IF ELSE statement
- Using the sys.Objects
- Using the sys.Tables
- Using the INFORMATION_SCHEMA.TABLES and SQL EXISTS Operator
Using the OBJECT_ID and the IF ELSE statement to check whether a table exists or not
SQL OBJECT_ID function returns the database object identification number if the object exists. By pairing it with the IF ELSE statement, we can check if a table with the same name already exists in the SQL Server.
Syntax:
USE [DB_NAME]
GO
IF OBJECT_ID('table_name', 'U') IS NOT NULL
BEGIN
PRINT 'Table exists.'
END
ELSE
BEGIN
PRINT 'Table does not exist.'
END
Using the INFORMATION_SCHEMA.TABLES and SQL EXISTS Operator to check whether a table exists or not
The INFORMATION_SCHEMA.TABLES is a system view that contains metadata about all tables in the database. Using EXISTS operator we can check if a table already exists in a database.
Syntax:
USE [DB_NAME]
GO
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'table_name')
BEGIN
PRINT 'Table exists.'
END
ELSE
BEGIN
PRINT 'Table does not exist.'
END
Using the sys.Objects and SQL EXISTS Operator to check whether a table exists in SQL Server or not
The sys.Objects is a system view that contains every user-defined object created within the database. By pairing it with the EXISTS operator, we can verify if the table already exists in the SQL Server database.
Syntax:
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 does not exists.
Using the sys.Tables to check whether a table exists or not
sys.Tables is a system view, that contains each table in the current database. Using it, we can check if a table already exists in the SQL Server Database.
Query :
USE [DB_NAME]
GO
IF EXISTS(SELECT 1 FROM sys.Tables
WHERE Name = N'table_name')
BEGIN
PRINT 'Table exists.'
END
ELSE
BEGIN
PRINT 'Table does not exist.'
ENDthe
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.
Similar Reads
Check the Dependencies of a Table in SQL Server Sometimes there is a need to find the dependencies of a table in SQL Server using SQL Server Management Studio or SQL Query. It is useful to have information about the dependencies while altering or dropping any table. In this article, we will learn how to find table dependencies in SQL Server using
2 min read
How to Check if a Column Exists in a SQL Server Table? In this article, we will look at how to check if a particular column exists in a database table or not. For checking the existence of a column we need to create the table first. So, let us create a table with some columns and data. Creating table: Syntax: CREATE TABLE table_name ( column1 datatype,
2 min read
Copy tables between databases in SQL Server using Import-and-Export Wizard Introduction to Import-and-Export Wizard : The wizard allows easy steps through a process, and to execute the data copy process with writing very little or no code. However, for importing and exporting data from one source into another, the below steps could be followed - Open the Object Explorer, s
2 min read
Copy Tables Between Databases In SQL Server Copying tables between databases in SQL Server can be crucial for data migration, backups, or setting up test environments. One effective method to achieve this is by generating scripts using SQL Server Management Studio (SSMS).In this article, we will learn how to Copy Tables Between Databases In S
3 min read
List the available Databases for Current User in SQL SERVER Introduction : One of the pre-needful of Database Performance Health Check is to have access to the database which we're going to tune. As SQL DBAs we can also additionally discover it unexpected that quite sometimes, we ended up in a scenario in which we've got a customer who needs us to assist wit
2 min read