HomeSQL ServerSQL Server 101 – Check if a column exists in a table

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

You May Also Like

When dealing with a relational database management system (RDBMS) like SQL Server, compatibility level is an important concept to understand....
In this blog post, let’s learn about the error message “49975 – Unable to load controller client certificate due to...
In this blog post, let’s learn about the error message “49973 – Cannot remove tempdb remote file to local tempdb...