Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2004
    Posts
    3

    Unanswered: How to extract all Primary Keys in a RS ?(SQLServer 2k)

    Hi Everybody,

    i´d like to know how can i get all PK´s and their respective table name listed in a result set.
    I did something similar to list all FKs (using the sysforeingkeys table), but there is no ´sysprimarykeys´.

    What i am trying to do is to document the database in 2 tables, one for tables and one for columns. Similar to the sys* views, but i will add description, comments, etc. This way documentation can be viewed with a single select, or a view to the programmers. AND WE hope we can keep it up to date more easily than printed sutff.

    Thx a lot
    dffcanimo

  2. #2
    Join Date
    Sep 2004
    Posts
    22
    SELECT o.name as pkname, x.name as tablename
    FROM sysobjects o JOIN sysobjects x
    ON x.id = o.parent_obj
    WHERE o.xtype = 'PK'

  3. #3
    Join Date
    Nov 2004
    Posts
    3
    Thx Mitchell,

    I will try this.

  4. #4
    Join Date
    Nov 2004
    Posts
    3
    Hey Mitchell,

    it worked great...
    But i found out that I need :
    * The name of the column (Pk) and not the constraints name.
    * 1 Row per Pk column (for cases where Pk is made up of 2 or more columns).

    I tried to relate the Constraint Name to the column name, but had no sucess....

    Appreciate any help !!!

  5. #5
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    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.

Posting Permissions

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