如何确定SQL Server中表的主键?

前端之家收集整理的这篇文章主要介绍了如何确定SQL Server中表的主键?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望能够在sql Server 2005中以某种方式使用表名作为输入来确定构成主键的所有字段. sp_columns似乎没有此字段.关于在哪里看的任何想法?

解决方法

我在我编写的代码生成器中使用它来获取主键:
SELECT i.name AS IndexName,OBJECT_NAME(ic.OBJECT_ID) AS TableName,COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName,c.is_identity,c.user_type_id,CAST(c.max_length AS int) AS max_length,CAST(c.precision AS int) AS precision,CAST(c.scale AS int) AS scale 
FROM sys.indexes AS i 
INNER JOIN sys.index_columns AS ic 
INNER JOIN sys.columns AS c ON ic.object_id = c.object_id AND ic.column_id = c.column_id 
    ON i.OBJECT_ID = ic.OBJECT_ID AND i.index_id = ic.index_id 
WHERE i.is_primary_key = 1 AND ic.OBJECT_ID = OBJECT_ID('dbo.YourTableNameHere')
ORDER BY OBJECT_NAME(ic.OBJECT_ID),ic.key_ordinal
原文链接:https://www.f2er.com/mssql/83623.html

猜你在找的MsSQL相关文章