Welcome to Cactus Juice Sign in | Join | Help

Cameron Scholtz's Blog

Enthusiasm Always Wins
SQL Server Description Property

Ever notice that SQL Server Query Analyzer color codes the word Description? Ever wonder why? SQL Server uses this field just as the name implies; to store a description of a column or table. It stores these values in the sysproperties table.

Check it out. Right click a table in Enterprise Manager and choose Design. Notice each column has a Description property that you can fill out. Futhermore, click on the Properties button and you'll see a place to enter a description for the table itself.[fig. 1]

Figure 1
Adding a table Description via Enterprise Manager

If you've ever poked around with custom Table Views in EM's Diagrams node, you've noticed the Description column can be added here as well. To do so, right click a table in your diagram and choose Table View | Modify Custom. Scroll to the bottom and add the Description field to the Selected Column. Choose OK. Right click again and choose Table View | Custom. You should now see a view of the table that includes the Description column.[fig. 2]

Figure 2
SQL Server diagram with a custom table view showing the Description column.

This is the same field, just another handy way of looking at it. Very handy is more like it. Take a few minutes to fill out your Description fields, drop the tables into a diagram and any future developer will have instant basic documentation.

OK so let's assume you've added some descriptions. Now you know how to configure and print a custom diagram. But what if you'd like to combine the Description column with some other information about your tables. T-SQL is the answer. I'll show a brief example here but it should be enough to get you going. Once you know how to SELECT the Description column you can JOIN in any other other existing T-SQL you might have. Here's how the T-SQL looks and below that is the example output.[fig. 3]

In the first code block I've used SQL Server's built-in function FN_LISTEXTENDEDPROPERTY. This function retrieves extended property values from database objects. The function has seven arguments. In our case here we're only concerned with the first (name) and fifth (level 1 name) values. These two values specify we want the MS_DESCRIPTION column -- which holds descriptions -- duh :) -- and the table named "Horses".

-- Simple example retrieves column descriptions only.
SELECT Value
FROM :: FN_LISTEXTENDEDPROPERTY('MS_DESCRIPTION', 'User', 'dbo',
'table'
, 'Horses', 'column', DEFAULT)


The second code block demonstrates how to get the Description by directly querying the sysproperties table. Note: some organizations have rules or permissions relating to using SQL system tables so this option may or may not be available to you.

 

-- A less preferred way is to query the system table. This example gets the description of column 1.
SELECT value FROM sysproperties
WHERE id
= object_id('Horses') AND smallid = 1


Getting a result set of Description values is all well and good but it's not too useful by itself. Therefore, the third code block is an example of how you might combine the Description column with other system table data to get a more useful result set.

 

-- Queries table and column description fields.
SET NOCOUNT ON
GO
SELECT SO.Name AS TableName, SC.Name AS ColumnName,
ST.Name AS DataType,
SC.Length AS Length, SC.Colid ColumnPosition,
SP1.Value AS TableDescription, SP2.Value AS ColDescription
FROM sysobjects SO (NOLOCK)
INNER JOIN syscolumns SC (NOLOCK) ON SC.id = SO.id
INNER JOIN systypes ST (NOLOCK) ON ST.xtype = SC.xtype
AND SC.XUserType = ST.XUserType
LEFT JOIN sysproperties SP1 (NOLOCK) ON SP1.id = SO.id
AND SP1.Type = 3
LEFT JOIN sysproperties SP2 (NOLOCK) ON SP2.id = SC.id
AND SP2.Type = 4
AND SP2.Smallid = SC.Colid
WHERE SO.Type = 'u' AND so.name = 'Horses'
ORDER BY SO.Name, SC.ColID


Example output of the three above T-SQL blocks.

 

Figure 3
SQL Server Description field with example Query Analyzer output

BTW, the Description field is also accessible in SQL Server 2005. Fire up SQL Server Management Console, right click a table and choose Modify. You'll see it in the Properties docker.

Posted: Tuesday, September 05, 2006 3:21 PM by Cameron

Comments

No Comments

Anonymous comments are disabled