我知道用户定义的类型存储在 中information_schema.domains
,但是否可以获得类型的定义table type
?
要清楚,我正在寻找实际的表类型定义:
例如,我想获取列定义myTableType
CREATE TYPE myTableType AS TABLE(
Id INT,
SomeValue NVARCHAR(20)
);
我知道用户定义的类型存储在 中information_schema.domains
,但是否可以获得类型的定义table type
?
要清楚,我正在寻找实际的表类型定义:
例如,我想获取列定义myTableType
CREATE TYPE myTableType AS TABLE(
Id INT,
SomeValue NVARCHAR(20)
);
要获取用户定义表类型的列列表,请运行此命令。您需要将您的表名替换为some_table_type
:
SELECT *
FROM sys.columns
WHERE object_id IN (
SELECT type_table_object_id
FROM sys.table_types
WHERE name = 'some_table_type'
);
获取用户定义的表类型的表类型、列和数据类型的列表。
select tt.name AS table_Type, c.name AS table_Type_col_name,st.name AS
table_Type_col_datatype
from sys.table_types tt
inner join sys.columns c on c.object_id = tt.type_table_object_id
INNER JOIN sys.systypes AS ST ON ST.xtype = c.system_type_id
获取列名、其类型和可空性的详细信息:
SELECT tt.name AS Table_Type,
c.name AS Column_Name,
st.name AS Datatype,
CASE WHEN st.name = 'numeric' THEN '(' + Convert(varchar(5), c.precision) + ',' + Convert(varchar(5), c.scale) + ')'
WHEN st.name IN ('char', 'nchar', 'varchar', 'nvarchar') THEN '(' + Convert(varchar(5), c.max_length) + ')'
ELSE '' END AS Size,
CASE WHEN c.is_nullable = 0 THEN 'NOT NULL,'
ELSE ',' END AS Nullable
FROM sys.table_types tt
INNER JOIN sys.columns c ON c.object_id = tt.type_table_object_id
INNER JOIN sys.systypes st ON (st.xusertype = c.system_type_id AND st.uid = 4)
ORDER BY tt.name, c.column_id
使用更少的代码,使用 sys.dm_exec_describe_first_result_set您可以执行以下操作:
SELECT * FROM sys.dm_exec_describe_first_result_set (N'DECLARE @TEST myTableType ; SELECT * FROM @TEST', null, 0) ;
以下是一些列: