From having descriptions added to tables and columns, but finding them using SQL Server: Searching Table Description proves to be unsuccessful. Debugging reveals that there are two spaces instead of one between two words, causing the search to fail. This leads to the need for a query to clean descriptions in both tables and columns.
Cleaning Descriptions in SQL Server
The script below is designed to clean descriptions in SQL Server by removing extra spaces, line breaks, and other unwanted characters.
SQL
SET NOCOUNT ON;
;WITH DirtyDescriptions AS (
SELECT
DB_NAME() AS DBName,
SCHEMA_NAME(o.schema_id) AS SchemaName,
OBJECT_NAME(o.object_id) AS TableName,
c.name AS ColumnName,
CAST(ep.value AS nvarchar(max)) AS OldDesc,
LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(CAST(ep.value AS nvarchar(max)), CHAR(13), ' '), CHAR(10), ' '), ' ', ' '))) AS CleanDesc
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON ep.major_id = o.object_id
LEFT JOIN sys.columns c ON ep.minor_id = c.column_id AND ep.major_id = c.object_id
WHERE ep.name = N'MS_Description'
AND ep.class = 1
AND ep.value IS NOT NULL
AND (
CAST(ep.value AS nvarchar(max)) LIKE '% %'
OR CAST(ep.value AS nvarchar(max)) LIKE '%' + CHAR(13) + '%'
OR CAST(ep.value AS nvarchar(max)) LIKE '%' + CHAR(10) + '%'
OR LTRIM(RTRIM(CAST(ep.value AS nvarchar(max)))) <> CAST(ep.value AS nvarchar(max))
)
)
SELECT
ROW_NUMBER() OVER (ORDER BY SchemaName, TableName, ColumnName) AS [No],
DBName + '.' + SchemaName + '.' + TableName AS [Full_Table_Name],
ISNULL(ColumnName, '(Table Level)') AS [Column_Name],
OldDesc AS [Old_Description],
CleanDesc AS [New_Description],
-- Update description to be clean
N'USE ' + QUOTENAME(DBName) + N'; EXEC sp_updateextendedproperty ' +
N'@name = N''MS_Description'', @value = N'''' + REPLACE(CleanDesc, '''', '''''') + N''''', ' +
N'@level0type = N''SCHEMA'', @level0name = N'''' + SchemaName + N''''', ' +
N'@level1type = N''TABLE'', @level1name = N'''' + TableName + N'''''' +
CASE WHEN ColumnName IS NOT NULL
THEN N', @level2type = N''COLUMN'', @level2name = N'''' + ColumnName + N'''''
ELSE N''
END + N';' AS [SQL_Command_To_Run]
FROM DirtyDescription
Conclusion
This script helps in maintaining clean and accurate descriptions for tables and columns in SQL Server, ensuring that searches for descriptions are more effective and efficient.