select avg(t1.a) from table1 t1,table1 t2  where t1.a=t2.a
group by t1.a
having count(*)>1如果有记录出来,说明a有重复
没有经过测试,但思路应该可以实现

解决方案 »

  1.   

    select col from table group by col having count(*)>1
      

  2.   

    对,CrazyFor(Fan) 的更为简洁
      

  3.   

    不对啊!各位。你们的做法是要求数据库里有记录。我要知道一个字段是不是设计成Unique型的。比如说该字段被设定成index时有可能被限定成Unique。
      

  4.   

    sysconstraints
    包含约束映射,映射到拥有该约束的对象。该系统目录存储在每个数据库中。列名 数据类型 描述 
    constid int 约束号。 
    id int 拥有该约束的表 ID。 
    colid smallint 在其上定义约束的列 ID,如果是表约束则为 0。 
    spare1 tinyint 保留。 
    status int 位图指示状态。可能的值包括: 
    1 = PRIMARY KEY 约束。
    2 = UNIQUE KEY 约束。
    3 = FOREIGN KEY 约束。
    4 = CHECK 约束。
    5 = DEFAULT 约束。
    16 = 列级约束。
    32 = 表级约束。
     
    actions int 保留。 
    error int 保留。 
      

  5.   

    if exists (select 1 from sysconstraints a,syscolumns b where a.id =object_id('tablename') and b.id= object_id('tablename') and a.
    status =2  and a.colid = b.colid and b.name = 'colname') print '有UNIQUE KEY 约束' else print '没有UNIQUE KEY 约束'
      

  6.   

    status =2 有问题,上面是不对的。
      

  7.   


    if exists (select 1 from sysconstraints a,syscolumns b where a.id =object_id('test_key') and b.id= object_id('test_key') and 
    OBJECTPROPERTY (a.constid,'IsUniqueCnst') =1 and ((a.colid = b.colid and b.name = 'a') or (a.colid=0)))  print '有UNIQUE KEY 约束' else print '没有UNIQUE KEY 约束'
      

  8.   

    sky_blue(老衲) 查出来的结果好象都是显示'没有UNIQUE KEY 约束',最简单的办法,你打开设计表一看不就知道了吗?有必要那么复杂嘛?
      

  9.   

    我需要的是在给出数据表名的前体下,经过SQL查寻得到一个行集。
    每行记录对应该数据表中的一个字段的信息,行集有两列:ColName(字段名)和IsUnique(是否唯一)
    用print打出来的结果我在程序里没法用。
      

  10.   

    select ColName=name,IsUnique=(case colstat when 2 then 'Yes' else 'No' end) from syscolumns where id*=(select id from sysobjects where name='YourTableName')
      

  11.   

    colstat为2就是Unique?
    Help中说syscolumns.colstat是"For internal use only."
      

  12.   

    另外这段SQL也不能得到正确的结果。我设置的一个Unique Index字段总是显示No