--sql批量删除指定数据表中的所有字段说明属性
--现象:执行select * from sys.extended_properties where value is null;显示出多条记录类似于[1 object_or_column 2372069 1 ms_description null]
--操作:执行delete from sys.extended_properties where value is null;提示[不允许对系统目录进行即席更新]
--功能说明:因为错误的操作将到了字段描述为null添加到系统数据表sys.extended_properties中,最终需要批量删除掉后再添加字段描述
declare @tablename nvarchar(250)--定义当前操作的数据表名变量
set @tablename='user'--指定要查询的数据表名称(此处的数据表名user需要更新为你的数据库中的已有数据表名)
declare @columnname nvarchar(250)--游标中的当前查询的字段名称
declare @columndescription nvarchar (250)--游标中的当前查询的字段说明
--声明读取数据表中所有字段说明的游标
declare mycursor cursor for select a.name,cast(g.value as nvarchar) from sys.columns a left join sys.extended_properties g on (a.object_id = g.major_id and a.column_id=g.minor_id) where object_id=object_id(''+@tablename+'') order by object_id,a.column_id
--打开游标
open mycursor
--从游标里取出数据赋值到约束名称变量中
fetch next from mycursor into @columnname,@columndescription
--如果游标执行成功
while (@@fetch_status=0)
begin
if(@columndescription is null)
begin
print '当前数据表['+@tablename+']字段['+@columnname+']对应的说明为空'
--删除字段的描述属性值为null的记录
--exec sp_dropextendedproperty 'ms_description','user',dbo,'table','数据表名称', 'column','字段名称'
exec sp_dropextendedproperty 'ms_description','user',dbo,'table',@tablename,'column',@columnname
end
else
begin
--查询当前找到的字段说明
print '当前数据表['+@tablename+']字段['+@columnname+']对应的说明是['+@columndescription+']'
end
--用游标去取下一条记录
fetch next from mycursor into @columnname,@columndescription
end
--关闭游标
close mycursor
--撤销游标
deallocate mycursor
,
