描述如下:
MSSQL版本为MSSQL Server 2000
知道表名 假设为 table1,写一个SQL要求按下列格式返回数据
下面是返回数据的一个例子
假设table1有两个索引index1和index2,
index1是主键,由字段A和B组成
index2是一个普通索引,由字段X,Y,Z字段构成
则返回的数据应该为
索引名称 序号 字段名称 索引类型
index1 1 A 主键
index1 2 B 主键
index2 1 X 普通索引
index2 2 Y 普通索引
index2 3 Z 普通索引
回答答案:
select c.name as '索引名称',d.keyno '序号',d.name '字段名称',
'索引类型'=case c.indid when 1 then '主键' else '普通索引' end
from
(select a.name name1,a.id as id,b.indid,b.name
from sysobjects a,sysindexes b
where a.id = b.id
) C,
(select c.id,d.name,c.colid,c.keyno,c.indid
from sysindexkeys c, syscolumns d
where c.colid = d.colid
and c.id = d.id
group by c.colid ,d.name,c.id,c.keyno,c.indid
) D
where c.id = d.id
and c.name1 = 'table1'
and c.indid = d.indid
group by c.name,C.indid ,d.name,d.keyno