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
List All Databases in SQL Server In SQL Server, databases are crucial for storing and managing data efficiently. Whether we are managing a large enterprise system or a small application, understanding how to list all the databases on our SQL Server is essential. In this article, we will write SQL queries that help us to retrieve al
3 min read
How to List All Tables in a Schema in Oracle Database? In Oracle Database, listing all tables within a schema can be crucial for database management and analysis. we can use specific queries to retrieve information about tables in our schema. Below, we explore various queries to list tables, focusing on the SYSOBJECTS view that provides essential metada
3 min read
Create Database in MS SQL Server Databases in Microsoft SQL Server are crucial for managing data, categorized into system databases, which are auto-created and user databases, created by users. In this article, We will learn about the basics of system and user databases along with methods for creating and managing them using T-SQL
5 min read
Delete Database in MS SQL Server Prerequisite â Introduction of MS SQL Server and Create Database in MS SQL Server System databases can't be deleted, only user databases could be deleted. Data and log files will automatically be deleted from disk with database deletion. To delete a database, the below methods could be used â SQL Se
1 min read
Create a Table if it Doesn't Exist in SQL Creating tables is a fundamental part of database management and sometimes it is important to ensure a table doesnât already exist before creating it. The CREATE TABLE IF NOT EXISTS statement in SQL provides a simple and effective way to handle such situations prevent errors and simplify database ma
4 min read