Roman Rehak

SQL Server and things not related

<October 2008>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678


Navigation

SQL Server Sites

Subscriptions

News

eXTReMe Tracker

Post Categories



Roman's Weekly SQL Server Tip - How to get table and column descriptions with T-SQL

Column and table descriptions are stored as extended properties, you can read more about extended properties in BOL. Microsoft uses a convention of naming them MS_Description, they are the ones that show in Enterprise Manager. You can declare your own extended properties and use them for data validation or other things.
 
You can create extended properties with the sp_addextendedproperty stored procedure and you can retrieve them by calling the fn_listextendedproperty() function. Here is how you get all column descriptions for the Orders table:
 
SELECT  * FROM  ::fn_listextendedproperty('MS_Description', 'user', 'dbo',
   'table', 'Orders', 'column', null)
 
More examples:
 
USE NORTHWIND
GO
 
-- add table comment
DECLARE @v sql_variant
SET @v = N'This table holds orders'
EXECUTE sp_addextendedproperty N'MS_Description', @v, N'user', N'dbo',
N'table', N'Orders', NULL, NULL
GO
 
-- add column comment
DECLARE @v sql_variant
SET @v = N'OrderID is auto-generated'
EXECUTE sp_addextendedproperty N'MS_Description', @v, N'user', N'dbo',
N'table', N'Orders', N'column', N'OrderID'
GO
 
-- get table description
SELECT  * FROM  ::fn_listextendedproperty('MS_Description', 'user', 'dbo',
   'table', 'Orders', null, null)
 
-- get OrderID column description
SELECT  * FROM  ::fn_listextendedproperty('MS_Description', 'user', 'dbo',
   'table', 'Orders', 'column', 'OrderID')
 
-- get descriptions for all columns in the table
SELECT  * FROM  ::fn_listextendedproperty('MS_Description', 'user', 'dbo',
   'table', 'Orders', 'column', null)
 
-- get all table properties
SELECT  * FROM  ::fn_listextendedproperty(null, 'user', 'dbo',
   'table', 'Orders', null, null)
 
-- get all column properties
SELECT  * FROM  ::fn_listextendedproperty(null, 'user', 'dbo',
   'table', 'Orders', 'column', null)

posted on Sunday, February 27, 2005 2:39 PM by Roman


# re: Roman's Weekly SQL Server Tip - How to get table and column descriptions with T-SQL @ Tuesday, August 07, 2007 4:51 AM

thanks for this help

adam

# re: Roman's Weekly SQL Server Tip - How to get table and column descriptions with T-SQL @ Friday, October 05, 2007 10:47 AM

This is what I am looking for!! Great!

G




Powered by Dot Net Junkies, by Telligent Systems