Giri Prasad
Giri Prasad

Reputation: 1205

How to check whether the table contains a particular column or not?

How to check whether the table contains a particular column or not?

Upvotes: 0

Views: 316

Answers (4)

HKB
HKB

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

Rahul Sharma
Rahul Sharma

Reputation: 453

if exists
(select * from sys.columns
 where Name = N'columnName' and Object_ID = Object_ID(N'tableName'))

Upvotes: 1

Bill
Bill

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

Code Different
Code Different

Reputation: 93141

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_name' AND COLUMN_NAME = 'column_name'

Upvotes: 3

Related Questions