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 ;

 

相关文章:

  • 2021-05-20
  • 2021-10-08
  • 2021-12-02
  • 2021-10-16
  • 2021-12-12
猜你喜欢
  • 2021-06-11
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2021-08-19
  • 2021-07-06
相关资源
相似解决方案