How to check if a column exists in a SQL Server table

By | June 11, 2018

How to check if a column exists in a SQL Server table,

Syntax:

IF EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'columnName'
          AND Object_ID = Object_ID(N'schemaName.tableName'))
BEGIN
    -- Column is exists in DB
END

Example:

Let’s Check wether address column is present in Employee table or not.

IF EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'address'
          AND Object_ID = Object_ID(N'dbo.Employee'))
BEGIN
    -- Column is exists in DB
END

 

Leave a Reply

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