Search

Thursday, April 12, 2012

List all Primary keys column

Run below script to get list of all primary keys column:

SELECT  
sysobjects.name AS TableName  
,sysindexes.name AS PKName  
,syscolumns.colid AS ColumnOrder  
,index_col(object_name(sysindexes.id), sysindexes.indid,syscolumns.colid) AS ColumnName  
FROM sysobjects   
INNER JOIN sysindexes  
ON sysobjects.id = sysindexes.id   
INNER JOIN syscolumns  
ON sysindexes.id = syscolumns.id  
WHERE syscolumns.colid <= sysindexes.keycnt  
AND sysindexes.indid = 1  AND index_col(object_name(sysindexes.id), sysindexes.indid,syscolumns.colid) IS NOT NULL
--AND sysobjects.name = 'tablename'  
ORDER BY sysobjects.name ,sysindexes.name  

No comments:

Post a Comment