create table testtable(c1 int NOT NULL, c2 char(6) NOT NULL, c3 char(7) NOT NULL)
create INDEX IDX5 ON testtable (c2,c1,c3)select c.name,b.name,a.keyno from dbo.sysindexkeys a,syscolumns b,sysindexes c 
where a.id=b.id and a.colid=b.colid and a.id=object_id('testtable') and a.indid=c.indid and c.name='idx5'
drop table testtable

解决方案 »

  1.   

    Hi wgsasd311(自强不息), 谢谢你的回复,我试了一下:
    drop table testtable
    create table testtable(c1 int NOT NULL, c2 char(6) NOT NULL, c3 char(7) NOT NULL)
    create INDEX IDX5 ON testtable (c2,c1,c3)
    然后:
    select c.name,b.name,a.keyno from dbo.sysindexkeys a,syscolumns b,sysindexes c where a.id=b.id and a.colid=b.colid and a.id=object_id('testtable') and a.indid=c.indid and c.name='idx5'
    得到这样的结果:
    name    name    keyno
    IDX5    c1      2
    IDX5    c2      1
    IDX5    c3      3你能解释下这些数字的意思吗?或者,我如何利用这些数字得到我想要的“一个index所涉及到的列的数目”?谢谢!
      

  2.   

    --分别表示,INDEX列名,和表列名,及列在索引中位置,要得到列的数据很简单啊,是3个啊,不是显示得清清楚楚吗?drop table testtable
    create table testtable(c1 int NOT NULL, c2 char(6) NOT NULL, c3 char(7) NOT NULL)
    create INDEX IDX5 ON testtable (c2,c1,c3)
    select c.name,列数目=count(b.name) from dbo.sysindexkeys a,syscolumns b,sysindexes c where a.id=b.id and a.colid=b.colid and a.id=object_id('testtable') and a.indid=c.indid and c.name='idx5' group by c.name
      

  3.   

    --or
    drop table testtable
    create table testtable(c1 int NOT NULL, c2 char(6) NOT NULL, c3 char(7) NOT NULL)
    create INDEX IDX5 ON testtable (c2,c1,c3)
    select 列数目=count(b.name) from dbo.sysindexkeys a,syscolumns b,sysindexes c where a.id=b.id and a.colid=b.colid and a.id=object_id('testtable') and a.indid=c.indid and c.name='idx5'
      

  4.   

    多谢 wgsasd311(自强不息)!搞定了!