gpt4 book ai didi

sql-server - 如何列出 SQL Server 用户定义表类型的主键?

转载 作者:行者123 更新时间:2023-12-04 02:12:33 28 4
gpt4 key购买 nike

我的问题很简单,如何列出 SQL Server 用户定义表类型的主键(列名)?

例如;

CREATE TYPE [dbo].[MyTableType] AS TABLE
(
[ID] int NOT NULL, PRIMARY KEY CLUSTERED ( [ID])
)

如何通过查询获取列[ID]

好像只能找到真正的表的主键,不能找到表类型。

最佳答案

这存储在目录 View 中:

SELECT  c.Name
FROM sys.table_types AS tt
INNER JOIN sys.key_constraints AS kc
ON kc.parent_object_id = tt.type_table_object_id
INNER JOIN sys.indexes AS i
ON i.object_id = kc.parent_object_id
AND i.index_id = kc.unique_index_id
INNER JOIN sys.index_columns AS ic
ON ic.object_id = kc.parent_object_id
INNER JOIN sys.columns AS c
ON c.object_id = ic.object_id
AND c.column_id = ic.column_id
WHERE tt.Name = 'YourTypeName';

关于sql-server - 如何列出 SQL Server 用户定义表类型的主键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36843216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com