zoukankan      html  css  js  c++  java
  • How to display table and its fields property

    -- 数据库表结构详细信息查看
    select
    [name] 表名,'' 字段名,'' 数据类型,'' 实际长度,'' 字段特性,'' 字段序号,'' 是否自增列,'' 是否计算列 ,
    isnull((select value from sys.extended_properties p where p.name= 'MS_Description'
    and p.class='1' and p.major_id = o.id and p.minor_id=0 ), '') as 备注
    from sysobjects o where o.type = 'U'
    union

    SELECT o.name 表名,c.name 字段名,t.name +
    case t.name
    when 'nvarchar' then '('+ case convert(varchar(10), c.prec) when '-1' then 'MAX' else convert(varchar(10), c.prec) end +')'
    when 'nchar' then '('+ convert(varchar(10), c.prec)+')'
    when 'char' then '('+ convert(varchar(10), c.prec)+')'
    when 'varchar' then '('+ case convert(varchar(10), c.prec) when '-1' then 'MAX' else convert(varchar(10), c.prec) end +')'
    when 'decimal' then '('+ convert(varchar(10), c.prec)+','+ convert(varchar(10), c.scale)+')'
    when 'numeric' then '('+ convert(varchar(10), c.prec)+','+ convert(varchar(10), c.scale)+')'
    else ''
    end as 数据类型,
    case when c.length = '-1' then 'MAX' else convert(varchar(10), c.length) end as 实际长度,
    case
    when (c.isnullable =0 and k.colid>0) then '主键'
    when c.isnullable =0 then '非空'
    else '' end as 字段特性,
    --convert(varchar(10), c.colid) 字段序号,
    c.colid 字段序号,
    case when COLUMNPROPERTY(c.id,c.NAME,'IsIdentity') = 1 then '是' else '' end as 是否自增列,
    case when COLUMNPROPERTY(c.id,c.NAME,'IsComputed') = 1 then '是' else '' end as 是否计算列,
    --isnull(convert(varchar(10),k.colid), '') as 主键序号,
    isnull((select value from sys.extended_properties p
    where p.name= 'MS_Description' and p.class='1' and p.major_id = c.id
    and p.minor_id=c.colorder ),'' ) as 备注
    FROM syscolumns c
    join sysobjects o on o.id = c.id and o.type = 'U'
    left join systypes t on c.xusertype = t.xusertype
    left join sysindexes i on i.id = c.id and exists(select 1 from sysobjects where xtype = 'pk' and name = i.name)
    left join sysindexkeys k on k.id = c.id and i.indid = k.indid and k.colid = c.colid
    order by 表名, 字段序号


    Running against differenct database ,you will get different result.

  • 相关阅读:
    什么是Web Service?
    按钮上显示值的轮流切换
    跟偶一起做:击退眼睛疲劳的五大运动
    Windows下权限设置详解
    实现数据分类汇总的SQL语句
    毕业不吼不快十首经典歌曲
    使用命令查看自己的外网IP地址
    如何面对30岁?
    JavaScript中this关键字使用方法详解
    发掘WinRAR的“自解压安装”功能
  • 原文地址:https://www.cnblogs.com/JamesLi2015/p/2984288.html
Copyright © 2011-2022 走看看