MS SQL Tips & Tricks

How to test field existance in a particular table

If You want to alter table and add column unless it already exists You just need to check then INFORMATION_SCHEMA.COLUMNS view. For example:

if not exists ( 
	SELECT * from INFORMATION_SCHEMA.COLUMNS 
	where table_name='Dict' and column_name='Description')
begin
   print 'Adding field Description to Dict Table';
   ALTER TABLE  Dict ADD Description varchar(128)
end;

Learn more tricks

Add Comment

Comments