Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Function to List Primary Key Columns for a Table Expand / Collapse
Author
Message
Posted Thursday, October 4, 2007 3:29 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, April 19, 2011 5:10 PM
Points: 275, Visits: 168
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
Post #407127
Posted Tuesday, May 20, 2008 1:47 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, February 19, 2011 7:19 PM
Points: 31, Visits: 101
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!
Post #504012
Posted Wednesday, July 9, 2008 3:21 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, August 19, 2009 12:36 PM
Points: 1, Visits: 32
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'
Post #531252
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse