我尝试将扩展属性添加到我的表和列中,但由于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
但是当我使用这个存储过程时,我得到了这个错误:
Msg 15135,Level 16,State 8,Procedure sp_addextendedproperty,Line 58
对象无效。 “dbo.P.P”不允许使用扩展属性,或者该对象不存在。
答案 0 :(得分:0)
我将其存储过程更改为marc_S建议,现在可以正常工作。
这可能是一个笨拙的错误,但我希望如果有人需要简化添加或更新扩展属性到一个存储过程中的表或列只有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