Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Test to see if a column exist.

Author  Topic 

FSURob
Starting Member

6 Posts

Posted - 2007-01-22 : 10:14:33
Do you know of some SQL that I can test a table to see if a column exists or not inside a stored procedure?

What I have is a table that contains data for a 10 year history. I asked at the time and was told at the beginning of every new year the table would create 2 new columns so I dynamically set up some of my stored procedure to provide for that….well as of Jan 22 they have not updated them so I have 2 options:

1) hard code the first year in there and change it when they tell us

2) test for the field if it is there start the countdown to grab the rest of the historic info….if not check for the next year until it finds the field in the DB.



I’d rather do 2.

Thanks for any help.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-22 : 10:16:59
use INFORMATION_SCHEMA.COLUMNS

select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'yourtablename'
and COLUMN_NAME = 'yourcolumnname'



KH

Go to Top of Page

FSURob
Starting Member

6 Posts

Posted - 2007-01-22 : 10:22:59
That will work. Thanks for the help!
Go to Top of Page
   

- Advertisement -