• 查询指定表的数据类型和长度


    use xin20170628  --指定数据库
    go
    --只能查询出类型,主键和顺序
    select SO.name as TableName,SC.name as TableCloumn,ST.name as DataType,
    (
    SELECT COUNT(1) AS Is_PK 
     FROM  syscolumns  
     JOIN  sysindexkeys  ON   syscolumns.id=sysindexkeys.id  AND  syscolumns.colid=sysindexkeys.colid   
     JOIN  sysindexes   ON   syscolumns.id=sysindexes.id  AND  sysindexkeys.indid=sysindexes.indid  
     JOIN  sysobjects   ON   sysindexes.name=sysobjects.name  AND  sysobjects.xtype='PK'
     WHERE syscolumns.name=SC.name AND syscolumns.id=object_id(SO.name)
    ) as IsPrimaryKey
    ,SC.colid as CloumnIndex 
    from sysobjects SO 
    inner join syscolumns SC on SO.id = SC.id  and  SO.xtype = 'U' and  SO.status >= 0 and SO.name= '表名'
    inner join systypes ST on SC.xtype = ST.xusertype 
    order by SO.name asc, SC.colorder asc
    
    
    ------------------------------------------------------------------------
    go
    --查询出字段类型和长度
    select o.name as 表名, c.name as 字段名称, t.name as 字段类型, c.length as 字段长度 
    from 
    syscolumns c inner join 
    sysobjects o on c.id = o.id and o.xtype = 'u' inner join 
    systypes t on c.xtype = t.xtype
    where o.name = '表名'   --查询的表名
    ---------------------------------------------------------------------------------------
    --查询表的字段类型,没有长度
    select column_name,data_type from information_schema.columns 
    where table_name = '表名'
    ----------------------------------------------------------------------------------------
    --列出所有表: 
    
    SELECT (CASE WHEN a.colorder=1 THEN d.name ELSE '' END)表名
    ,a.colorder 字段序号
    ,a.name 字段名
    ,(CASE WHEN COLUMNPROPERTY(a.id ,a.name ,'IsIdentity')=1 THEN '√' ELSE ''
    END) 标识,(CASE WHEN (SELECT COUNT(*)FROM sysobjects WHERE (
    NAME IN (SELECT NAME FROM sysindexes WHERE (id=a.id) AND (indid IN (SELECT 
    indid FROM sysindexkeys WHERE (id = a.id) AND (colid IN (SELECT colid
    FROM syscolumns WHERE (id = a.id) AND (NAME = a.name)))))))
    AND (xtype='PK'))>0 THEN '√'ELSE '' END) 主键,b.name 类型,a.length 占用字节数
    ,COLUMNPROPERTY(a.id ,a.name ,'PRECISION') AS 长度,ISNULL(COLUMNPROPERTY(a.id ,a.name ,'Scale') ,0) AS 小数位数
    ,(CASE WHEN a.isnullable=1 THEN '√' ELSE '' END) 允许空,ISNULL(e.text ,'') 默认值 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
    ORDER BY a.id ,a.colorder
    

      

  • 相关阅读:
    selenium浏览器参数设置详解——转
    py打包工具
    BurpSuite暴力破解和防御实战
    费马小定理(确定n 是否为素数)
    如何实现一个RPC框架1 —— RPC简介和通信协议的定义
    如何实现一个RPC框架2 ——用Netty实现协议通信
    15.三数之和
    flex开发零碎笔记,随时补充
    转“国内图片网站Yupoo的架构”
    转“经验分享:大型高并发高负载网站的系统架构 ”
  • 原文地址:https://www.cnblogs.com/accumulater/p/7113167.html
Copyright © 2020-2023  润新知