读取SQLServer 数据表结构
2006-11-07 20:46
471 查看
刚好需要这个过程, 在网上找了几个, 感觉都差一些, 其SQL都特别长. 而且我特别需要用到聚类索引, 自己写了个, 这是程序开发使用到的版本.
select o.name tablename, c.name fieldname, t.name fieldtype, columnproperty(c.id,c.name,'PRECISION') fieldlen, c.Scale,c.length,
c.colid fieldorder, c.isnullable,
case when c.colid in(select ik.colid
from sysindexes i, Sysindexkeys ik, sysobjects oo
where i.id=ik.id and i.indid=ik.indid
and i.name=oo.name and oo.xtype='PK' --主键
and o.id=i.id
) then 1 else 0 end isPrimaryKey,
case when c.colid in(select ik.colid
from sysindexes i, Sysindexkeys ik
where i.id=ik.id and i.indid=ik.indid
and o.id=i.id and i.indid=1 --聚类索引
) then 1 else 0 end isClusterKey,
columnproperty( c.id, c.name,'IsIdentity') IsIdentity,
isnull(m.text,'') defaultvalue
from sysobjects o, syscolumns c, systypes t, syscomments m
where o.xtype='U'
and o.id=c.id
and c.xtype=t.xtype
and c.cdefault*=m.id
order by o.name, c.colid
程序使用到的版本是否用0/1表示, 如果用在文档整理, 难看得要命, 优化了个文档版本的,在查询分析器找出来还是蛮好看的.
select case when c.colid=1 then o.name else '' end 表名,
c.colid 顺序,
c.name 字段名,
t.name 字段类型,
columnproperty(c.id,c.name,'PRECISION') 字段长度,
isnull(c.Scale,'') 小数位,
c.length 占用字节,
case when c.isnullable=1 then '√' else '' end 可为空,
case when c.colid in(select ik.colid
from sysindexes i, Sysindexkeys ik, sysobjects oo
where i.id=ik.id and i.indid=ik.indid
and i.name=oo.name and oo.xtype='PK' --主键
and o.id=i.id
) then '√' else '' end 主键,
case when c.colid in(select ik.colid
from sysindexes i, Sysindexkeys ik
where i.id=ik.id and i.indid=ik.indid
and o.id=i.id and i.indid=1 --聚类索引
) then '√' else '' end 聚类索引,
case when columnproperty( c.id, c.name,'IsIdentity')=1 then '√' else '' end 自增长,
isnull(m.text,'') 默认值
from sysobjects o, syscolumns c, systypes t, syscomments m
where o.xtype='U'
and o.id=c.id
and c.xtype=t.xtype
and c.cdefault*=m.id
order by o.name, c.colid
select o.name tablename, c.name fieldname, t.name fieldtype, columnproperty(c.id,c.name,'PRECISION') fieldlen, c.Scale,c.length,
c.colid fieldorder, c.isnullable,
case when c.colid in(select ik.colid
from sysindexes i, Sysindexkeys ik, sysobjects oo
where i.id=ik.id and i.indid=ik.indid
and i.name=oo.name and oo.xtype='PK' --主键
and o.id=i.id
) then 1 else 0 end isPrimaryKey,
case when c.colid in(select ik.colid
from sysindexes i, Sysindexkeys ik
where i.id=ik.id and i.indid=ik.indid
and o.id=i.id and i.indid=1 --聚类索引
) then 1 else 0 end isClusterKey,
columnproperty( c.id, c.name,'IsIdentity') IsIdentity,
isnull(m.text,'') defaultvalue
from sysobjects o, syscolumns c, systypes t, syscomments m
where o.xtype='U'
and o.id=c.id
and c.xtype=t.xtype
and c.cdefault*=m.id
order by o.name, c.colid
程序使用到的版本是否用0/1表示, 如果用在文档整理, 难看得要命, 优化了个文档版本的,在查询分析器找出来还是蛮好看的.
select case when c.colid=1 then o.name else '' end 表名,
c.colid 顺序,
c.name 字段名,
t.name 字段类型,
columnproperty(c.id,c.name,'PRECISION') 字段长度,
isnull(c.Scale,'') 小数位,
c.length 占用字节,
case when c.isnullable=1 then '√' else '' end 可为空,
case when c.colid in(select ik.colid
from sysindexes i, Sysindexkeys ik, sysobjects oo
where i.id=ik.id and i.indid=ik.indid
and i.name=oo.name and oo.xtype='PK' --主键
and o.id=i.id
) then '√' else '' end 主键,
case when c.colid in(select ik.colid
from sysindexes i, Sysindexkeys ik
where i.id=ik.id and i.indid=ik.indid
and o.id=i.id and i.indid=1 --聚类索引
) then '√' else '' end 聚类索引,
case when columnproperty( c.id, c.name,'IsIdentity')=1 then '√' else '' end 自增长,
isnull(m.text,'') 默认值
from sysobjects o, syscolumns c, systypes t, syscomments m
where o.xtype='U'
and o.id=c.id
and c.xtype=t.xtype
and c.cdefault*=m.id
order by o.name, c.colid
相关文章推荐
- 读取SQLServer 数据表结构
- 读取SqlServer表名及结构
- 使用SQLyog将sqlserver的数据及表结构迁移到mysql中
- 文章评论类,数据结构与读取方法
- SqlServer 树结构数据 子节点父节点的查询
- SQLServer中查询表结构(表主键 、列说明、列数据类型、所有表名)的Sql语句
- SQLSERVER的数据页面结构
- 在SQLSERVER中读取Oracle中的数据
- 无限级分类数据结构和读取分类的php代码
- mybatis实现读取树结构数据实例代码
- SQLSERVER读取ORACLE缺少数据,OPENQUERY只能读取一条数据
- sqlserver 表及结构的复制和数据插入
- sqlserver连接服务器读取Oracle数据,sqlserver读取oracle的ODBC配置
- SQLserver数据文件(MDF)的页面文件头结构剖析
- 第三章数程序设计初步--控制结构综合项目4-1文件操作初体验练习1从文件读取数据
- 从数据库中读取数据时报错,sqlserver默认值问题
- 从oracle数据表中读取表结构
- SQLServer 数据结构和基础数据导出工具
- SQLserver数据文件(MDF)的页面文件头结构剖析
- sqlserver数据导入mysql五:多线程导数据脚本(读取前面拆分的表名进行数据导入)