Tuesday, December 17, 2013

List all table constraints, by table

This query will return  your table constraints, ordered by table: 

SELECT
     so.[Name] [ContraintName],
     tab.[Name] [Table],
     scol.[Name] [Column]
FROM

     sysobjects so INNER JOIN (
          SELECT [Name],[ID]
          FROM sysobjects
          WHERE XType = 'U'
    ) tab
       ON so.[Parent_Obj] = tab.[ID] INNER JOIN sysconstraints sc

         ON so.[ID] = sc.Constid INNER JOIN syscolumns scol
           ON sc.[ColID] = scol.[ColID]
           AND tab.[ID] = scol.[ID]
ORDER BY
     tab.[Name]

No comments:

Post a Comment