Function to List Primary Key Columns for a Table

  • Comments posted to this topic are about the item Function to List Primary Key Columns for a Table


    Karen Gayda
    MCP, MCSD, MCDBA

    gaydaware.com

  • The "i.id = t.id" in the WHERE clause is not needed because it's already in the join condition.

    Since we are looking for primary keys and text fields cannot be part of primary key, the "AND i.indid BETWEEN 1 And 254" is not needed as well.

    Otherwise, nice code!

  • Here is an alternative using the INFORMATION_SCHEMA view.

    select column_name

    from information_schema.table_constraints join information_schema.key_column_usage on (information_schema.table_constraints.constraint_name = information_schema.key_column_usage.constraint_name)

    where information_schema.table_constraints.table_name = @table_name

    and constraint_type = 'PRIMARY KEY'

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply