views:

3106

answers:

5

I am trying to find a way to extract information about my tables in SQL Server (2008).
The data I need needs to include the description of the table (filled from the Description property in the Properties Window), a list of fields of that table and their respective data types.

Is there any way I can extract such meta-data? I presume I have to use some sys sp but I'n not sure which one.

+2  A: 

Generic information about tables and columns can be found in these tables:

select * from INFORMATION_SCHEMA.TABLES
select * from INFORMATION_SCHEMA.COLUMNS

The table description is an extended property, you can query them from sys.extended_properties:

select 
    TableName = tbl.table_schema + '.' + tbl.table_name, 
    TableDescription = prop.value,
    ColumnName = col.column_name, 
    ColumnDataType = col.data_type
FROM information_schema.tables tbl
INNER JOIN information_schema.columns col 
    ON col.table_name = tbl.table_name
LEFT JOIN sys.extended_properties prop 
    ON prop.major_id = object_id(tbl.table_schema + '.' + tbl.table_name) 
    AND prop.minor_id = 0
    AND prop.name = 'MS_Description' 
WHERE tbl.table_type = 'base table'
Andomar
You may have to be slightly careful how you call OBJECT_ID() to disambiguate with different owners
Marc Gravell
MS_Description_Table? Also - the table metadata and column metadata is stored separately; you need to handle the minor_id appropriately
Marc Gravell
Good points all edited now! Except minor_id which appears not to work per the comment on your answer.
Andomar
Just like as I said to Marc, when you include 'prop.minor_id IS NULL', the description is not retrieved, but when you remove it, it works
Andreas Grech
It should have been minor_id = 0; otherwise you'll find column descriptions against the table
Marc Gravell
yes, that's exactly what was happening; column descriptions where interfering the the table descriptions
Andreas Grech
Aha, so the object_id() for a column and its table are equal. Editing the answer again :-)
Andomar
A: 
select   TABLE_CATALOG,
    TABLE_SCHEMA, 
    TABLE_NAME,
    COLUMN_NAME,
    DATA_TYPE,
    CHARACTER_MAXIMUM_LENGTH
from 
information_schema.columns
Mitch Wheat
+4  A: 

To get the description data, you unfortunately have to use sysobjects/syscolumns to get the ids:

SELECT   u.name + '.' + t.name AS [table],
            td.value AS [table_desc],
      c.name AS [column],
      cd.value AS [column_desc]
FROM     sysobjects t
INNER JOIN  sysusers u
    ON  u.uid = t.uid
LEFT OUTER JOIN sys.extended_properties td
    ON  td.major_id = t.id
    AND  td.minor_id = 0
    AND  td.name = 'MS_Description'
INNER JOIN  syscolumns c
    ON  c.id = t.id
LEFT OUTER JOIN sys.extended_properties cd
    ON  cd.major_id = c.id
    AND  cd.minor_id = c.colid
    AND  cd.name = 'MS_Description'
WHERE t.type = 'u'
ORDER BY    t.name, c.colorder

You can do it with info-schema, but you'd have to concatenate etc to call OBJECT_ID() - so what would be the point?

Marc Gravell
Info-schema seemed easier to retrieve the data type. I think you could retrieve it here with a join to systype on c.xtype.
Andomar
I agree with you there; info-schema makes this very easy... there is probably some ideal merge between the two.
Marc Gravell
Marc, why did you include 'td.minor_id IS NULL' ? With this addition it's not retrieving the description, but if if I remove it, it works.
Andreas Grech
@Dreas: I'll double check what it should be...
Marc Gravell
The table description is working now, with the = 0
Andreas Grech
A: 

You could try sp_help <Name of object>

Brandon Montgomery
A: 

Using Object Catalog Views:

SELECT  T.NAME AS [TABLE NAME], C.NAME AS [COLUMN NAME], P.NAME AS [DATA TYPE], P.MAX_LENGTH AS[SIZE],   CAST(P.PRECISION AS VARCHAR) +‘/’+ CAST(P.SCALE AS VARCHAR) AS [PRECISION/SCALE]
FROM ADVENTUREWORKS.SYS.OBJECTS AS T
JOIN ADVENTUREWORKS.SYS.COLUMNS AS C
ON T.OBJECT_ID=C.OBJECT_ID
JOIN ADVENTUREWORKS.SYS.TYPES AS P
ON C.SYSTEM_TYPE_ID=P.SYSTEM_TYPE_ID
WHERE T.TYPE_DESC=‘USER_TABLE’;

Using Information Schema Views

SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION,
       COLUMN_DEFAULT, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,
       NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, NUMERIC_SCALE,
       DATETIME_PRECISION
FROM ADVENTUREWORKS.INFORMATION_SCHEMA.COLUMNS
MarlonRibunal