Wednesday, September 30, 2009

List of all the primary key and foreign key table and column names

List of all the primary key and foreign key table and column names

SELECT distinct
SCHEMA_NAME(SOF.SCHEMA_ID) as PK_SCHEMA ,object_name(rkeyid) as PK_TABLE ,  C.name as PK_COL,SCHEMA_NAME(SOF.SCHEMA_ID) AS FK_SCHEMA ,object_name(fkeyid) as FK_TABLE,b.name as FK_COL


FROM
sys.sysforeignkeys A 
left outer join syscolumns b on a.fkeyid = b.id and a.fkey = b.colid 

left outer join syscolumns c on a.rkeyid = c.id and a.rkey = c.colid 

left outer JOIN sys.objects SOF ON SOF.OBJECT_ID = A.FKEYID

No comments:

Post a Comment

Featured Post

SQL Server AWS Migration BCP

stored procedure to generate BCP scritps to migrate the SQL Server database. Developed this stored procedure on my labs to simulate t...

Contributors