Monday, November 16, 2009

Script out all PK's in a database...

Recently I had to grab all of the Primary Key for the tables in the DB and found this script...

SELECT   t.table_name,
         t.constraint_name,
         k.column_name
         k.ordinal_positio
FROM     information_schema.table_constraints t
         INNER JOIN information_schema.key_column_usage k
           ON t.constraint_name = k.constraint_name
WHERE    t.constraint_type = 'PRIMARY KEY'
ORDER BY t.table_name,
         k.ordinal_position

No comments:

Post a Comment