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.
Author |
Topic |
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2006-12-11 : 17:18:58
|
Guys,I am trying to join sysforeignkeys, sysobjects and information_schema.columns to get the details of the foreignkeys. But somehow I dont seem to get the desired output.Below query is how far I gotSELECT T.NAME FNAME, O.NAME PNAME, F.FKEY, S.NAME CNAME, F.RKEYFROM SYSFOREIGNKEYS F INNER JOIN SYSOBJECTS O ON F.FKEYID = O.ID INNER JOIN SYSOBJECTS S ON F.RKEYID = S.ID INNER JOIN SYSOBJECTS T ON F.CONSTID = T.ID fname - foreignkey namepname - parent table namepcol - parent column namepdtype - parent column datatypecname - child table nameccol - child column namecdtype - child column datatypeMy desired output is something like below.cname pname pcol pdtype cname col cdtypeAny suggestions/inputs would helpThanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2006-12-11 : 17:31:15
|
Peso,But I am looking to drill down to the column levele detailsand the link you sent me doesnt seem to do this.any suggestions/inputs??Thanks |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-11 : 17:33:41
|
Maybe you can make that topic a start, and just add the datatype as you need?Peter LarssonHelsingborg, Sweden |
 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2006-12-12 : 17:31:04
|
This query should suffice my requirementsSELECT OBJECT_NAME(sfk.constid) FNAME, OBJECT_NAME(sfk.fkeyid) PNAME, sc1.name PCOL, st1.name PDTYPE, OBJECT_NAME(sfk.rkeyid) CNAME, sc2.name CCOL, st2.name CDTYPEFROM sysforeignkeys sfk JOIN syscolumns sc1 ON sfk.fkeyid = sc1.id AND sfk.fkey = sc1.colid JOIN systypes st1 ON sc1.xtype = st1.xtype JOIN syscolumns sc2 ON sfk.rkeyid = sc2.id AND sfk.rkey = sc2.colid JOIN systypes st2 ON sc2.xtype = st2.xtypeWHERE st1.name <> 'sysname' AND st2.name <> 'sysname'ORDER BY FNAME, PNAME, CNAME, keyno |
 |
|
|
|
|
|
|