如果想知道表的主键的名字.那很简单:
select name from sysobjects where parent_obj = object_id('TableName') and xtype = 'PK';
如果是要知道表的主键有哪些字段,那幺这样:
select
COLUMN_NAME = convert(sysname,c.name),
KEY_SEQ = convert(smallint,c1.colid),
PK_NAME = convert(sysname,i.name)
from
sysindexes i, syscolumns c, sysobjects o, syscolumns c1
where
o.id = object_id('TableName')
and o.id = c.id
and o.id = i.id
and (i.status & 0x800) = 0x800
and c.name = index_col (quotename('TableName'), i.indid, c1.colid)
and c1.colid <= i.keycnt /* create rows from 1 to keycnt */
and c1.id = object_id('TableName')
order by 2