日期:2014-05-17  浏览次数:20472 次

约束问题?
SELECT
  t.name AS table_name,
  o.name AS constraint_name,
  o.type_desc AS type_desc,
  --OBJECT_DEFINITION(o.object_id),
  c.name AS column_name
-- ty.name AS type_name
FROM sys.tables AS t
  JOIN sys.columns AS c
ON t.object_id=c.object_id
-- JOIN sys.types AS ty
--ON c.system_type_id=ty.system_type_id
  JOIN sys.objects AS o
ON t.object_id=o.parent_object_id
  AND o.type_desc LIKE '%_CONSTRAINT'
ORDER BY table_name
这个查到的主键约束显示所有键都是,有没有跟好的语句?

------解决方案--------------------
SQL code
SELECT  OBJECT_NAME(parent_obj) tname ,
 a.name AS indexname,b.name as columnname
    FROM    sys.sysobjects a inner join sys.syscolumns b on a.parent_obj=b.id
    WHERE   a.xtype IN ( 'pk', 'uq' )            
    AND OBJECT_NAME(parent_obj) = '表名' and b.name='列名'

------解决方案--------------------
SQL code
SELECT  OBJECT_NAME(parent_obj) tname ,
        a.name AS indexname ,
        b.name AS columnname
FROM    sys.sysobjects a
        INNER JOIN sys.syscolumns b ON a.parent_obj = b.id
WHERE   a.xtype IN ( 'pk', 'uq' )
        AND OBJECT_NAME(parent_obj) IN ( SELECT DISTINCT
                                                name
                                         FROM   sys.tables )--and b.name='列名'