470,572 Members | 2,099 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Check to see weather the column already exists, if not add the column

111 100+
This article will explain you how to check weather a column already exists in a table before you add the column to the table using alter command.

Using the system tables you can check to see weather a column already belongs to a specific table. SYSCOLUMNS is the system table which stores all the table columns information, from this column you can check weather a specific column exists in a specific table.


if ((SELECT COUNT(*) FROM SYSCOLUMNS WHERE ID = OBJECT_ID('Table_Name') AND Name = 'Column_U_Want_To_Add') < 1 )
alter table Table_Name add Column_U_Want_To_Add DataType;

Here OBJECT_ID takes the Table name as parameter and Name, the column name you want to add.

If the count is more than zero, it indicates that the column already exist with the table. If not then you can add the statement to add the column to the table using the alter command.

Bharath Reddy VasiReddy
Sep 9 '09 #1
0 6150

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Maciej Sobczak | last post: by
4 posts views Thread by perspolis | last post: by
14 posts views Thread by John Salerno | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.