简体   繁体   English

SQL Server:从查询中获取某些表名

[英]SQL Server : getting certain table names from query

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' 无效的列名“ ContactDateTime”

even though that is one of the row names. 即使那是行名之一。

Is this possible to do? 这可能吗?

if ContactDateTime is a column that you are looking for in table memberEmails then you can do this 如果ContactDateTime是您要在表memberEmails中查找的列,则可以执行此操作

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

The column ContactDateTime may be a column in your table but it is not a column in the INFORMATION_SCHEMA.COLUMNS view. ContactDateTime列可能是表中的列,但不是INFORMATION_SCHEMA.COLUMNS视图中的列。

Since it is not a column there, SQL Server is going to error out saying that it is invalid. 由于那里没有一列,因此SQL Server将错误地指出它无效。

I think what you're trying to do is add another WHERE clause to your statement: 我认为您要执行的操作是在语句中添加另一个WHERE子句:

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 . 另请参阅此处,以了解反对使用INFORMATION_SCHEMAS的情况。

The view INFORMATION_SCHEMA.COLUMNS don't have column name ContactDateTime . 视图INFORMATION_SCHEMA.COLUMNS没有列名ContactDateTime Please read document first 请先阅读文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM