Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Getting unique or primary key columns for a given table


Getting unique or primary key columns for a given table

Author
Message
Kiran-339615
Kiran-339615
SSC Veteran
SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)

Group: General Forum Members
Points: 233 Visits: 109
Comments posted to this topic are about the item Getting unique or primary key columns for a given table

Regards,

-Kiran
peterhails
peterhails
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 229
Thats great Kiran...

For interest I have modified this to just show Unique cols plus PK for the param table:

create PROCEDURE [dbo].[GetUniqueCols]
@table_name nvarchar(50)

AS

select
c.name as [Column_Name],
kc.name as [Constraint_Name],
object_name(c.object_id) as [Table_Name]
from sys.columns c
join sys.key_constraints kc
on (c.column_id = kc.unique_index_id and c.object_id = kc.parent_object_id)
where kc.type='UQ' or kc.type = 'PK'
and c.object_id = object_id(@table_name)
GROUP BY c.name,kc.NAME, object_name(c.object_id)
Having object_name(c.object_id)=@table_name
irene.floyd
irene.floyd
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 41
What version of SQL are you using?

I got these error messages when I tried your code in MS SQL 2000:

Server: Msg 208, Level 16, State 1, Line 5
Invalid object name 'sys.columns'.
Server: Msg 208, Level 16, State 1, Line 5
Invalid object name 'sys.key_constraints'.
peterhails
peterhails
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 229
Yes ...this code will only work with 2005/8 I think...
irene.floyd
irene.floyd
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 41
Kiran -
Do you have a version of your query for MS SQL 2000?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search