Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a Query to access column description in SQL

Tags:

sql

sql-server

I am trying to access the Column description properties using the INFORMATION_SCHEMA

I have created this Query in the past to get the column name but i can not figure out how to get description of the column

SELECT COLUMN_NAME AS Output, ORDINAL_POSITION 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE  (TABLE_NAME = @Tablename) AND (ORDINAL_POSITION = @Location)

 Screen shot

This is where the Description is on the field properties

like image 759
Will Peckham Avatar asked Mar 01 '13 15:03

Will Peckham


People also ask

How do I view a SQL Description?

SQL DESCRIBE TABLE is a SQL statement that is accountable for telling something about a specific table in the database. If we want to show the structure of a database table or tables in the server then, we will use the SQL command DESCRIBE or other keyword DESC, which is identical to DESCRIBE one.

What is DESC query in SQL?

The DESC command is used to sort the data returned in descending order.


2 Answers

If by 'description' you mean 'Description' displayed in SQL Management Studio in design mode, here it is:

    select 
        st.name [Table],
        sc.name [Column],
        sep.value [Description]
    from sys.tables st
    inner join sys.columns sc on st.object_id = sc.object_id
    left join sys.extended_properties sep on st.object_id = sep.major_id
                                         and sc.column_id = sep.minor_id
                                         and sep.name = 'MS_Description'
    where st.name = @TableName
    and sc.name = @ColumnName
like image 89
Incidently Avatar answered Oct 16 '22 05:10

Incidently


If you specifically want to use INFORMATION_SCHEMA (as I was) then the following query should help you obtain the column's description field:

SELECT COLUMN_NAME AS [Output]
    ,ORDINAL_POSITION
    ,prop.value AS [COLUMN_DESCRIPTION]
FROM INFORMATION_SCHEMA.TABLES AS tbl
INNER JOIN INFORMATION_SCHEMA.COLUMNS AS col ON col.TABLE_NAME = tbl.TABLE_NAME
INNER JOIN sys.columns AS sc ON sc.object_id = object_id(tbl.table_schema + '.' + tbl.table_name)
    AND sc.NAME = col.COLUMN_NAME
LEFT JOIN sys.extended_properties prop ON prop.major_id = sc.object_id
    AND prop.minor_id = sc.column_id
    AND prop.NAME = 'MS_Description'
WHERE tbl.TABLE_NAME = @TableName
like image 16
Kez Floyd Avatar answered Oct 16 '22 06:10

Kez Floyd