Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322

    Unanswered: SQL Server 2k - RI Query

    Anyone have a query that would return FK info

    All I want is

    PARENT TABLE, CHILD TABLE

    SQL Server catalog bites
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Something like this should do you
    Code:
    SELECT fkey.name As [key]
         , parent.name As [parent]
         , kid.name As [kid]
    FROM   sysforeignkeys fks
     INNER
      JOIN sysobjects fkey
        ON fks.constid = fkey.id
     INNER
      JOIN sysobjects parent
        ON fks.rkeyid = parent.id
     INNER
      JOIN sysobjects kid
        ON fks.fkeyid = kid.id
    George
    Home | Blog

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Code:
    select	parents.name,
    	keys.name,
    	children.name
    from	sys.objects as parents
    	inner join sys.foreign_keys keys on parents.object_id = keys.referenced_object_id
    	inner join sys.objects as children on keys.parent_object_id = children.object_id
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    If you don't get paid by the keystroke
    Code:
    SELECT    parentName    = OBJECT_NAME(rkeyid)
            , childName     = OBJECT_NAME(fkeyid)
            , keyName       = OBJECT_NAME(constid)
    FROM   dbo.sysforeignkeys
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Snotty Brit.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  6. #6
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    If you don't get paid by the key stroke, make a report of it in a web page, or SSRS, and invoke it as necessary with a few clicks of the mouse.

  7. #7
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    he is pulling your chains. Brett Kaiser has a query like this laying around. This is his usual aloof check in post.

    I hope Eli Manning dies.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  8. #8
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Thrasymachus
    I hope Eli Manning dies.
    but only in a successful-string-of-quarterbacking-performances-come-to-an-end kind of way, shurely

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  9. #9
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    don't call me Shirley, but of course I am just trying to push Kaiser buttons.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •