12,066,448 members (56,096 online)
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.
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
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.
||Removes an extended property from a database object|
||Updates the value of an existing extended property|
||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
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 '
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'
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):
|fileGroup.Logical file Name||Schema.Table.Column||user.Procedure.Parameter|
|partition Function||Schema.Table.Index||user.Queue.Event Notification|
|Remote Service Binding||Schema.Type||user.Service|
|schema.function||Schema.XML Schema Collection||User.Table.Index|
|schema.Queue.Event Notification||user.function||User.XML Schema Collection|
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' -- 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 (email@example.com). The best five suggestions for ways to help make a database inherently self-documenting will receive a prize!