输出表结构描述

Use dxhr
Go
;With CTE_s As
(
Select 
        c.Name,
        isnull(f.[value],'')    As [表说明],
        a.Column_id                As [序号],
        a.Name                    As [字段名],
        b.Name                    As [类型],
        case 
            when exists(select 1 from sys.objects where parent_object_id=a.object_id and type=N'PK' and name in
                (select Name from sys.indexes where index_id in
                    (select indid from dbo.sysindexkeys where ID=a.object_id and colid=a.column_id)))
                then '√' 
            else '' 
        end                        As [主键],
        case 
            when is_identity=1 then '√' 
            else '' 
        end                        As [标识],
        case 
            when a.[max_length]=-1 and b.Name!='xml' then 'max/2G' 
            when b.Name='xml' then ' 2^31-1字节/2G'
            else rtrim(a.[max_length]) 
        end                        As [占用字节数],
        ColumnProperty(a.object_id,a.Name,'Precision')    As [长度],
        isnull(ColumnProperty(a.object_id,a.Name,'Scale'),0) As [小数位数],
        case 
            when a.is_nullable=1 then '√' 
            else '' 
        end                        As [允许空],
        isnull(d.text,'')        As [默认值],
        isnull(e.[value],'')    As [字段说明],
        Row_number() Over(Partition By c.Name Order By a.Column_id) As Row
    From  sys.columns                            As a
        Left Outer Join  sys.types                As b on a.user_type_id=b.user_type_id
        Inner Join sys.objects                    As c on a.object_id=c.object_id and c.Type='U'
        Left Outer Join dbo.syscomments            As d on a.default_object_id=d.ID
        Left Outer Join sys.extended_properties As e on e.major_id=c.object_id and e.minor_id=a.Column_id and e.class=1
        Left Outer Join sys.extended_properties    As f on f.major_id=c.object_id and f.minor_id=0 and f.class=1
    Where c.is_ms_shipped=0
        And Not Exists (Select 1 
                            From  sys.extended_properties 
                            Where major_id=c.Object_id 
                             And minor_id = 0  
                             And class = 1  
                             And Name = N'microsoft_database_tools_support'
                        )

)
Select 
         [表名]=Case row When 1 Then Name Else '' End ,
         [表说明],
         [序号],
         [字段名],
         [类型],
         [主键],
         [标识],
         [占用字节数],
         [长度],
         [小数位数],
         [允许空],
         [默认值],
         [字段说明]
    From CTE_s
    Order By Name,Row
    

 

你可能感兴趣的:(C++,c,C#,F#,Microsoft)