My company works with data from a number of customers and has neglected to document what the tables and fields of our databases represent. To help resolve this, I wrote some stored procedures that only seem to work for the DB they live on. I would like to have one instance of the stored procedures on the server that can be used on all its databases but can't figure out how to accomplish that. Here are the procedures:
Procedure 1 - sp_GetTableDocumentation
Create Procedure sp_GetTableDocumentation(@TableName SYSNAME)
AS
SELECT
@TableName AS [Table Name]
,'' AS [Column Name]
,CONVERT(NVARCHAR(MAX), ISNULL(D.value, '')) AS [Description]
FROM sys.Tables AS T
OUTER APPLY (SELECT TOP 1 * FROM ::fn_listextendedproperty('Description', 'SCHEMA', 'dbo', 'TABLE', @TableName, NULL, NULL)) AS D
WHERE T.Name = @TableName
UNION ALL
SELECT
@TableName AS [Table Name]
,C.Name AS [Column Name]
,CONVERT(NVARCHAR(MAX), ISNULL(D.value, '')) AS [Description]
FROM sys.Tables AS T
INNER JOIN sys.Columns AS C ON T.Object_id = C.Object_id
OUTER APPLY (SELECT TOP 1 * FROM ::fn_listextendedproperty('Description', 'SCHEMA', 'dbo', 'TABLE', @TableName, 'COLUMN', C.Name)) AS D
WHERE T.Name = @TableName
GO
Procedure 2 - sp_SetTableDocumentation
Create Procedure sp_SetTableDescription(
@schemaName sysname
, @tableName sysname
, @description sql_variant
)
As
If Exists (
Select 1
From fn_listextendedproperty('Description','SCHEMA',@schemaName,'TABLE',@tableName,NULL,NULL)
)
exec sp_DropExtendedProperty 'Description','SCHEMA',@schemaName,'TABLE',@tableName
If (Not @description Is Null) And (Not @description = '')
exec sp_AddExtendedProperty 'Description', @description,'SCHEMA',@schemaName,'TABLE',@tableName
GO
Procedure 3 - sp_SetTableDescription
Create Procedure sp_SetTableDescription(
@schemaName sysname
, @tableName sysname
, @description sql_variant
)
As
If Exists (
Select 1
From fn_listextendedproperty('Description','SCHEMA',@schemaName,'TABLE',@tableName,NULL,NULL)
)
exec sp_DropExtendedProperty 'Description','SCHEMA',@schemaName,'TABLE',@tableName
If (Not @description Is Null) And (Not @description = '')
exec sp_AddExtendedProperty 'Description', @description,'SCHEMA',@schemaName,'TABLE',@tableName
GO
Procedure 4 - sp_SetColumnDescription
CREATE PROCEDURE sp_SetColumnDescription (
@schemaName SYSNAME
,@tableName SYSNAME
,@columnName SYSNAME
,@description SQL_VARIANT
)
AS
IF EXISTS (
SELECT 1
FROM fn_listextendedproperty('Description', 'SCHEMA', @schemaName, 'TABLE', @tableName, 'COLUMN', @columnName)
)
EXEC sp_DropExtendedProperty 'Description','SCHEMA',@schemaName,'TABLE',@tableName,'COLUMN',@columnName
IF (NOT @description IS NULL) AND (NOT @description = '')
EXEC sp_AddExtendedProperty 'Description',@description,'SCHEMA',@schemaName,'TABLE',@tableName,'COLUMN',@columnName
GO
Thanks