zoukankan      html  css  js  c++  java
  • 使用SQL语句导出数据库表结构定义,便于生成数据库定义文档

    平时开发项目时,经常需要将数据库的表结构记录为文档,便于今后维护时查阅,可以使用以下SQL语句得到表结构定义,方便拷贝到文档:

    select --t.object_id,
        t.name as tablename
        ,c.name 
    as columnname
        ,
    case when ty.name='varchar' then 
                
    case when c.max_length=-1 
                    
    then ty.name + '(max)'
                    
    else ty.name + '(' + convert(varchar, c.max_length)  + ')'
                
    end
               
    when ty.name='nvarchar' then 
                
    case when c.max_length=-1 
                    
    then ty.name + '(max)'
                    
    else ty.name + '(' + convert(varchar, c.max_length/2)  + ')'
                
    end
               
    when ty.name='numeric' then 
                 ty.name 
    + '(' + convert(varchar, c.precision+ ',' + convert(varchar, c.scale)  + ')'
            
    else ty.name 
         
    end as typename
        ,
    case when c.is_identity=1 
            
    then 'Yes'
            
    else 'No'
         
    end as is_identity
        ,
    case when c.is_nullable=1 
            
    then 'Yes'
            
    else 'No'
         
    end as is_nullable
        ,c.max_length
        ,c.
    precision
        ,(
    select value
            
    from sys.extended_properties as ex
            
    where ex.major_id = c.object_id
                
    and ex.major_id = c.column_id) as notes
        ,scale
    from sys.tables as t 
        
    left join sys.columns as c 
            
    on c.object_id=t.object_id 
            
    inner join  
                (
    select name
                    ,system_type_id
                 
    from sys.types 
                
    where name<>'sysname'as ty 
            
    on c.system_type_id=ty.system_type_id 
    --where t.name like 'VS_%'   --filter table name
    order by t.name, c.column_id 

    执行以上语句后的结果

  • 相关阅读:
    弹窗多内容,灵活布局计算方式总结
    暖场广告设计方案
    UIStackView上手教程
    多弹窗排序总结
    常用的code snipper
    iOS开发常用技能点(持续更新中。。。)
    32位和64位系统区别及int字节数
    liunx环境,摄像头无法识别,解决方案
    TCP/IP 笔记 7 Ping
    TCP/IP 笔记 6 netstat -s 命令查看每个协议统计数据
  • 原文地址:https://www.cnblogs.com/andy65007/p/1694915.html
Copyright © 2011-2022 走看看