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 の提案と組み合わせると、次を使用して拡張プロシージャの存在を確認できます。

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 に答える