Explain Codes LogoExplain Codes Logo

Adding a column description

sql
best-practices
database-management
sql-scripts
Anton ShumikhinbyAnton Shumikhin·Nov 8, 2024
TLDR

To add a column description in SQL Server, the stored procedure sp_addextendedproperty is your best friend:

EXEC sp_addextendedproperty 'MS_Description', 'Description text', 'Schema', 'dbo', 'Table', 'YourTable', 'Column', 'YourColumn';

Just replace 'YourTable', 'YourColumn', and 'Description text' to fit your table name, column name, and the description you want to set. This will assign the description directly in the database.

Method breakdown

Permissions check

First, make sure your account has the necessary privileges to modify the schema. Essentially, don't attempt this with a guest account.

Automate the addition

For efficiency, script an automatic loop through columns or formulate dynamic SQL to add descriptions massively. This can be a real timesaver—like finding money in your old jeans.

Syntax is key

Don't forget, precision in script syntax is non-negotiable. One wrong comma could be like stepping onto banana peel—slippery!

Diving deeper

Forming good habits

As a best practice, ensure that column descriptions are an integral part of your table creation scripts. Imagine them as a blueprint for any new columns.

If scripting isn't feasible

If you're allergic to scripts, SSMS’s GUI is an alternative cure. Right-click the table, choose "Design" and voilà—descriptions added! Remember, this isn't as tasty for scaling.

Result validation

Post script-execution, make sure to use the sys.extended_properties query to verify your descriptions. It's like serving a food safety test to your database dish!