Script: Getting PK and FK info

  • rsharma

    SSCarpal Tunnel

    Points: 4865

    Comments posted to this topic are about the content posted at

  • Calvin Lawson


    Points: 11030

    This query provides "To" and "From" Fields as well. I like your query for PK constraints...


    so1.Name as FKConstraint,

    so.Name as FromTable,

    sc.Name as FromColumn, so2.Name as ToTable, sc1.Name as ToColumn

    From sysobjects so (nolock)

    JOIN sysforeignkeys fk (nolock) on so.[id] = fk.fkeyid

    join syscolumns sc (nolock) on fk.fkeyid = and fk.fkey = sc.colid

    JOIN sysobjects so2 (nolock) on fk.rkeyid =

    join syscolumns sc1 (nolock) on fk.rkeyid = and fk.rkey = sc1.colid

    JOIN sysobjects so1 (nolock) on fk.constid =

    where = X

    Signature is NULL

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply