Quantcast
Channel: SQL Server.pro
Viewing all articles
Browse latest Browse all 19

How to check if a table exists in SQL Server database

$
0
0

There are several methods to check if a table exists in SQL Server database.

Using INFORMATION_SCHEMA.TABLES

Query like below can be used to check if a Table exists in the selected mssql database.

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'YouTableName')
BEGIN
   PRINT 'Table Exists'
END

Using OBJECT_ID() function

OBJECT_ID() can be used to confirm existence of a table.

IF OBJECT_ID(N'dbo.MyTableName', N'U') IS NOT NULL
BEGIN
  PRINT 'Table Exists'
END

Using sys.Objects or sys.Tables Catalog View

Sys.Objects and sys.Tables catalog views can also be used to check if a table exist or not.

IF EXISTS(SELECT 1 FROM sys.Objects WHERE Object_id = OBJECT_ID(N'dbo.MyTableName') AND Type = N'U')
BEGIN
   PRINT 'Table Exists'
END

 

IF EXISTS(SELECT 1 FROM sys.Tables WHERE Name = N'MyTableName' AND Type = N'U')
BEGIN
PRINT 'Table Exists'
END

The post How to check if a table exists in SQL Server database appeared first on SQL Server.pro.


Viewing all articles
Browse latest Browse all 19

Trending Articles