Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List of Primary Keys on user tables

Status
Not open for further replies.

joemcs

Programmer
Joined
Aug 13, 2001
Messages
5
Location
AU
This is what i did to get a list of primary keys and the column names.

SELECT o.name as
, i.name AS [Index], c.name AS [Column], k.keyno as [Keyno]
FROM sysindexkeys k INNER JOIN
syscolumns c ON k.id = c.id AND
k.colid = c.colid INNER JOIN
sysindexes i ON k.id = i.id AND
k.indid = i.indid INNER JOIN
sysobjects o ON c.id = o.id INNER JOIN
sysobjects p on p.parent_obj = o.id AND
p.name = i.name
WHERE o.type = 'U' and p.xtype = 'PK' and o.name
<> 'dtproperties'
ORDER BY o.name, i.name, k.keyno

There may be a cleaner way, would welcome comments.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top