如何完成当前关于用户定义 Table 类型的查询并获取 Schema、IsIdentity、IsPrimaryKey 和...?

How can I complete my current query about User Defined Table Types and getting Schema,IsIdentity,IsPrimaryKey and ...?

我查询有关用户定义 Table 类型的信息。

我需要通过添加以下列来完成此查询:

IsIdentity
IsPrimaryKey
Schema
ColumnDefaultValue

查询:

SELECT  o.name AS TableName ,
        c.name AS ColumnName ,
        c.isnullable AS [IsNullable] ,
        t.name AS [DataType] ,
        t.[length] AS [MaxLength] ,
        t.prec AS [Precision]
FROM    syscolumns c
        INNER JOIN sysobjects o ON o.id = c.id
        LEFT JOIN systypes t ON t.xtype = c.xtype
WHERE   c.id IN ( SELECT    type_table_object_id
                  FROM      sys.table_types )
ORDER BY o.name ,
        c.name;

我还有一个关于上述查询的问题。

我有名称为 dbo.MyType 的用户定义 Table 类型,但在此查询中显示 'TT_MyType_37C5420D'.

如何获得真实姓名?

从 SQL Server 2005 开始,您应该使用来自 sys 架构的新目录视图,并避免使用旧的 syscolumnssysobjects

将查询重写为:

SELECT  
    tt.name AS TableName,
    c.name AS ColumnName,
    c.is_nullable AS [IsNullable],
    c.is_identity,
    t.name AS [DataType],
    t.max_length [MaxLength],
    t.precision AS [Precision]
FROM
    sys.table_types tt
INNER JOIN 
    sys.columns c ON c.object_id = tt.type_table_object_id
LEFT JOIN 
    sys.types t ON t.system_type_id = c.system_type_id
ORDER BY 
    tt.name, c.name;

您可以轻松利用 sys 目录视图中提供的更广泛的信息 - 如 sys.columns 上的 c.is_identity 列。而且 - sys.table_types 中的 name 似乎 return 是您正在寻找的更易读的名称。

查看 very extensive MSDN documentation on the sys catalog views 发现更多您可能感兴趣的信息