Showing posts with label knowthe. Show all posts
Showing posts with label knowthe. Show all posts

Wednesday, March 21, 2012

Name of primary key

What 's the way to know
the name of the column that is
the primary key of a tableTry this one:

select 'Table ['+su.name+'.'+so.name+']',sc.name
from sysindexes sy
join sysobjects so on so.id=sy.id and so.xtype='U'
join sysusers su on su.uid=so.uid
join sysindexkeys si on si.id=so.id and si.indid=sy.indid
join syscolumns sc on sc.id=so.id and sc.colid=si.colid
join sysobjects sop on sop.parent_obj=so.id and sop.xtype='PK' and sop.name=sy.name
where sy.indid not in(0,255)|||Thank you

forgot that existed sysindexes|||Thank you very much