Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select column comments in SQL Server?

Tags:

sql-server

I need to select the column comments of a table. What table has this information?

like image 271
Rodrigo Avatar asked May 26 '26 13:05

Rodrigo


2 Answers

There is no SQL standard compliant support for comments in SQL Server.

The only thing that comes close to the usual comment on ... syntax is using an extended property to store the comments.

To list the values of an extended property, you can use the following statement:

SELECT objname, cast(value as varchar) as value 
FROM fn_listextendedproperty ('MS_DESCRIPTION','schema', 'dbo', 'table', 'your_table', 'column', null);

This assumes you are using property named MS_DESCRIPTION to store the comments.

To set a comment using an extended property you have to use the sp_addextendedproperty() function.

SELECT
   *
FROM
   sys.extended_properties
WHERE
   major_id = OBJECT_ID('mytable')
   AND
   minor_id = COLUMNPROPERTY(major_id, 'MyColumn', 'ColumnId')
like image 22
gbn Avatar answered May 28 '26 16:05

gbn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!