如何在sql查询中显示所有列的数据类型

时间:2014-08-31 14:52:11

标签: sql types sqldatatypes

我已尝试过此查询,但会返回错误:

Select DATA_TYPE 
From INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customers' AND
COLUMN_NAME IN (Select *
From INFORMATION_SCHEMA.COLUMNS
Where TABLE_NAME = 'Customers');

错误:

  

当EXISTS没有引入子查询时,只能在选择列表中指定一个表达式。

3 个答案:

答案 0 :(得分:1)

只需使用

Select COLUMN_NAME , DATA_TYPE 
From INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customers'

BTW如果你使用in子句,那么在子查询中只选择一个列,而不是全部

where COLUMN_NAME IN (Select col from table)

而不是

where COLUMN_NAME IN (Select * from table)

答案 1 :(得分:1)

我会小心整理和使用:

Select DATA_TYPE 
From INFORMATION_SCHEMA.COLUMNS
WHERE lower(TABLE_NAME) = 'customers';

答案 2 :(得分:0)

select DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='Customers'

或者更好的解决方案是

SELECT  c.name AS [Column Name],
       t.Name AS [Data type]
FROM sys.columns c
INNER JOIN  sys.types t ON c.user_type_id = t.user_type_id
WHERE c.object_id = OBJECT_ID('Customers')