Home > Database > Mysql Tutorial > SQL批量删除指定数据表中的所有字段说明属性

SQL批量删除指定数据表中的所有字段说明属性

WBOY
Release: 2016-06-07 17:25:26
Original
1607 people have browsed it

declare @TableName nvarchar(250)--定义当前操作的数据表名变量set @TableName=

 --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

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template