views:

83

answers:

3

is it possible to retrieve by using connection.getschema() the description item from a sql server table column, just like it's possible to retrieve the column name, data type, is nullable, column default value, etc? if so, how?

+1  A: 

Try this:

    SELECT 
    [Table Name] = i_s.TABLE_NAME, 
    [Column Name] = i_s.COLUMN_NAME, 
    [Description] = s.value 
FROM 
    INFORMATION_SCHEMA.COLUMNS i_s 
LEFT OUTER JOIN 
    sys.extended_properties s 
ON 
    s.major_id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME) 
    AND s.minor_id = i_s.ORDINAL_POSITION 
    AND s.name = 'MS_Description' 
WHERE 
    OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0 
    --AND i_s.TABLE_NAME = 'table_name' 
ORDER BY 
    i_s.TABLE_NAME, i_s.ORDINAL_POSITION

edit: fixed the query :-)

HTH

Raja
On SQL Server 2005, I get `Msg 208, Level 16, State 1, Line 1 Invalid object name 'sysproperties'.`
KM
A: 

On Sql Server 2005, you can use this system table value function:

fn_listextendedproperty (Transact-SQL)

or try a query, from from this article, like this:

SELECT  
    [Table Name] = OBJECT_NAME(c.object_id), 
    [Column Name] = c.name, 
    [Description] = ex.value  
FROM  
    sys.columns c  
LEFT OUTER JOIN  
    sys.extended_properties ex  
ON  
    ex.major_id = c.object_id 
    AND ex.minor_id = c.column_id  
    AND ex.name = 'MS_Description'  
WHERE  
    OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0  
    -- AND OBJECT_NAME(c.object_id) = 'your_table' 
ORDER  
    BY OBJECT_NAME(c.object_id), c.column_id
KM
thanks, but I already have a datatable returned by connection.GetSchema() and I was trying to grab the description info from it. Since by looping through the rows of the datatable I can get the info of the column name, data type, etc. I suppose the description should be there too, but I cant find where
Pablo
A: 

If you already have the DataTable, as you mention - look at its data columns!

foreach(DataColumn col in dataTable.Columns)
{
  // check out all the properties on the DataColumn
}

Does that contain what you need to have??

marc_s
yes, I already have the loop made and Im grabbing other properties in the data column such as "IS_NULLABLE","COLUMN_NAME","DATA_TYPE", etc. , but I cant find how to grab the description of a column. I use the object inspector to see the properties/methods of the datacolumn but I cant find how to grab that particular info with the object inspector either.
Pablo
this seems to be the same problem I have http://www.bigresource.com/MS_SQL-GetSchema-not-returning-description-of-table-or-column-GSfX9tHx.html
Pablo