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

取得MSSQL表中字段及主键等属性SQL语法

时间:2014-12-25 20:23:44      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

SELECT  c.NAME AS [Column Name], t.NAME AS [Data type],
        c.max_length AS [Max Length], c.precision, c.scale,
        c.is_nullable AS [Is Nullable], c.is_identity AS [Is Identity],
        ISNULL(i.is_primary_key, 0) [Primary Key], i.type_desc,
        OBJECT_SCHEMA_NAME(fk.object_id) + . + OBJECT_NAME(fk.object_id)
        +  ( + fk.Name + ) AS [Foreign Key]
FROM    sys.columns c
        INNER JOIN sys.types t ON c.system_type_id = t.system_type_id
                                  AND c.user_type_id = t.user_type_id
        LEFT OUTER JOIN ( sys.index_columns ic
                          INNER JOIN sys.indexes i ON ic.object_id = i.object_id
                                                      AND i.is_primary_key = 1
                                                      AND ic.index_id = i.index_id
                        ) ON ic.object_id = c.object_id
                             AND ic.column_id = c.column_id
        LEFT JOIN sys.foreign_key_columns fkc ON fkc.parent_object_id = c.object_id
                                                 AND fkc.parent_column_id = c.column_id
        LEFT JOIN sys.columns fk ON fk.object_id = fkc.referenced_object_id
                                    AND fk.column_id = fkc.referenced_column_id
WHERE   c.object_id = OBJECT_ID(Table Name)
ORDER BY c.Column_ID ;

 

取得MSSQL表中字段及主键等属性SQL语法

标签:

原文地址:http://www.cnblogs.com/ywkpl/p/4185279.html

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