【发布时间】:2013-06-13 12:05:23
【问题描述】:
有没有办法通过脚本更改/更新和删除/删除列描述?
我使用sp_addextendedproperty 添加描述,但它不允许更新。当我尝试使用相同的 sp 来更新现有的描述值时,它会说“描述属性已经存在”
更改或删除/创建类似的解决方案对我来说都可以。
解决方案
在有用的答案和 cmets 之后,您可以在下面看到我的最终解决方案。可以帮助别人。
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
【问题讨论】:
-
我相信有相应的更新过程:
sp_updateextendedproperty -
好像有
sp_updateextendedproperty和sp_dropextendedproperty。我会试试的。 -
sp_updateextendedpropertymsdn.microsoft.com/en-us/library/ms186885.aspx -
如果属性不存在,删除和更新过程会抛出异常,如果属性已经存在,则添加 sp 会抛出异常。我尝试了
try add catch update逻辑,但不知何故它会导致数据库锁定。执行“回滚”后,锁消失了,但是为这个简单的操作处理事务是不必要的复杂。我们最好检查扩展属性是否存在。
标签: sql sql-server