onsdag 14. september 2011

Commenting tables, column, procedures ++ in SQL Server

Unlike Oracle, PostgreSQL and others, you don't have specific table or column comments. Instead, SQL Server has so-called extended properties which are key-value pairs containing whatever information you want to put there. You can access these by using built-in procedures.

Example:
exec sp_addextendedproperty 'MS_Description', 'This column is awesome', 'schema', 'dbo', 'table', 'mytable', 'column', 'mycolumn';

This adds an extended property called MS_Description to the column dbo.table.mycolumn.

Note:
  • MS_Description is the standardized name for a short description. Using this helps when auto generating documentation from your database.
  • You can't add a property if it already exists. In that case, you must specifically update it. See examples for more info.
  • If you don't use schemas in your database, then dbo is your current schema.

Examples:

ActionCommand
Add description to a procedureexec sp_addextendedproperty 'MS_Description', 'My super cool procedure', 'schema', 'dbo', 'procedure', 'my_proc';
Update existing property version on a tableexec sp_updateextendedproperty 'version', '2.0', 'schema', 'dbo', 'table', 'my_table';
Delete existing property version from a tableexec sp_deleteextendedproperty 'version', 'schema', 'dbo', 'table', 'my_table';
List all properties of a columnselect * from fn_listextendedproperty(default, 'schema', 'dbo', 'table', 'my_table', 'column', 'my_column');
List the MS_Description property of a tableselect * from fn_listextendedproperty('MS_Description', 'schema', 'dbo', 'table', 'my_table', null, null);

Ingen kommentarer:

Legg inn en kommentar