Adding a new column to a MS SQL Table safely

April 8th, 2008 admin Posted in sql | 1 Comment »

How could we check if a column exists and add it to a table if it doesn't? Very easily!

 
IF NOT EXISTS
	(SELECT * FROM syscolumns
	WHERE id = OBJECT_ID(N'[MyTable]') AND name = 'MyColumn')
             ALTER TABLE dbo.MyTable ADD MyColumn INT

Enjoy!

One Response to “Adding a new column to a MS SQL Table safely”

  1. Nice little tip.

Leave a Reply

You must be logged in to post a comment.