码迷,mamicode.com
首页 > 数据库 > 详细

Sql Server 获取所有的列

时间:2017-10-03 00:23:01      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:结果   ima   语句   type   alt   src   select   nullable   软实力   

目标数据库为微软实力数据库:AdventureWorks2012

1、Sql 语句:

SELECT DISTINCT
        c.TABLE_SCHEMA ,
        c.TABLE_NAME ,
        c.COLUMN_NAME ,
        c.DATA_TYPE ,
        c.IS_NULLABLE ,
        tc.CONSTRAINT_TYPE ,
        CONVERT(INT, c.NUMERIC_PRECISION) NUMERIC_PRECISION ,
        c.NUMERIC_SCALE ,
        c.CHARACTER_MAXIMUM_LENGTH ,
        c.ORDINAL_POSITION ,
        tc.CONSTRAINT_NAME ,
        COLUMNPROPERTY(OBJECT_ID(c.TABLE_SCHEMA + . + c.TABLE_NAME),
                       c.COLUMN_NAME, IsIdentity) ISIDENTITY ,
        ( SELECT    CASE WHEN COUNT(1) = 0 THEN 0
                         ELSE 1
                    END
          FROM      INFORMATION_SCHEMA.TABLE_CONSTRAINTS x
                    INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccux ON c.TABLE_NAME = ccux.TABLE_NAME
                                                              AND c.COLUMN_NAME = ccux.COLUMN_NAME
                                                              AND c.TABLE_SCHEMA = ccux.TABLE_SCHEMA
          WHERE     x.CONSTRAINT_TYPE = UNIQUE
                    AND x.TABLE_SCHEMA = ccux.TABLE_SCHEMA
                    AND x.CONSTRAINT_NAME = ccux.CONSTRAINT_NAME
        ) ISUNIQUE
FROM    INFORMATION_SCHEMA.COLUMNS c
        LEFT OUTER JOIN ( INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
                          JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON ( tc.TABLE_SCHEMA = ccu.TABLE_SCHEMA
                                                              AND tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME
                                                              AND NOT tc.CONSTRAINT_TYPE IN (
                                                              CHECK,
                                                              UNIQUE )
                                                              )
                        ) ON ( c.TABLE_SCHEMA = ccu.TABLE_SCHEMA
                               AND c.TABLE_NAME = ccu.TABLE_NAME
                               AND c.COLUMN_NAME = ccu.COLUMN_NAME
                             )
ORDER BY c.TABLE_SCHEMA ,
        c.TABLE_NAME ,
        c.COLUMN_NAME; 

2、结果:

技术分享

 

Sql Server 获取所有的列

标签:结果   ima   语句   type   alt   src   select   nullable   软实力   

原文地址:http://www.cnblogs.com/jeffrey-net/p/7622974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!