Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

How to check if a column exists in a tablle

I have a proc p_GetData that is invoked by many other procs,
 A temporary table is passed to the proc p_GetData as a paramater, @TableName  

I would like to know if a particular column exists in the temp table, can someone provide the sql for that

CREATE PROCEDURE p_GetData
    @TableName  varchar(100),
    @ServerName varchar(50),
    @UpdateFlag int,
    @SuccessFlag tinyint OUTPUT
AS



Avatar of rpkhare
rpkhare
Flag of India image

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_NAME = 'MyTable' AND COLUMN_NAME = 'abcd'
Avatar of countrymeister
countrymeister

ASKER

rpkhare:

Doe this work in SQL Server 2000
ASKER CERTIFIED SOLUTION
Avatar of rpkhare
rpkhare
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you please let me know which is better
SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '@TableName' AND COLUMN_NAME = 'Action'

select object_name(id) as TableName, * from syscolumns where lower(name) like 'action'
and object_name(id) = '@TableName'
The former seems to be more close to what you are desiring.
Hi

here in this query
SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '@TableName' AND COLUMN_NAME = 'Action'

you are putting @TableName within quotes which will produce error because @TableName is a variable that is containing the name of your table, so remove those quotes.

SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName AND COLUMN_NAME = 'Action'



For whatever reason it does not work, I would like to know if I create a global table in another  proc and then pass this to proc p_GetData , will it detect if the @TableName  has the column I am looking for
This does not seem to work for global tables so I need to close this qt