SqlServer2k: How do I find the primary key column?

aceO07

Diamond Member
Nov 6, 2000
4,491
0
76
I've been using mysql and I need to convert some code to use sqlserver. After some searching I found the equiv of "describe" for sqlserver. However, it doesn't return which column is the primary key. What command will return the primary key of a table? sp_column and sp_help procedures don't return that information...

edit: nevermind I found it. damn Microsoft has to do everything their own way. :p
sysobjects..

edit: damnit! no I haven't! I thought select * from sysobjects where xtype='pk'; would give me the column name. BUT NOPE! It know's there's a primary key column in the right key, but doesn't give me the column name.

edit: nevermind. geez. I had to stumble on a MS programmer's page to get the code to get a freaking primary column listing.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Something wrong with using the gui? It's pretty intuitive. Or does this have to be done programatically?
 

aceO07

Diamond Member
Nov 6, 2000
4,491
0
76
I needed to do it using java.

Assuming you're interested, here's the long query statement that's needed to get it:
select c.COLUMN_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c where pk.TABLE_NAME = 'FIND IN TABLENAME'
and CONSTRAINT_TYPE = 'PRIMARY KEY' and c.TABLE_NAME = pk.TABLE_NAME
and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME;