I'd like to determine the primary key of a table using TSQL (stored procedure or system table is fine). Is there such a mechanism in SQL Server (2005 or 2008)?
I'd like to determine the primary key of a table using TSQL (stored procedure or system table is fine). Is there such a mechanism in SQL Server (2005 or 2008)?
Can't add a comment, not enough rep points, but this is in response to those saying sp_Pkeys is not usable. Doesn't have to be a function as mentioned in another comment to an answer.
DECLARE @tbl TABLE
(
Table_Qualifier varchar(30),
Table_Owner varchar(30),
Table_Name varchar(50),
Column_Name varchar(30),
Key_Seq int,
PK_Name varchar(50)
)
insert into @tbl EXEC sp_Pkeys 'tablename'
select * from @tbl