How to check if a Field Exists?

I need to discover if a Field Exists in a specific database table on MS SQL Server 2000.  I was using:

            SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'Field1'

this apparently is NOT the correct approach is it will default to the Master database not the database I'm connected to.

What is the correct way to determine if a field exists in a table for my database?

thanks, Rob.
RobAinscoughAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim P.Commented:
SELECT *
FROM DbName.dbo.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'Field1'

0
Jim P.Commented:
You can also do it the hard way:

select tables.name, cols.*  
from northwind.dbo.syscolumns cols
right join northwind.dbo.sysobjects tables
on cols.id = tables.id
where cols.name ='ContactName'
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael_DCommented:
Try

SELECT *
FROM MyDatabase.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'MyColumn' AND Table_Name ='MyTable'
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jim P.Commented:
Michael_D you missed a period

SELECT *
FROM MyDatabase..INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'MyColumn' AND Table_Name ='MyTable'
0
Michael_DCommented:
Nope,

It works perfectly on my side :)
0
Michael_DCommented:
And Your version returns error :
Could not find server 'MyDatabase' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.
0
RobAinscoughAuthor Commented:
Sorry folks, none of the above are working.

I tried to execute the above query from a View in either MyDatabase or Master and still get:

could not find "MyDatabase" in sysservers.  And I know MyDatabase does exists as I can browse to in in Enterprise Manager and manipulate tables, etc. etc.
0
Jim P.Commented:
Dumb Question:  Is the query analyzer running against the same server?

Michael_D,

My Sincerest humble apologies. Forgot  INFORMATION_SCHEMA was the schema.

RobAinscough, it should be


SELECT *
FROM northwind.INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'ContactName'
  AND Table_Name ='Customers'
0
RobAinscoughAuthor Commented:
Ok, this works:

SELECT     *
FROM         INFORMATION_SCHEMA.COLUMNS
WHERE     (COLUMN_NAME = 'Field1') AND (TABLE_NAME = 'Table1')

From within a View/connection to MyDatabase.

Table_Name was what I needed.

Thanks, Rob.
0
Scott PletcherSenior DBACommented:

IF EXISTS(      SELECT 1
      FROM dbName.dbo.syscolumns WITH (NOLOCK)
      WHERE OBJECT_NAME(id) = N'tableName'
      AND name = N'columnName')
      PRINT 'Yes'
ELSE
      PRINT 'No'
0
Michael_DCommented:
WOW!!!

Grade B!!!

THANK YOU!!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.