4

有没有办法通过脚本更改/更新和删除/删除列描述?

sp_addextendedproperty用来添加描述,但它不允许更新。当我尝试使用相同的 sp 更新现有的描述值时,它会说“描述属性已经存在”

改变或删除/创建类似的解决方案对我来说都可以。

解决方案

在有用的答案和评论之后,您可以在下面看到我的最终解决方案。可以帮助某人。

create procedure sp_set_column_description (
    @schema varchar(256),
    @table varchar(256),
    @column varchar(256),
    @description varchar(256))
    as
begin
    if exists (
        select p.* 
        from
            sys.extended_properties p, 
            sys.columns c, 
            sys.tables t, 
            sys.schemas s
        where
            t.schema_id = s.schema_id and
            c.object_id = t.object_id and
            p.major_id = t.object_id and
            p.minor_id = c.column_id and
            p.name = N'MS_Description' and 
            s.name = @schema and
            t.name = @table and
            c.name = @column
    )
        exec sys.sp_updateextendedproperty 
            @level0type=N'SCHEMA', @level0name=@schema,
            @level1type=N'TABLE', @level1name=@table,
            @level2type=N'COLUMN', @level2name=@column,
            @name=N'MS_Description', @value=@description
    else
        exec sys.sp_addextendedproperty 
            @level0type=N'SCHEMA', @level0name=@schema,
            @level1type=N'TABLE', @level1name=@table,
            @level2type=N'COLUMN', @level2name=@column,
            @name=N'MS_Description', @value=@description
end

go

create procedure sp_drop_column_description (
    @schema varchar(256),
    @table varchar(256),
    @column varchar(256))
    as
begin
    if exists (
        select p.* 
        from
            sys.extended_properties p, 
            sys.columns c, 
            sys.tables t, 
            sys.schemas s
        where
            t.schema_id = s.schema_id and
            c.object_id = t.object_id and
            p.major_id = t.object_id and
            p.minor_id = c.column_id and
            p.name = N'MS_Description' and 
            s.name = @schema and
            t.name = @table and
            c.name = @column
    )
        exec sys.sp_dropextendedproperty 
            @level0type=N'SCHEMA', @level0name=@schema,
            @level1type=N'TABLE', @level1name=@table,
            @level2type=N'COLUMN', @level2name=@column,
            @name=N'MS_Description'
end
4

1 回答 1

7

结合 Steph Locke 的建议,您可以使用以下命令检查扩展 proc 是否存在:

if exists(
    SELECT  * 
    FROM    sys.extended_properties p
            join sys.columns c on p.major_id = c.object_id and p.minor_id = c.column_id 
    where   p.major_id = OBJECT_ID('yourtablename','table')
            and p.name = 'Description'  
)
于 2013-06-13T14:37:38.477 回答