Technical Article

Columns that are primary keys in a database

,

Below is a SQL Server 2000 query to return all columns that are primary keys to tables in a database

SELECT '[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'
AS [Table Name], COLUMN_NAME
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE OBJECTPROPERTY(OBJECT_ID(QUOTENAME(CONSTRAINT_NAME)),
'IsPrimaryKey') = 1
AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
'.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0
ORDER BY TABLE_NAME, COLUMN_NAME

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating