zoukankan      html  css  js  c++  java
  • 数据库信息查询(作者不是我)

    //查询数据库摘要
                select_str[0] = "SELECT DB_NAME() AS 数据库, "
                                + "(select sys.extended_properties.name from sys.extended_properties where sys.extended_properties.class=0) as 说明,"
                                + "(select sys.extended_properties.value from sys.extended_properties where sys.extended_properties.class=0) as 属性";
                //查询表摘要:表的名称,列的数量,注释
                select_str[1] = "SELECT sys.tables.name AS 表名,count(sys.columns.column_id) AS 列的个数,sys.extended_properties.value AS 描述 "
                                + "FROM sys.tables LEFT JOIN sys.columns ON sys.tables.object_id=sys.columns.object_id LEFT JOIN sys.extended_properties ON sys.tables.object_id = sys.extended_properties.major_id "
                                + "WHERE sys.tables.type ='U' and sys.tables.name <>'dtproperties' and sys.tables.name <> 'sysdiagrams' and (sys.extended_properties.minor_id=0 or sys.extended_properties.minor_id IS NULL) "
                                + "GROUP BY sys.tables.name ,sys.extended_properties.value "
                                + "ORDER BY sys.tables.name";
                //查询视图摘要:视图名称,列的数量,说明
                select_str[2] = "SELECT sys.views.name AS 视图,count(sys.columns.column_id) AS 列的个数,sys.extended_properties.value AS 说明 "
                                + "FROM sys.views LEFT JOIN sys.columns ON sys.views.object_id=sys.columns.object_id "
                                    + " left join sys.extended_properties on sys.views.object_id=sys.extended_properties.major_id  "
                                + "WHERE sys.views.type ='V' and sys.extended_properties.name= '说明'  "
                                + "GROUP BY sys.views.name ,sys.extended_properties.value "
                                + "ORDER BY sys.views.name";
                //查询数据表的详细信息
                select_str[3] = "SELECT "
                                //+ "表名=case   when   a.colorder=1   then   d.name   else   ''   end,"
                                + "表名=d.name,"
                                + "字段序号=a.colorder,"
                                + "字段名=a.name, "
                                + "索引=case   when   exists(SELECT 1 FROM   sys.index_columns   WHERE sys.index_columns.column_id=a.colorder and sys.index_columns.object_id=d.id) then   '存在'   else   ''   end,"
                                + "标识=case   when   COLUMNPROPERTY(   a.id,a.name,'IsIdentity')=1   then   '是'   else   ''   end, "
                                + "主键=case   when   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     )))   then   '是'   else   ''   end,"
                                + "类型=b.name,"
                                + "占用字节数=a.length, "
                                + "长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),"
                                + "小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),"
                                + "允许空=case   when   a.isnullable=1   then   '允许空'else   '不允许'   end,"
                                + "默认值=isnull(e.text,''), "
                                + "字段说明=isnull(g.[value],'')"
                                + "  FROM   syscolumns   a   left   join   systypes   b   on   a.xtype=b.xusertype  "
                                    + "inner   join   sysobjects   d   on   a.id=d.id     and   d.xtype='U'   and     d.name<>'dtproperties'  "
                                    + "left   join   syscomments   e   on   a.cdefault=e.id "
                                    + "left   join   sys.extended_properties   g   on   d.id=g.major_id   and   a.colid=g.minor_id  "
                                    +"where d.name <>'sysdiagrams'  "
                                    + "order   by   d.name,a.id,a.colorder ";
                //查询数据视图的详细信息
                select_str[4] = "SELECT  sys.views.name AS 视图,sys.syscomments.text AS 定义  "
                                + "FROM  sys.views LEFT JOIN sys.syscomments ON sys.views.object_id=sys.syscomments.id";
                //查询外键详细信息
                select_Dic[5] = "select b.name as 父,a.name as 外键名称,d.name as 列,c.name as 子,e.name as 列,sys.extended_properties.value as 说明 "
                                + "from sys.foreign_key_columns inner join sys.objects as a on sys.foreign_key_columns.constraint_object_id=a.object_id  "
                                + "inner join sys.objects as b on sys.foreign_key_columns.parent_object_id=b.[object_id]  "
                                + "inner join sys.objects as c on sys.foreign_key_columns.referenced_object_id=c.[object_id]  "
                                + "inner join sys.syscolumns as d on (sys.foreign_key_columns.parent_column_id=d.colorder and sys.foreign_key_columns.parent_object_id=d.id)  "
                                + "inner join sys.syscolumns as e on (sys.foreign_key_columns.referenced_column_id=e.colorder and sys.foreign_key_columns.referenced_object_id=e.id)  "
                                + "left join sys.extended_properties on sys.foreign_key_columns.constraint_object_id=sys.extended_properties.major_id   "
                                + "group by b.name ,a.name,d.name,c.name,e.name ,sys.extended_properties.value";
                //查询索引信息--主键的索引信息
                select_Dic[6] = "select sys.objects.name as 表名,sys.indexes.name as 索引的名字,"
                                + "case sys.indexes.type when 0 then '堆'when 1 then '聚集'when 2 then '非聚集' when 3 then 'XML' end as 索引的类型,"
                                + "case sys.index_columns.is_descending_key when 0 then '升序排序' when 1 then '降序排序' end as 是否降序,"
                                + "sys.syscolumns.name as  列名  "
                                + "from sys.index_columns left join sys.objects on sys.index_columns.object_id = sys.objects.object_id "
                                    + "inner join sys.indexes on (sys.index_columns.index_id = sys.indexes.index_id and sys.index_columns.object_id=sys.indexes.object_id)  "
                                    + "left join sys.syscolumns on (sys.index_columns.object_id=sys.syscolumns.id and sys.index_columns.column_id=sys.syscolumns.colorder)  "
                                + "where sys.objects.type ='U' and sys.objects.name  not in ('dtproperties','sysdiagrams') and sys.indexes.is_primary_key =1   "
                                + "group by sys.objects.name,sys.indexes.name,sys.indexes.type,sys.index_columns.is_descending_key,sys.syscolumns.name ";

                // 查询存储过程信息
                select_Dic[7] = "select sys.procedures.name as 存储过程 ,count (sys.parameters.parameter_id) as 参数个数,sys.extended_properties.value as 说明  "
                                + "from sys.procedures left join sys.parameters on(sys.procedures.object_id = sys.parameters.object_id)  "
                                    + "left join sys.extended_properties on (sys.procedures.object_id=sys.extended_properties.major_id)  "
                                + "where sys.procedures.is_ms_shipped =0 and sys.procedures.name NOT IN ('sp_alterdiagram','sp_creatediagram','sp_dropdiagram','sp_helpdiagramdefinition','sp_helpdiagrams','sp_renamediagram','sp_upgraddiagrams')  "
                                + "group by sys.procedures.name,sys.extended_properties.value  "
                                + "order by sys.procedures.name";

                //查询函数信息
                select_Dic[8] = "select sys.objects.name as 函数名称,count (sys.parameters.parameter_id) as 参数个数 ,sys.extended_properties.value as 说明  "
                                + "from sys.objects left join sys.parameters on(sys.objects.object_id = sys.parameters.object_id)  "
                                    + "left join sys.extended_properties on (sys.objects.object_id=sys.extended_properties.major_id)  "
                                + "where sys.objects.type  in ('FN','TF','AF')and (sys.objects.name<>'fn_diagramobjects')  "
                                + "group by sys.objects.name,sys.extended_properties.value   "
                                + "order by sys.objects.name";

                //查询非主键的索引信息
                select_Dic[9] = "select sys.objects.name as 表名,sys.indexes.name as 索引的名字, "
                                + "case sys.indexes.type when 0 then '堆'when 1 then '聚集'when 2 then '非聚集' when 3 then 'XML' end as 索引的类型,"
                                + "case sys.index_columns.is_descending_key when 0 then '升序排序' when 1 then '降序排序' end as 是否降序, "
                                + "sys.syscolumns.name as  列名,"
                                + "sys.extended_properties.value as 说明   "
                                + "from sys.index_columns left join sys.objects on sys.index_columns.object_id = sys.objects.object_id  "
                                    + "inner join sys.indexes on (sys.index_columns.index_id = sys.indexes.index_id and sys.index_columns.object_id=sys.indexes.object_id)  "
                                    + "left join sys.syscolumns on (sys.index_columns.object_id=sys.syscolumns.id and sys.index_columns.column_id=sys.syscolumns.colorder)  "
                                    + "left join sys.extended_properties on (sys.index_columns.object_id =sys.extended_properties.major_id and sys.index_columns.index_id =sys.extended_properties.minor_id)  "
                                + "where sys.objects.type ='U' and sys.objects.name <>'dtproperties' and sys.indexes.is_primary_key <>1 and sys.extended_properties.name ='MS_Description'  "
                                + "group by sys.objects.name,sys.indexes.name,sys.indexes.type,sys.index_columns.is_descending_key,sys.syscolumns.name,sys.extended_properties.value ";

  • 相关阅读:
    精选PSD素材下载周刊【Goodfav PSD 20130720】
    25个吸引眼球的广告设计
    25个让人无法抗拒的HTML5网站设计实例
    10个非常有用的网页设计工具
    为网页设计师准备的30个使用的HTML5框架
    ElasticSearch
    Dism命令实操
    Bash Shell
    Lua语言学习
    开发命令
  • 原文地址:https://www.cnblogs.com/chenleinet/p/1326844.html
Copyright © 2011-2022 走看看