SQL Server 101 – Check if a column exists in a table

If you need to check if a column exists in a table or not in SQL Server , below is a sample code snippet that demonstrates how to do it.

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

The below query checks if the column EmploymentStatusID exists in the table Employee under the schema “dbo” and returns true if it exists.

IF EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'EmploymentStatusID'
          AND Object_ID = Object_ID(N'dbo.Employee'))
BEGIN
    /* Column exists and you can perform yoour business logic */
END

Leave a Reply

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