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 defined.
我需要使用CREATE TYPE枚举在SQL Server数据库中创建的所有用户定义类型,和/或找出它们是否已经定义。
With tables or stored procedures I'd do something like this:
使用表或存储过程,我会做这样的事情:
if exists (select * from dbo.sysobjects where name='foobar' and xtype='U')
drop table foobar
However I can't find the equivalent (or a suitable alternative) for user defined types! I definitely can't see them anywhere in sysobjects
.
但是我找不到用户定义类型的等价物(或合适的替代品)!我绝对无法在sysobjects中看到它们。
Can anyone enlighten me?
任何人都可以开导我吗?
3 个解决方案
#1
56
Types and UDTs don't appear in sys.objects. You should be able to get what you're looking for with the following:
类型和UDT不会出现在sys.objects中。您应该能够通过以下方式获得所需内容:
select * from sys.types
where is_user_defined = 1
#2
8
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.
虽然帖子很旧,但我发现使用类似的查询很有用。您可能找不到一些有用的格式,但我想要完全限定的类型名称,我希望看到按顺序列出的列。您可以删除所有SUBSTRING内容以仅获取列名称。
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
#3
1
To expand on jwolly2's answer, here's how you get a list of definitions including the standard data type:
为了扩展jwolly2的答案,以下是如何获得包括标准数据类型在内的定义列表:
-- 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
56
Types and UDTs don't appear in sys.objects. You should be able to get what you're looking for with the following:
类型和UDT不会出现在sys.objects中。您应该能够通过以下方式获得所需内容:
select * from sys.types
where is_user_defined = 1
#2
8
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.
虽然帖子很旧,但我发现使用类似的查询很有用。您可能找不到一些有用的格式,但我想要完全限定的类型名称,我希望看到按顺序列出的列。您可以删除所有SUBSTRING内容以仅获取列名称。
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
#3
1
To expand on jwolly2's answer, here's how you get a list of definitions including the standard data type:
为了扩展jwolly2的答案,以下是如何获得包括标准数据类型在内的定义列表:
-- 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