0

Is it possible to display just certain table names in the query:

USE [WebContact] 

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'

Most of the time I would need all the table names but there are curtain situations where I need just certain row names.

When I try doing the following:

USE [WebContact] 

SELECT COLUMN_NAME, ContactDateTime 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'

It tell me that

Invalid column name 'ContactDateTime'

even though that is one of the row names.

Is this possible to do?

1
  • 1
    ContactDateTime might be a column in YOUR table memberEmails - but it's most certainly NOT a column in INFORMATION_SCHEMA.COLUMNS.... Commented Apr 22, 2016 at 16:00

3 Answers 3

2

The column ContactDateTime may be a column in your table but it is not a column in the INFORMATION_SCHEMA.COLUMNS view.

Since it is not a column there, SQL Server is going to error out saying that it is invalid.

I think what you're trying to do is add another WHERE clause to your statement:

USE [WebContact] 
SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME] = 'ContactDateTime'; -- Here!

Or if you want to add multiple columns...

USE [WebContact] 
SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME] 
  IN ('ContactDateTime', 'column2', 'column3', ... 'column(n)'); -- Here!

Also see here for the case against using INFORMATION_SCHEMAS.

Sign up to request clarification or add additional context in comments.

2 Comments

That last example seems to be the one I needed. Thanks Shaneis!
ah...yeah...no problem...seriously though learn the joys that is sys.columns and sys.tables and leave INFORMATION_SCHEMA to itself :)
2

if ContactDateTime is a column that you are looking for in table memberEmails then you can do this

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
and COLUMN_NAME='ContactDateTime'

2 Comments

Does this work with more than one row name? When I try to add another and it comes back with blank data.
in that case, you have to use the in syntax such as select .... where...column_name in ('ContactDateTime','AnotherColumnName')
0

The view INFORMATION_SCHEMA.COLUMNS don't have column name ContactDateTime. Please read document first

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.