Sunday, April 18, 2021

Database documentation within a database

This post demonstrates how to add descriptive comments to SQL Server database objects using SQL scripts. These comments help document your database structure and provide context to developers and analysts.

How to Comment on a Table in SQL Server

-- Add a description to a table
EXEC sys.sp_addextendedproperty   
 @name = N'MS_Description', 
 @value = N'Contains customer-related data.',   
 @level0type = N'SCHEMA',    
 @level0name = 'dbo',  
 @level1type = N'TABLE',     
 @level1name = 'tblCustomer';  
GO

Now let's see how to add a comment to a specific column within a table.

-- Add a description to a column
EXEC sp_addextendedproperty   
 @name  = N'MS_Description',
 @value = 'Primary key identifier for the customer.',  
 @level0type = N'Schema',  
 @level0name = 'dbo',  
 @level1type = N'Table',   
 @level1name = 'tblCustomer',   
 @level2type = N'Column',  
 @level2name = 'CustomerID';  
GO

For more in-depth guidance on querying all existing comments across SQL Server objects, visit the reference below:

Find All Comments in SQL Server – Himanshu Patel

Popular Posts