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 *

You May Also Like

In this blog post, let’s learn about the error message “1459 – An error occurred while accessing the database mirroring...
In this blog post, let’s learn about the error message “7937 – Columnstore index has one or more missing column...