从另一个存储过程调用 sp_addextendedproperty 时出错

Error when calling sp_addextendedproperty from another stored procedure

我尝试将扩展属性添加到我的 table 和列中,但是由于 SQL 服务器将添加和更新存储过程分开,并且我的情况需要太多未使用的参数,我决定为这 2 个存储过程创建包装器,这样我就可以创建仅包含在 table 和列级别添加或更新所需的 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

但是当我使用这个存储过程时,我得到了这个错误:

Msg 15135, Level 16, State 8, Procedure sp_addextendedproperty, Line 58
Object is invalid. Extended properties are not permitted on 'dbo.P.P', or the object does not exist.

我按照 marc_S 的建议更改了我的存储过程,现在可以使用了。

这可能是一个笨拙的错误,但我希望如果有人需要简化添加或更新扩展 属性 到 table 或一个存储过程中只有 3 个参数 "TableName" 的列, "ColumnName", "Description".

这是代码。

CREATE PROCEDURE setdescription 
    @tableName varchar(100),
    @columnName varchar(100) = NULL, 
    @objectDescription varchar(250)
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
                    --EXECUTE   sp_addextendedproperty @name=N'CXC_DESCRIPTION', @value=N@temp3, @level0type=N'user', @level0name=N'dbo', @level1type=N'table', @level1name=N@temp1, @level2type=N'column', @level2name=N@temp2
                END
            ELSE
                BEGIN
                    EXECUTE   sp_updateextendedproperty 'MY_DESCRIPTION', @objectDescription, 'user', dbo, 'table', @tableName, 'column', @columnName
                END
        END 
END
GO