• MSSQL 查询表结构


    -- 查询 数据库 视图

    SELECT * FROM sysdatabases ;

    -- 查询 表 视图

    SELECT * from sysobjects
    WHERE 1 = 1 AND XTYPE IN ('U','V')
    order by name;

      

    /**

    * 查询 列 视图

    * OBJECT_Id: sysobjects.Id 可以通过 object_id('你的表名') 转换为id

    */

    SELECT * FROM syscolumns ;

      

    /**

    * 查询  注释  视图

    * major_id: sysobjects.Id

    * minor_id: 0 ? 表注释 : 对应的列注释[syscolumns.Column_Id]

    */

    SELECT * FROM sys.extended_properties;

    /**

    * 主键

    */

    SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE  
    --WHERE TABLE_NAME='PDA_xjData'

      

    /**

    * 查询表结构

    */

    SELECT O.name AS 'TableName', C.name AS 'ColumnName',T.name AS 'Type'
           , C.IsNullAble      
           , CASE T.name WHEN 'char' THEN C.prec WHEN 'varchar' THEN C.prec WHEN 'nvarchar' THEN C.prec
                  WHEN 'text' THEN C.prec WHEN 'ntext' THEN C.prec
                  WHEN 'decimal' THEN C.xprec
                  ELSE C.length END AS 'Length'
           , C.scale
           , C.id TableId
           , C.colid ColumnId
           , S.value AS Comment
           , ST.value AS TableComment
           , CASE WHEN K.COLUMN_NAME IS NULL THEN 0 ELSE 1 END IsPk
           , C.*
    FROM sysobjects O
    INNER JOIN syscolumns C ON C.id = O.id
    INNER JOIN systypes T ON T.xusertype = C.xusertype
    INNER JOIN sys.extended_properties S ON S.major_id = C.id AND S.minor_id = C.colid
    LEFT JOIN sys.extended_properties ST ON ST.major_id = C.id AND ST.minor_id = 0
    LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE K ON K.TABLE_NAME = O.name AND K.COLUMN_NAME = C.name
    WHERE 1 = 1
           AND O.XTYPE IN ('U','V') 
    ORDER BY O.name, C.colid;
  • 相关阅读:
    XMLHttpRequest 跨域问题
    jQuery+AJAX实现纯js分页功能
    PHP验证码
    PHP基础
    UIView易忽略点
    UITableView  优化
    SpringBoard界面层级结构分析
    给App在“设置”中添加选项(类似招行App)
    通过USB线SSH登陆到越狱手机上(命令行模式的),不通过wifi
    在IOS项目中使用Lua
  • 原文地址:https://www.cnblogs.com/Cailf/p/15094533.html
Copyright © 2020-2023  润新知