How can I tell if a column for a table already exists?

Below are the two ways in SQL server to identify whether column in a particular table Exists or Not.

Create Table Employee (ID int, Name varchar(100))
GO

--Way 1

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employee' AND COLUMN_NAME = 'Name')
BEGIN
		PRINT 'EXISTS'
END
ELSE
BEGIN
		PRINT 'DOESN''T EXISTS'
END

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employee' AND COLUMN_NAME = 'Adress')
BEGIN
		PRINT 'EXISTS'
END
ELSE
BEGIN
		PRINT 'DOESN''T EXISTS'
END

--Way 2
if columnproperty (object_id('Employee'), 'Name', 'AllowsNull') IS NOT NULL
BEGIN
		PRINT 'EXISTS'
END
ELSE
BEGIN
		PRINT 'DOESN''T EXISTS'
END

if columnproperty (object_id('Employee'), 'Address', 'AllowsNull') IS NOT NULL
BEGIN
		PRINT 'EXISTS'
END
ELSE
BEGIN
		PRINT 'DOESN''T EXISTS'
END
Advertisements

#if-exists-for-column