¿Cómo enumero los tipos definidos por el usuario en una base de datos de SQL Server?


Necesito enumerar todos los tipos definidos por el usuario creados en una base de datos SQL Server con CREATE TYPE, y/o averiguar si ya han sido definidos.

Con tablas o procedimientos almacenados haría algo como esto:

if exists (select * from dbo.sysobjects where name='foobar' and xtype='U')
    drop table foobar

Sin embargo, no puedo encontrar el equivalente (o una alternativa adecuada) para los tipos definidos por el usuario! Definitivamente no puedo verlos en ninguna parte de sysobjects.

¿Puede alguien iluminarme?

Author: gotqn, 2008-09-10

3 answers

Los tipos y UDT no aparecen en sys.objeto. Usted debe ser capaz de obtener lo que usted está buscando con lo siguiente:

select * from sys.types
where is_user_defined = 1
 56
Author: jwolly2,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-10 16:13:05

Aunque el post es antiguo, me pareció útil usar una consulta similar a esta. Puede que algunos de los formatos no le resulten útiles, pero quería el nombre de tipo completo y quería ver las columnas en orden. Puede eliminar todas las SUBCADENAS para obtener el nombre de la columna por sí mismo.

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
 8
Author: Ron Sanderson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-21 21:22:08

Para ampliar la respuesta de jwolly2, así es como se obtiene una lista de definiciones incluyendo el tipo de datos estándar:

-- User Defined Type definitions TP 20180124
select t1.name, t2.name, t1.precision, t1.scale, t1.max_length as bytes, t1.is_nullable
from sys.types t1
join sys.types t2 on t2.system_type_id = t1.system_type_id and t2.is_user_defined = 0
where t1.is_user_defined = 1 and t2.name <> 'sysname'
order by t1.name
 1
Author: Ton Plooij,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-24 15:47:22