Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Primary key

Author  Topic 

asm
Posting Yak Master

140 Posts

Posted - 2010-12-24 : 23:01:27

Hi,

How do we get the primary key column name and table name from a selected database through query.


thanks



kumarsudu
Starting Member

1 Post

Posted - 2010-12-25 : 06:58:10
Hi,

SELECT i.name AS IndexName,
OBJECT_NAME(ic.OBJECT_ID) AS TableName,
COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1

Source:http://blog.sqlauthority.com/2007/09/04/sql-server-2005-find-tables-with-primary-key-constraint-in-database/

Hope this may help you

thanks and regards,
kumar
Go to Top of Page
   

- Advertisement -