Reputation: 1205
How to check whether the table contains a particular column or not?
Upvotes: 0
Views: 316
Reputation: 55
Since you are looking for a particular column.
IF EXISTS(
SELECT TOP 1 *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE [TABLE_NAME] = 'TableName'
AND [COLUMN_NAME] = 'ColumnName'
AND [TABLE_SCHEMA] = 'SchemaName')
BEGIN
PRINT 'Your Column Exists'
END
Upvotes: 1
Reputation: 453
if exists
(select * from sys.columns
where Name = N'columnName' and Object_ID = Object_ID(N'tableName'))
Upvotes: 1
Reputation: 176
You can query the information schema tables for this kind of information and much more.
In your case something like this would be useful:
select
*
from
INFORMATION_SCHEMA.COLUMNS
where
table_schema = '<your schema>'
and
table_name = '<your table>'
Upvotes: 2
Reputation: 93141
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_name' AND COLUMN_NAME = 'column_name'
Upvotes: 3