IF NOT EXISTS(SELECT TOP 1 1FROM INFORMATION_SCHEMA.COLUMNSWHERE[TABLE_NAME] = 'Employees'AND [COLUMN_NAME] = 'EmployeeID')BEGINALTER TABLE [Employees]ADD [EmployeeID] INT NULLEND
if not exists (selectcolumn_namefromINFORMATION_SCHEMA.columnswheretable_name = 'MyTable'and column_name = 'MyColumn')alter table MyTable add MyColumn int
IF (OBJECT_ID(N'[dbo].[myTable]') IS NOT NULL ANDCOLUMNPROPERTY( OBJECT_ID(N'[dbo].[myTable]'), 'ThisColumnDoesNotExist', 'ColumnId') IS NULL)BEGINSELECT 'Column does not exist -- You can add TSQL to add the column here'END
if exists (select *from INFORMATION_SCHEMA.COLUMNSwhere TABLE_NAME = '<table_name>'and COLUMN_NAME = '<column_name>') beginprint 'Column you have specified exists'end else beginprint 'Column does not exist'end
IF NOT EXISTS (SELECT *FROM sys.ColumnsWHERE Name = N'QbId'AND Object_Id = Object_Id(N'Driver'))BEGINALTER TABLE Driver ADD QbId NVARCHAR(20) NULLENDELSEBEGINPRINT 'QbId is already added on Driver'END
IF(SELECT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'TableName' AND COLUMN_NAME = 'ColumnName') IS NOT NULLPRINT 'Column Exists in the given table';
USE [Northwind]GO
IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME = 'Categories'AND COLUMN_NAME = 'Note')BEGIN
ALTER TABLE Categories ADD Note NVARCHAR(800) NULL
ENDGO