Av rating:
Total votes: 43
Total comments: 7


William Brewer
Towards the Self-Documenting SQL Server Database
01 December 2006

Extended Properties in SQL Server

With SQL Server, it is strange how some of the most radical improvements that have been introduced over the years have been slipped in almost unnoticed. Conversely, the features that were plastered all over the marketing brochures are sometimes the ones that turn out to be dead ends.

A feature that fits into the former category is the extended properties of databases. Introduced quietly with SQL Server 2000, one could have easily missed them but they have proved, after all, to be of great value to the developer. Extended properties are the route to creating self-documenting databases. I use the term 'self-documenting' in the sense that one can attach the documentation directly to the database itself, rather than create a separate document. Basically, you use the extended properties of data objects to apply your own properties to the metadata.

Using extended properties

The classic example is to use extended properties of an object to append a description of that object, be it a trigger, stored procedure, function, table, view, parameter, index, constraint or column. One can also use extended properties to document details such as the date of revision, author, input mask, caption, history, rendering-style, and so on.

One of the classic general tricks that programmers have used in the past to add documentation to source code is to structure the comments of source code by inserting predefined markers to indicate the category of information (revision date, for example) to any application that generates the documentation (such as Javadocs). This can't be done in SQL Server as source is only maintained in the database for certain objects such as procedures and functions. Since the introduction of extended properties, such tricks would be unnecessary anyway.

The advantage of using the extended properties is that the documentation, notes, and so on stay with the databases and can be used as a "live documentation" mechanism. They are backed up with the database, and scripted out with the build scripts.

Despite their obvious utility, Microsoft has treated the feature with a curious lack of enthusiasm. There is the smell of 'wet paint' about the design. Extended properties allow you to document your database objects but it has been left to third-party utilities such as SQL Doc and DBDesc to exploit the use of these properties for generating the full documentation of the database from the database itself.

A consequence of Microsoft's indifference to extended properties is that they forgot to include them in the replication synchronisation process. You have to do it manually (a tool such as SQL Compare will synchronise them properly). Also, they neglected to provide an Information_Schema view of the extended properties, which would have made to make it easier to access them from SQL.

Another difficulty is that some third-party software vendors have used the extended properties for other purposes, such as storing parameters for entity-relationship diagrams. This makes it difficult for utilities that extract the documentation as there is no standard property name other than MS_Description.

Creating extended properties via code

Microsoft provides one extended property, MS_Description, which can be used from both Enterprise Manager and SSMS to provide a description of the object to which it is bound. Further, the Diagram Designer provides a description field, accessible via the custom view, which provides an easy way of viewing and editing the documentation of the columns.

However, extended properties are just about providing basic descriptions of objects. They are a lot more versatile that that. The designers of extended properties sensibly placed no restrictions on the properties that one could attribute to database objects. It is perfectly OK, for example, to provide extra metadata to assist the application layer in rendering or querying the data.

When writing the documentation for objects, it is generally quickest to use the facilities within Microsoft's own tools to add basic descriptions, but beyond that there eventually comes a time that one has to use stored procedures to add documentation.

At the basic level, in SQL Server 2000, all extended properties are stored in sysproperties, but are accessed by a number of stored procedures.

sp_addextendedproperty

Adds a new extended property to a database object

sp_dropextendedproperty

Removes an extended property from a database object

sp_updateextendedproperty

Updates the value of an existing extended property

fn_listextendedproperty

Retrieves the value of an extended property or the list of all extended properties from a database object

These stored procedures are clumsy to use and hardly encourage the programmer into using extended properties. However, a few examples (for SQL Server 2000) might make their use a bit clearer:

/*we add the extended property to provide a description to the
dbo.Customer.InsertionDate column   */

sp_addExtendedProperty 'MS_Description',
'the date at which the row was created'
'user''dbo''table''Customer',
'column''InsertionDate'

-- alternative syntax for SQL 2005
sp_addExtendedProperty 'MS_Description',
'the date at which the row was created',
'schema','sales', 'table', 'Customer',
'column', 'ModifiedDate'


/* and then update the description of the
dbo.Customer.InsertionDate column  */

sp_UpdateExtendedProperty 'MS_Description',
'the full date at which the row was created'
'user''dbo''table''Customer',
'column''InsertionDate'

/* we can list this column */
SELECT FROM ::fn_listExtendedProperty
       'MS_Description','user''dbo''table',
'Customer''column''InsertionDate')

/* or all the properties for the table column of dbo.Customer*/
SELECT FROM ::fn_listExtendedProperty
       (DEFAULT,'user''dbo''table''Customer''column',DEFAULT)

/* And now we drop the MS_Description property of 
dbo.Customer.InsertionDate column */

