Thursday, March 22, 2012

determin name of primary column

given the name of any table how can i programmatically get the name of the primary key column?

You can use the INFORMATION_SCHEMA.TABLE_CONSTRAINTS view like:

SELECT tc.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS as tc

WHERE tc.TABLE_SCHEMA = 'dbo'

and tc.TABLE_NAME = '<your_table>'

and tc.CONSTRAINT_TYPE = 'PRIMARY KEY';

sql

No comments:

Post a Comment