Sunday, February 19, 2012

Checking if column exists

Without throwing an error, how can I check if a column exists before
performing some action that references the column in question?You can do:
IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = @.col AND TABLE_NAME = 'tbl' )
Another option is to use the metadata functions like COL_LENGTH or
COLUMNPROPERTY. Use any argument and if it returns NULL then you can
conclude the column does not exist. See SQL Server Books Online for the
various arguments you can use for these functions.
Anith|||Thanks
"Anith Sen" wrote:

> You can do:
> IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
> WHERE COLUMN_NAME = @.col AND TABLE_NAME = 'tbl' )
> Another option is to use the metadata functions like COL_LENGTH or
> COLUMNPROPERTY. Use any argument and if it returns NULL then you can
> conclude the column does not exist. See SQL Server Books Online for the
> various arguments you can use for these functions.
> --
> Anith
>
>

No comments:

Post a Comment