How do I list user defined types in a SQL Server database?

后端 未结 3 1205
逝去的感伤
逝去的感伤 2020-12-14 00:45

I need to enumerate all the user defined types created in a SQL Server database with CREATE TYPE, and/or find out whether they have already been de

3条回答
  •  隐瞒了意图╮
    2020-12-14 01:16

    Although the post is old, I found it useful to use a query similar to this. You may not find some of the formatting useful, but I wanted the fully qualified type name and I wanted to see the columns listed in order. You can just remove all of the SUBSTRING stuff to just get the column name by itself.

    SELECT USER_NAME(TYPE.schema_id) + '.' + TYPE.name      AS "Type Name",
           COL.column_id,
           SUBSTRING(CAST(COL.column_id + 100 AS char(3)), 2, 2)  + ': ' + COL.name   AS "Column",
           ST.name                                          AS "Data Type",
           CASE COL.Is_Nullable
           WHEN 1 THEN ''
           ELSE        'NOT NULL' 
           END                                              AS "Nullable",
           COL.max_length                                   AS "Length",
           COL.[precision]                                  AS "Precision",
           COL.scale                                        AS "Scale",
           ST.collation                                     AS "Collation"
    FROM sys.table_types TYPE
    JOIN sys.columns     COL
        ON TYPE.type_table_object_id = COL.object_id
    JOIN sys.systypes AS ST  
        ON ST.xtype = COL.system_type_id
    where TYPE.is_user_defined = 1
    ORDER BY "Type Name",
             COL.column_id
    

提交回复
热议问题