Reputation: 11549
Is there a way to alter/update and drop/delete column descriptions via script?
I use sp_addextendedproperty
to add description, but it does not allow update. When I try to use same sp to update an existing description value, it says something like "description property already exists"
Both alter or drop/create like solutions are ok for me.
After helpful answers and comments, you can see my final solution below. May help someone.
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
Upvotes: 5
Views: 8415
Reputation: 3145
Combined with Steph Locke's suggestion, you can check for existence of the extended proc using:
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'
)
Upvotes: 7