zoukankan      html  css  js  c++  java
  • SQL从SQL SERVER中获取数据库结构信息

    用SQL从SQL SERVER中获取数据库结构信息

    1.获得数据库名: select name as dbase_name from sysfiles where fileid='1'

    2. 表名: select name as tb_name from sysobjects where xtype='u' or xtype='pk' order by tb_name 

     3. 数据库中表结构(字段/主、外健): select SysObjects.Name as tb_name, SysColumns.Name as col_name, SysTypes.Name as col_type, SysColumns.Length as col_len, isnull(SysProperties.Value,SysColumns.Name) as col_memo, case when SysColumns.name in (select 主键=a.name FROM syscolumns a inner join sysobjects b on a.id=b.id and b.xtype='U' and b.name<>'dtproperties' where exists(SELECT 1 FROM sysobjects where xtype='PK' and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid ))) and b.name=SysObjects.Name ) then 1 else 0 end as is_key FROM SysObjects LEFT JOIN SysColumns ON Syscolumns.Id = Sysobjects.Id LEFT JOIN SysProperties ON (Syscolumns.Id = Sysproperties.Id AND Syscolumns.Colid = Sysproperties.Smallid) LEFT JOIN SysTypes ON SysTypes.Name <> 'sysname' and SysTypes.XType = Syscolumns.XType WHERE (Sysobjects.Xtype ='u' OR Sysobjects.Xtype ='v') AND Sysobjects.Name Like 'web_do' ORDER By SysObjects.Name, SysColumns.colid

    4. 视图名:

     select name as view_name from sysobjects where xtype='v' order by view_name 

     5.存储过程:

     select name as sp_name from sysobjects where xtype='p' order by tb_name 

     三:整合部分

    1. 把数据库名、表名、字段、key值、视图以一张表的形式表示:

     SELECT sysfiles.name as dbase_name,SysObjects.Name as tb_name, SysColumns.Name as col_name, SysTypes.Name as col_type, SysColumns.Length as col_len, isnull(SysProperties.Value,SysColumns.Name) as col_memo, case when SysColumns.name in (select 主键=a.name FROM syscolumns a inner join sysobjects b on a.id=b.id and b.xtype='U' and b.name<>'dtproperties' where exists(SELECT 1 FROM sysobjects where xtype='PK' and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid ))) and b.name=SysObjects.Name ) then 1 else 0 end as is_key FROM SysColumns LEFT JOIN SysTypes on SysTypes.XType = Syscolumns.XType LEFT JOIN SysObjects on Sysobjects.Id = Syscolumns.Id LEFT JOIN sysfiles on SysObjects.userstat=sysfiles.groupid LEFT JOIN SysProperties ON (Syscolumns.Id = Sysproperties.Id AND Syscolumns.Colid = Sysproperties.Smallid) WHERE (Sysobjects.Xtype ='u' OR Sysobjects.Xtype ='v') AND Sysobjects.Id = Syscolumns.Id AND SysTypes.XType = Syscolumns.XType AND SysTypes.Name <> 'sysname'and sysfiles.groupid=SysObjects.userstat AND Sysobjects.Name Like 'STORER' ORDER By SysObjects.Name, SysColumns.colid 2. 上面的SQL语句中dbase_name是要查表的数据库名,tb_name是要查的表名,col_name是所查表的字段名,col_type是所查表的字段类型,col_len是所查表的字段长度,is_key所查表的key值。把这些属性更形象的表示如下: dbase_name tb_name col_name col_type col_len is_key

    注:本文转自网络!!!

  • 相关阅读:
    【08】Python数据分析基础
    C#类和类成员初始化顺序
    C# 在子窗体调用父窗体的值(转)
    求实对称阵的 特征值 和 特征向量(转)
    Dev express 笔记
    Arcgis桌面开发,Python引用GDAL库的方法
    操作系统学习笔记--第三章--进程
    操作系统学习笔记--第一章--计算机系统概述
    GIS专业分析方法(待更新)
    C#中不用安装Oracle客户端连接Oracle数据库(转)
  • 原文地址:https://www.cnblogs.com/YuanSong/p/2610373.html
Copyright © 2011-2022 走看看