关于取MSSQL 2000索引信息的sql

 

描述如下:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值