This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing two options for checking whether a column exists in a SQL Server table or not.
When you are preparing the full database change script, you should put DDL statements in the IF EXISTS condition for avoiding any error.
Option 1: Check the column existence and add id
1 2 3 4 5 6 7 8 9 |
if not exists ( select column_name from INFORMATION_SCHEMA.columns where table_name = 'table_name' and column_name = 'new_column_name' ) ALTER TABLE table_name add new_column_name TEXT |
Option 2: Check the column existance
1 2 3 4 |
IF COL_LENGTH('table_name','column_name') IS NULL BEGIN /*Write your code...*/ END |
Leave a Reply