WEB开发网
开发学院数据库MSSQL Server SQL Doc获取表结构信息的SQL 阅读

SQL Doc获取表结构信息的SQL

 2009-08-29 00:00:00 来源:WEB开发网   
核心提示: 最后一行Where so.NAME='schools',是只获取表名为Schools的信息,把这个Where条件去掉,SQL Doc获取表结构信息的SQL(2),则获取当前数据库的所有对象的架构信息,相比SubSonic 2.2中获取数据库对象详细信息,SQL Doc要详细和准确

最后一行Where so.NAME='schools',是只获取表名为Schools的信息,把这个Where条件去掉,则获取当前数据库的所有对象的架构信息。

相比SubSonic 2.2中获取数据库对象详细信息,SQL Doc要详细和准确一些,经我的测试,对于字段描述信息,如果删除某个表的字段,SubSonic的方式获取字段描述信息有可能会出现错位的情况,下面是SubSonic中的获取数据库表详细信息的SQL:

SubSonic Code

SELECT
    TABLE_CATALOG AS [Database],
    TABLE_SCHEMA AS Owner,
    TABLE_NAME AS TableName,
    COLUMN_NAME AS ColumnName,
    ORDINAL_POSITION AS OrdinalPosition,
    COLUMN_DEFAULT AS DefaultSetting,
    IS_NULLABLE AS IsNullable, DATA_TYPE AS DataType,
    CHARACTER_MAXIMUM_LENGTH AS MaxLength,
    DATETIME_PRECISION AS DatePrecision,
    COLUMNPROPERTY(object_id('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), COLUMN_NAME, 'IsIdentity') AS IsIdentity,
    COLUMNPROPERTY(object_id('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), COLUMN_NAME, 'IsComputed') as IsComputed,
    properties.value   as    ColumnDescription 
FROM  INFORMATION_SCHEMA.COLUMNS
left   join    sys.extended_properties    properties  
  ON   object_Id(columns.Table_Name)   =    properties.major_id   --AND columns.COLLATION_SCHEMA
  AND    columns.Ordinal_position   =    properties.minor_id AND class_desc='OBJECT_OR_COLUMN'

WHERE TABLE_NAME='schools'
ORDER BY OrdinalPosition ASC

上一页  1 2 

Tags:SQL Doc 获取

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接