当前位置:  开发笔记 > 编程语言 > 正文

T-SQL:如何通过ID获取列的用户定义数据类型?

如何解决《T-SQL:如何通过ID获取列的用户定义数据类型?》经验,为你挑选了1个好方法。

我有这样的查询:

SELECT TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE
FROM MY_DB.INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'OrderId'
ORDER BY TABLE_NAME
GO

OrderId列是用户定义的数据类型.但是DATA_TYPE在查询中显示了底层系统类型(即bigint).如何显示用户定义的类型名称?



1> DJ...:

您可以更改Where子句:

SELECT 
    c.name as ColumneName, 
    c.colid As ColumnOrder, 
    st.name as UserType,
    bt.name as BaseType
FROM dbo.syscolumns c
INNER JOIN dbo.systypes st ON st.xusertype = c.xusertype
INNER JOIN dbo.systypes bt ON bt.xusertype = c.xtype
WHERE c.id = OBJECT_ID('TableName')
ORDER BY c.colid

编辑 - 更完整的版本 - 享受!

SELECT 
    OBJECT_NAME(c.id) as TableName,
    c.name as ColumnName, 
    c.colid As ColumnOrder, 
    st.name as UserType,
    bt.name as BaseType
FROM 
    dbo.syscolumns c
    INNER JOIN dbo.systypes st ON st.xusertype = c.xusertype
    INNER JOIN dbo.systypes bt ON bt.xusertype = c.xtype
WHERE 
    OBJECTPROPERTY(c.id,'ISTABLE') = 1
    AND c.name = 'ColumnName'
ORDER BY
    OBJECT_NAME(c.id), 
    c.colid

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有