SQL Server 2005查找数据库中所有列的特定数据类型

时间:2011-06-03 21:57:49

标签: sql-server sql-server-2005

有没有办法查询系统表以查找数据库中具有特定数据类型的所有列。

例如,如果我需要知道表名和数据类型为ntext的列

有办法吗?

4 个答案:

答案 0 :(得分:6)

试试这个:

SELECT a.name -- OR a.* 
  FROM syscolumns a INNER JOIN systypes b
    ON a.xtype = b.xtype
   AND b.name = 'ntext' -- OR OTHER DATA TYPE.

答案 1 :(得分:2)

试试这个

SELECT o.name AS 'Table Name', c.name AS 'Column Name' FROM sysobjects AS o
INNER JOIN syscolumns AS c ON o.name = c.object_id
INNER JOIN systypes AS t ON t.xtype = c.xtype
WHERE b.name = ' ntext' 

希望这有帮助。

答案 2 :(得分:1)

SELECT so.name, sc.name
  FROM sys.objects so
  JOIN sys.columns sc ON so.object_id = sc.object_id
  JOIN sys.types stp ON sc.user_type_id = stp.user_type_id
                    AND stp.name = 'ntext'
 WHERE so.type = 'U' -- to show only user tables

答案 3 :(得分:1)

我知道这个问题已经得到解答但是我想将表名添加到结果集中,这个查询就是这样做的。

SELECT a.name, o.name AS TableName, o.type, a.id, o.object_id, o.schema_id
FROM sys.syscolumns AS a INNER JOIN sys.systypes AS b ON a.xtype = b.xtype 
AND b.name = 'char' 
AND a.length = 6 INNER JOIN
sys.objects AS o ON a.id = o.object_id
WHERE (o.type = 'u') 
AND (o.schema_id = 1)