How do you check whether the given table exists in your database or not?

There might be multiple ways to check for a table in database. Below are the few.

——————————————————————————
USE [SQLVersity_6_InterviewQ&As]
GO
——————————————————————————
IF EXISTS (
SELECT * FROM sys.tables
WHERE Name = ‘Transactions_Demo’
)
SELECT ‘Exists’

——————————————————————————
— OR
——————————————————————————
IF EXISTS (
SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(‘Transactions_Demo’)
AND Type in (‘U’)
)
BEGIN
SELECT ‘Exists’
END
——————————————————————————
— OR
——————————————————————————
IF OBJECT_ID(‘Transactions_Demo’, ‘U’) IS NOT NULL
SELECT ‘Exists’
——————————————————————————

Happy Reading :)

Leave a Reply

Your email address will not be published. Required fields are marked *