在Sql表中显示特定列

时间:2018-10-27 08:42:17

标签: sql sql-server tsql schema database-schema

如何仅显示INFORMATION_SCHEMA.COLUMNS模式中的特定列?

例如:

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'rate_Structure'
ORDER BY ORDINAL_POSITION

返回:

+-------------+
|             |
| COLUMN_NAME |
+-------------+
|    a        |
|    b        |
|    c        |
|    d        |
|    e        |
+-------------+

如何仅显示a和e列?

4 个答案:

答案 0 :(得分:1)

您需要添加其他条件,例如:

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'rate_Structure'  
  AND data_type = 'int'    -- condition to get only specific columns
ORDER BY ORDINAL_POSITION;

答案 1 :(得分:1)

处添加另一个条件
SELECT COLUMN_NAME    
FROM INFORMATION_SCHEMA.COLUMNS    
WHERE TABLE_NAME = 'rate_Structure' and COLUMN_NAME in ('a','c')    
ORDER BY ORDINAL_POSITION

答案 2 :(得分:1)

  

如何仅显示a和e列

只需将 WHERE 子句中的结果过滤为

SELECT COLUMN_NAME    
FROM INFORMATION_SCHEMA.COLUMNS    
WHERE TABLE_NAME = 'rate_Structure' and COLUMN_NAME in ('a','e') 
-- You can also use: and (COLUMN_NAME = 'a' or COLUMN_NAME = 'e')    
ORDER BY ORDINAL_POSITION

您需要添加条件以获取所需的内容,and COLUMN_NAME in ('a','e')将仅返回具有COLUMN_NAME'a'值的'e'

答案 3 :(得分:0)

您可以按以下方式使用INFORMATION_SCHEMA.COLUMNS

USE Test
GO
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'SalesTerritory'
AND COLUMN_NAME = 'Region'