我尝试向我的表和列添加扩展属性,但由于 SQL Server 将添加和更新存储过程分开,并且我的情况需要太多未使用的参数,我决定为这两个存储过程创建包装器,以便我可以创建描述在表和列级别上添加或更新只需要 3 个参数。这是我当前的代码:
CREATE PROCEDURE sp_addorupdatedesc
@tableName varchar,
@columnName varchar = NULL,
@objectDescription varchar
AS
BEGIN
IF (@columnName IS NULL)
BEGIN
IF NOT EXISTS (SELECT 1 FROM fn_listextendedproperty (NULL, 'user', 'dbo', 'table', default, NULL, NULL) WHERE OBJNAME=@tableName)
BEGIN
EXECUTE sp_addextendedproperty 'MY_DESCRIPTION', @objectDescription, 'user', dbo, 'table', @tableName, default, NULL
END
ELSE
BEGIN
EXECUTE sp_updateextendedproperty 'MY_DESCRIPTION', @objectDescription, 'user', dbo, 'table', @tableName, default, NULL
END
END
ELSE
BEGIN
IF NOT EXISTS (SELECT 1
FROM sys.extended_properties AS ep
INNER JOIN sys.tables AS t ON ep.major_id = t.object_id
INNER JOIN sys.columns AS c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id
WHERE class = 1 AND T.NAME=@tableName AND C.name = @columnName)
BEGIN
EXECUTE sp_addextendedproperty 'MY_DESCRIPTION', @objectDescription, 'user', dbo, 'table', @tableName, 'column', @columnName
END
ELSE
BEGIN
EXECUTE sp_updateextendedproperty 'MY_DESCRIPTION', @objectDescription, 'user', dbo, 'table', @tableName, 'column', @columnName
END
END
END
GO
但是当我使用这个存储过程时,我得到了这个错误:
消息 15135,级别 16,状态 8,过程 sp_addextendedproperty,第 58 行
对象无效。'dbo.PP' 上不允许扩展属性,或者该对象不存在。