sp_dropExtendedProperty 'MS_Description',  
'user''dbo''table''Customer''column''InsertionDate'

A database-documenting stored procedure

I find the stored procedures described in the previous section unintuitive and don't exactly tempt the programmer into adding documentation. I'd prefer something that described the object and its hierarchy in a more conventional way. For example if one wanted to alter the description of a surname column in a Customer table then it should be 'dbo.Customer.Surname.MS_Description' using the 'user.table.column' hierarchy.

In order to make things easier, I created a simple 'helper' stored procedure which simplifies the access to Microsoft's system stored procedures, but doesn't try to replace them. If you provide the description of the object and the hierarchy, then it displays what is there. If you provide a value, it either assigns it or, if you want, appends it to the end of the current value.

The stored procedure autosenses which version of SQL Server it is on, and loads with the valid object hierarchies for the operating system. It checks the hierarchy you give it to see if it is valid. This list is rather handy, so the stored procedure also includes a feature that provides the hierarchy as a table.

The full source code for this spDBDoc stored procedure is provided in the source code for this article (simply click the "Code Download" link in the box to the right of the article title).

Create the stored procedure and let's try it out. First, let's see what hierarchies can have extended properties:

EXEC spDbDoc '','','','possible'

or, alternatively:

spDbDoc @Function='possible'

You will see that there are rather a lot – and that the possible objects that can have attributes attached to them has been greatly expanded in SQL 2005 (those picked out in bold are available in both 2000 and 2005):

assembly

Schema.Service

user.function.Constraint

contract

Schema.Synonym

user.function.Parameter

Event Notification

Schema.Table

user.Procedure

fileGroup.Logical file Name

Schema.Table.Column

user.Procedure.Parameter

Message type

Schema.Table.Constraint

user.Queue

partition Function

Schema.Table.Index

user.Queue.Event Notification

partition Scheme

Schema.Table.Trigger

User.Rule

Remote Service Binding

Schema.Type

user.Service

route

Schema.View

user.Synonym

Schema

Schema.View.Trigger

User.Table

schema.aggregate

Schema.View.column

User.Table.Column

schema.Default

Schema.View.index

User.Table.Constraint

schema.function

Schema.XML Schema Collection

User.Table.Index

schema.function.column

Service

User.Table.Trigger

schema.function.Constraint

trigger

user.Type

schema.function.Parameter

type

User.View

schema.Procedure

user

User.View.column

schema.Procedure.Parameter

user.aggregate

User.View.index

schema.Queue

user.Default

User.View.Trigger

schema.Queue.Event Notification

user.function

User.XML Schema Collection

schema.Rule

user.function.column

Let's start very simply. Let's just create a description for the entire database:

EXEC spdbDoc  '','','This is a sample database that illustrates how
extended properties can be assigned to objects'

Nothing exciting here so let's add a 'revision date' property:

EXEC spdbDoc  '','revisionDate','20 Nov 2006: Built the first iteration'

Now we want to add the new version, rather than replace the existing value:

EXEC spdbDoc  '','revisionDate','21 Nov 2006: Fixed warning message
in  build script'
,'append'

We can list all extended properties and values for columns of a given table, for example the customer table:

EXEC spdbDoc 'user.table.column','dbo.customer'   -- SQL 2000
EXEC spdbDoc 'schema.table.column','sales.customer' -- alternative in SQL 2005

Assigning a standard MS_Documentation property and value is easy:

/* either alter or create an entry for the MS_Documentation property
for insertionDate  */
spdbDoc  'user.table.column','dbo.customer.insertionDate',
'This logs the date that the row was inserted'

Or you can add your own property. By way of example, we assign a suggested convert style for a date field:

/* either alter or create an entry for the ConvertStyle property
for insertionDate */ 
EXEC spdbDoc  'user.table.column',
'dbo.customer.insertionDate.ConvertStyle',113 

If you take a look at the source code download you'll find several more examples for this procedure included as comments.

Of course, ultimately, extended properties are just one of the various means of ensuring that your databases are well-documented and easily understood, such as using long descriptive object names. We welcome your contributions – leave your comments below, or drop a mail to the editor (editor@simple-talk.com). The best five suggestions for ways to help make a database inherently self-documenting will receive a prize!



This article has been viewed 10615 times.
William Brewer

Author profile: William Brewer

William Brewer is a SQL Server developer who has worked as a Database consultant and Business Analyst for several Financial Services organisations in the City of London. True to his name, he is also an expert on real ale.

Search for other articles by William Brewer

Rate this article:   Avg rating: from a total of 43 votes.


Poor

OK

Good

Great

Must read
 
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Sql Documentation and Extended Properties
Posted by: Anonymous (not signed in)
Posted on: Wednesday, December 13, 2006 at 7:15 PM
Message: If you looking for a tool to easy document your database look at www.CaberComputing.com See the product call "FullMetalJacket".

Subject: FullMetalJacket
Posted by: WBrewer (view profile)
Posted on: Thursday, December 14, 2006 at 7:06 AM
Message:

The product is interesting. It is a Beta version, which has no installer and has ..er.. quirks, but is certainly an interesting approach to the problem of documenting databases, and generating C# code. This is just the sort of product that would be worth reviewing on Simple-Talk, if only for its original and creative approach.

In fairness, I'd ask you also to look at DBDESC on http://www.dbdesc.com/
SQL Doc on http://www.red-gate.com/products/SQL_Doc/index.htm
Apex SQL Doc on http://www.apexsql.com/sql_tools_doc.asp
BI Documentor on http://www.bidocumenter.com/Public/Features.aspx
SQLtoDoc on http://www.schematodoc.com/products_std.htm

ps (9/5/2007) FullMetalJacket has a new name; SqlTac.  Additionally, it has its own website.  See www.SqlTac.com


Subject: 2005 Changes?
Posted by: Anonymous (not signed in)
Posted on: Thursday, December 14, 2006 at 7:30 AM
Message: How has this feature and it usage changed in SQL 2005?

Subject: re: 2005 Changes?
Posted by: WBrewer (view profile)
Posted on: Thursday, December 14, 2006 at 5:12 PM
Message: You'll see from the article that there is a lot more in SQL Server 2005 that you can attach documentation to. The changes are listed in the table. The stored procedure spdbDoc actually senses which version you are using and lists the possible.
In SQL Server 2005 you also have the view sys.Extended_Properties (described in http://msdn2.microsoft.com/en-us/library/ms177541.aspx ) which is very useful. I should, perhaps, have mentioned this in the article

Subject: Documentation Report
Posted by: daveib (view profile)
Posted on: Friday, January 05, 2007 at 5:08 PM
Message: I used this sproc (modified to include the drop function) and turned it into a reporting Services Report. I used the 'possible' command to fill a drop down menu and created a static drop down menu for the function. It works great and makes documenting a snap. Thanks for the head start. Anyone interested in it can ask me for it and I will send you the rdl file along with my spDBDoc.sql
david.briggs@unityworksmedia.com
dba

Subject: re: FullMetalJacket
Posted by: Anonymous (not signed in)
Posted on: Monday, February 05, 2007 at 5:37 PM
Message: SqlSpec is another documentation tool I rather like. It does several other RDBMS besides SQL Server.

It's available here:

http://www.elsasoft.org

Subject: RE: Extended Properties
Posted by: Anonymous (not signed in)
Posted on: Tuesday, February 05, 2008 at 9:58 AM
Message: Extended properties are great! It's a shame more people don't use them though, but I suppose it's not that convenient to add them through the SQL Management Studio.

Incidentally, my own SQL database documentor, the SQL Documentation Tool extracts MS_Description extended properties...

http://www.winnershtriangle.com/w/Products.SQLDocumentationTool.asp

 









Phil Factor
To Boldly Ask IT for Development Work
 Phil has always been mystified by the way that, in Science-Fiction films, the crew of space-ships are able to... Read more...



 View the blog
Using the Filtering API with the SQL Comparison SDK
 Red Gate's SQL Comparison SDK provides a means to compare and synchronize database schemas and data... Read more...

SQL Toolbelt 2008: Predominantly an Engineering Task
 The conversion of the Red-Gate tools to be compatible with SQL Server 2008 might not seem, on first... Read more...

Audit Crosschecks
 In this short article, the second of a 2-part series, William suggests a solution, using SQL Data... Read more...

SQL Response: The dim sum interview
 Richard Morris met David and Nigel of the SQL Response team, in a dim sum Restaurant in Cambridge. They... Read more...

SQL Server 2008: Performance Data Collector
 With Performance Data Collector in SQL Server 2008, you can now store performance data from a number of... Read more...

Beginning SQL Server 2005 Reporting Services Part 1
 Steve Joubert begins an in-depth tour of SQL Server 2005 Reporting Services with a step-by-step guide... Read more...

Ten Common Database Design Mistakes
 Database design and implementation is the cornerstone of any data centric project (read 99.9% of... Read more...

Beginning SQL Server 2005 Reporting Services Part 2
 Continuing his in-depth tour of SQL Server 2005 Reporting Services, Steve Joubert demonstrates the most... Read more...

SQL Server Full Text Search Language Features
 SQL Full-text Search (SQL FTS) is an optional component of SQL Server 7 and later, which allows fast... Read more...

Executing SSIS Packages
 Nigel Rivett demonstrates how to execute all SSIS packages in a given folder using either an SSIS... Read more...

Over 150,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.

Join Simple Talk