Professional Web Applications Themes

PK columns - Microsoft SQL / MS SQL Server

How to get the list of columns which compose the PK (not the clustered index) on a table? I want to get it from system tables. Thanks....

Sponsored Links
  1. #1

    Default PK columns

    How to get the list of columns which compose the PK (not
    the clustered index) on a table? I want to get it from
    system tables.

    Thanks.
    Sponsored Links
    JZHU Guest

  2. #2

    Default Re: PK columns

    > How to get the list of columns which compose the PK (not
    > the clustered index) on a table?
    EXEC sp_pkeys 'tablename'
    > I want to get it from system tables.
    Don't do this. System tables may change or disappear in future versions, so
    your code will break. At least if you use a system proc (or
    INFORMATION_SCHEMA views, when possible), your code will have a chance of
    living through version upgrades...


    Aaron Bertrand - MVP Guest

  3. #3

    Default Re: PK columns

    Thanks. One more question, how can I put the result of
    this SP into a temporary table, when the table name is a
    varaible? Basically, I want to do something like:
    declare pkcolumns table
    (table_qualifier varchar(100),
    table_owner varchar(100),
    table_name varchar(100),
    column_name varchar(100),
    key_seq int,
    pk_name varchar(100))
    declare tablename varchar(100)
    set tablename = 'tablename'
    insert into pkcolumns
    EXEC ('exec sp_pkeys ''' + tablename + '''')

    But it doesn't pass the grammar check.

    Thanks again.
    >-----Original Message-----
    >> How to get the list of columns which compose the PK (not
    >> the clustered index) on a table?
    >
    >EXEC sp_pkeys 'tablename'
    >
    >> I want to get it from system tables.
    >
    >Don't do this. System tables may change or disappear in
    future versions, so
    >your code will break. At least if you use a system proc
    (or
    >INFORMATION_SCHEMA views, when possible), your code will
    have a chance of
    >living through version upgrades...
    >
    >
    >.
    >
    JZHU Guest

  4. #4

    Default Re: PK columns

    >> You can do INSERT..EXEC on a table variable ..<<

    Oops.. I meant cannot!

    --
    - Anith
    ( Please reply to newsgroups only )


    Anith Sen Guest

Similar Threads

  1. xml sortable columns
    By Douglas Evans in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: April 3rd, 10:00 PM
  2. How To Create Columns?
    By TonyP in forum Coldfusion Database Access
    Replies: 5
    Last Post: April 26th, 03:49 PM
  3. AutoGenerateColumns on some columns
    By foldface@yahoo.co.uk in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: June 14th, 11:29 AM
  4. Columns and Inherited Datagrid...Active Schema does not support columns
    By rob thomson in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: September 4th, 03:09 PM
  5. Replies: 0
    Last Post: July 2nd, 06:18 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139