Last active
April 22, 2021 17:32
-
-
Save GustavoAmerico/3760b3f0ec2ecb26bb77da634ff7406b to your computer and use it in GitHub Desktop.
Procedure for add or update SQL Server table and column description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE PROCEDURE dbo.PROC_AddOrUpdateTableAndColumnDescription | |
@fullName varchar(255), | |
@description varchar(255) | |
AS | |
/* | |
Table exemple: <schema>.<table name> dbo.MyTable | |
Column Exemple: <schema>.<table name>.<column name> dbo.MyTable.Id | |
*/ | |
/*Extrai o primeiro valor antes do ponto, representa o schema*/ | |
declare @schemaName varchar(255) = | |
(select top 1 trim(x.[value]) from string_split(@fullName, '.') x inner join sys.schemas s on s.[name]= x.[value]); | |
declare @msg varchar(255); | |
if(@schemaName is null or @schemaName ='') | |
begin | |
set @msg =concat('The schema ',@schemaName, ' was found in ',db_name()); | |
THROW 90000, @msg, 1; | |
end | |
/*Extrai o primeiro valor após o ponto, representa o nome da tabela*/ | |
declare @tableName varchar(255)=(select top 1 trim(x.[value]) as [value] | |
from string_split(@fullName, '.') x | |
inner join sys.tables t on t.[name] = x.[value] | |
where x.[value] <> @schemaName); | |
if(@tableName is null or @tableName ='') | |
begin | |
set @msg =concat('The table ',@tableName, ' was found in schema ', @schemaName, ' on database ',db_name()); | |
THROW 90001, @msg, 1; | |
end | |
/*Verifica se existe 3 valores seprados por ponto (schema, tabela e coluna)*/ | |
if (select count(*) from string_split(@fullName, '.')) =3 | |
BEGIN | |
/*Extrai o valor da coluna*/ | |
declare @columnName varchar(255) =(select top 1 trim(x.[value]) as [value] | |
from string_split(@fullName, '.') x | |
inner join sys.all_columns c on c.[name] = x.[value] and c.object_id =object_id(concat(@schemaName,'.', @tableName)) | |
where x.[value] <> @schemaName and x.[value]<>@tableName); | |
if(@tableName is null or @tableName ='') | |
begin | |
set @msg =concat('The column ',@columnName, ' on table ',@tableName, ' was found in schema ', @schemaName, ' on database ',db_name()); | |
THROW 90002, @msg, 1; | |
end | |
/*Verifica se existe comentario para essa coluna e decide se precisa add ou atualizar*/ | |
if not exists(SELECT top 1 * FROM fn_listextendedproperty (NULL, 'schema',@schemaName, 'table', @tableName, N'COLUMN', @columnName)) | |
exec sp_addextendedproperty N'MS_Description', @description, N'SCHEMA', @schemaName, N'TABLE', @tableName, N'COLUMN', @columnName; | |
else if 1=1 | |
exec sp_updateextendedproperty N'MS_Description', @description, N'SCHEMA', @schemaName, N'TABLE', @tableName, N'COLUMN', @columnName; | |
END | |
else | |
/*Entra nessa condição quando o texto enviado tem apenas schema e tabela*/ | |
/*Verifica se existe comentario para essa coluna e decide se precisa add ou atualizar*/ | |
if not exists(SELECT top 1 * FROM fn_listextendedproperty (NULL, 'schema',@schemaName, 'table', @tableName, default, default)) | |
EXEC sys.sp_addextendedproperty @name=N'MS_Description', | |
@value=@description , @level0type=N'SCHEMA', @level0name=@schemaName, | |
@level1type=N'TABLE',@level1name=@tableName | |
else | |
EXEC sys.sp_updateextendedproperty | |
@name=N'MS_Description', @value=@description, | |
@level0type=N'SCHEMA', @level0name=@schemaName, | |
@level1type=N'TABLE', @level1name=@tableName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment