Knowing constraint type,column names (on which constraint is applied) of a table dynamically

  • Hi. I want to know at run time the names of the columns of a table on which either Primary Key or Unique key constraint is applied along with the type of the constraint. I tried using sysobjects,sysconstraints,sysindexkeys,syscolumns system tables . But I am unable to relate these tables

  • This might serve as a starting point

     

    SELECT

     a.id AS Tableid, a.Name AS TableName, b.name AS colName, b.colid AS colId,

     d.name AS constraintname, d.xtype AS constrainttype , e.name

    FROM

     ((((sysobjects a INNER JOIN syscolumns b ON  a.id=b.id)

     LEFT OUTER JOIN sysconstraints c ON c.id=b.id AND c.colid=b.colid-1 )

     LEFT OUTER JOIN sysobjects d ON d.parent_obj=a.id AND c.constid=d.id)

     LEFT OUTER JOIN sysobjects e ON e.parent_obj=a.id AND e.xtype='TR')

    WHERE

     a.xtype= 'U'

    ORDER BY

     1, 2, b.colid

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • hi!

    have a look at: http://www.sqlservercentral.com/scripts/contributions/246.asp

    best regards, chris.

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

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