查询以删除包含特定名称的所有扩展属性

时间:2011-02-04 14:08:06

标签: sql-server tsql

是否有人编写或知道查询,会查找并删除具有给定名称的所有扩展属性(数据库,表和列级别)?我知道我可以通过sp_dropextendedproperty存储过程删除一个扩展属性。

4 个答案:

答案 0 :(得分:8)

对我来说,Mikael链接中的脚本没有为所有扩展属性创建丢弃,例如:那些观点。 Jamie Thomson创建的脚本是什么工作,可以从article下载。

我已经在这里粘贴了他的脚本(完整的确认,如果文章被删除):

/*
This script will generate calls to sp_dropextendedproperty for every
extended property that exists in your database.
Actually, a caveat: I don't promise that it will catch each and every 
extended property that exists, but I'm confident it will catch most of them!

It is based on this: 
http://blog.hongens.nl/2010/02/25/drop-all-extended-properties-in-a-mssql-database/ 
by Angelo Hongens.

Also had lots of help from this:
http://www.sqlservercentral.com/articles/Metadata/72609/
by Adam Aspin

Adam actually provides a script at that link to do something very similar
but when I ran it I got an error:
Msg 468, Level 16, State 9, Line 78
Cannot resolve the collation conflict between "Latin1_General_100_CS_AS" and "Latin1_General_CI_AS" in the equal to operation.

So I put together this version instead. 

Use at your own risk.

Jamie Thomson
2012-03-25
*/


/*Are there any extended properties? Let's take a look*/
select  *,OBJECT_NAME(major_id) from    sys.extended_properties xp

/*Now let's generate sp_dropextendedproperty statements for all of them.*/
--tables
set nocount on;
select 'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(xp.major_id) + ''''
from sys.extended_properties xp
join sys.tables t on xp.major_id = t.object_id
where xp.class_desc = 'OBJECT_OR_COLUMN'
and xp.minor_id = 0
union
--columns
select 'EXEC sp_dropextendedproperty
@name = '''+sys.extended_properties.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(extended_properties.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(extended_properties.major_id) + '''
,@level2type = ''column''
,@level2name = ''' + columns.name + ''''
from sys.extended_properties
join sys.columns
on columns.object_id = extended_properties.major_id
and columns.column_id = extended_properties.minor_id
where extended_properties.class_desc = 'OBJECT_OR_COLUMN'
and extended_properties.minor_id > 0
union
--check constraints
select  'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(cc.parent_object_id) + '''
,@level2type = ''constraint''
,@level2name = ''' + cc.name + ''''
from    sys.extended_properties xp
join sys.check_constraints cc       on  xp.major_id = cc.object_id
union
--check constraints
select  'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(cc.parent_object_id) + '''
,@level2type = ''constraint''
,@level2name = ''' + cc.name + ''''
from    sys.extended_properties xp
join sys.default_constraints cc     on  xp.major_id = cc.object_id
union
--views
select 'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''view''
,@level1name = ''' + object_name(xp.major_id) + ''''
from sys.extended_properties xp
join sys.views t on xp.major_id = t.object_id
where xp.class_desc = 'OBJECT_OR_COLUMN'
and xp.minor_id = 0
union
--sprocs
select 'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''procedure''
,@level1name = ''' + object_name(xp.major_id) + ''''
from sys.extended_properties xp
join sys.procedures t on xp.major_id = t.object_id
where xp.class_desc = 'OBJECT_OR_COLUMN'
and xp.minor_id = 0
union
--FKs
select  'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(cc.parent_object_id) + '''
,@level2type = ''constraint''
,@level2name = ''' + cc.name + ''''
from    sys.extended_properties xp
join sys.foreign_keys cc        on  xp.major_id = cc.object_id
union
--PKs
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '], @level1type = ''TABLE'', @level1name = [' + TBL.name + '] , @level2type = ''CONSTRAINT'', @level2name = [' + SKC.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.tables TBL
 INNER JOIN sys.schemas SCH
 ON TBL.schema_id = SCH.schema_id 
 INNER JOIN sys.extended_properties SEP
 INNER JOIN sys.key_constraints SKC
 ON SEP.major_id = SKC.object_id 
 ON TBL.object_id = SKC.parent_object_id 
WHERE SKC.type_desc = N'PRIMARY_KEY_CONSTRAINT'
union
--Table triggers
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '], @level1type = ''TABLE'', @level1name = [' + TBL.name + '] , @level2type = ''TRIGGER'', @level2name = [' + TRG.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.tables TBL
 INNER JOIN sys.triggers TRG
 ON TBL.object_id = TRG.parent_id 
 INNER JOIN sys.extended_properties SEP
 ON TRG.object_id = SEP.major_id 
 INNER JOIN sys.schemas SCH
 ON TBL.schema_id = SCH.schema_id
union
--UDF params
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '], @level1type = ''FUNCTION'', @level1name = [' + OBJ.name + '] , @level2type = ''PARAMETER'', @level2name = [' + PRM.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.objects OBJ
 ON SEP.major_id = OBJ.object_id 
 INNER JOIN sys.schemas SCH
 ON OBJ.schema_id = SCH.schema_id 
 INNER JOIN sys.parameters PRM
 ON SEP.major_id = PRM.object_id 
 AND SEP.minor_id = PRM.parameter_id 
WHERE SEP.class_desc = N'PARAMETER'
 AND OBJ.type IN ('FN', 'IF', 'TF') 
union
--sp params
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '], @level1type = ''PROCEDURE'', @level1name = [' + SPR.name + '] , @level2type = ''PARAMETER'', @level2name = [' + PRM.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.procedures SPR
 ON SEP.major_id = SPR.object_id 
 INNER JOIN sys.schemas SCH
 ON SPR.schema_id = SCH.schema_id 
 INNER JOIN sys.parameters PRM
 ON SEP.major_id = PRM.object_id 
 AND SEP.minor_id = PRM.parameter_id 
WHERE SEP.class_desc = N'PARAMETER'
union
--DB
SELECT 
'EXEC sys.sp_dropextendedproperty @name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
WHERE class_desc = N'DATABASE'
union
--schema
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.schemas SCH
 ON SEP.major_id = SCH.schema_id 
WHERE SEP.class_desc = N'SCHEMA'
union
--DATABASE_FILE
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''FILEGROUP'', @level0name = [' + DSP.name + '], @level1type = ''LOGICAL FILE NAME'', @level1name = ' + DBF.name + ' ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.database_files DBF
 ON SEP.major_id = DBF.file_id 
 INNER JOIN sys.data_spaces DSP
 ON DBF.data_space_id = DSP.data_space_id 
WHERE SEP.class_desc = N'DATABASE_FILE'
union
--filegroup
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''FILEGROUP'', @level0name = [' + DSP.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.data_spaces DSP
 ON SEP.major_id = DSP.data_space_id
WHERE DSP.type_desc = 'ROWS_FILEGROUP'

答案 1 :(得分:6)

指向可能对您有所帮助的内容的链接。 http://social.msdn.microsoft.com/Forums/en/transactsql/thread/747cb7c2-b176-4f75-9d92-c5a9360b4ed3

为找到的每个属性生成动态exec sp_dropextendedproperty语句。

答案 2 :(得分:3)

以前的答案对我来说无法正常工作 - 他们没有删除数据库触发器,xml架构集合或索引的扩展属性。我发现了这个script to delete extended properties(评论来自日语,使用谷歌翻译),这对我来说非常有用。

警告: 实际上 会删除所有扩展属性,而不是像以前的答案那样输出查询。要更改行为以输出查询,请将每个EXEC (@sql)更改为PRINT (@sql)

DECLARE @prop_name sysname
DECLARE @schema_name sysname
DECLARE @name sysname
DECLARE @name2 sysname
DECLARE @sql nvarchar(max)

-- Delete extended properties database
DECLARE PROP_CURSOR CURSOR FOR
SELECT name FROM sys.extended_properties where class = 0

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'USE [master];EXEC ' + QUOTENAME(DB_NAME()) + N'.sys.sp_dropextendedproperty @name=N''' + @prop_name + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Remove schema extension property
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(s.schema_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.schemas s ON s.schema_id = ep.major_id
WHERE ep.class = 3

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR

-- Delete extended properties of a file group
DECLARE PROP_CURSOR CURSOR FOR
SELECT f.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.filegroups f ON f.data_space_id = ep.major_id
WHERE ep.class = 20

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @name, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''FILEGROUP'',' + 
    N'@level0name=N''' + @name + ''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @name, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of a file
DECLARE PROP_CURSOR CURSOR FOR
SELECT f.name, sf.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.filegroups f ON f.data_space_id = ep.major_id
INNER JOIN sys.sysfiles sf ON sf.groupid = f.data_space_id
WHERE ep.class = 22

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @name, @name2, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''FILEGROUP'',' + 
    N'@level0name=N''' + @name + ''', ' + 
    N'@level1type=N''Logical File Name'',' + 
    N'@level1name=N''' + @name2 + ''''

    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @name, @name2, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR

-- Remove the expansion properties of the XML schema collection
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(x.schema_id), x.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.xml_schema_collections x ON x.xml_collection_id = ep.major_id
WHERE ep.class = 10

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''XML SCHEMA COLLECTION'',' + 
    N'@level1name=N''' + @name + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of a table
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(t.schema_id), OBJECT_NAME(t.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.tables t ON t.object_id = ep.major_id
WHERE ep.class = 1 AND ep.minor_id = 0

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''TABLE'',' + 
    N'@level1name=N''' + @name + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of view
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(v.schema_id), OBJECT_NAME(v.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.views v ON v.object_id = ep.major_id
WHERE ep.class = 1 AND ep.minor_id = 0

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''VIEW'',' + 
    N'@level1name=N''' + @name + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of the index
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(o.schema_id), OBJECT_NAME(o.object_id), i.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON o.object_id = ep.major_id AND o.type IN ('U')
INNER JOIN sys.indexes i ON i.object_id = ep.major_id AND i.index_id = ep.minor_id
WHERE ep.class = 7

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @name2, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''TABLE'',' + 
    N'@level1name=N''' + @name + N''', ' + 
    N'@level2type=N''INDEX'',' + 
    N'@level2name=N''' + @name2 + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @name2, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR

-- Delete extended properties of the index of the view
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(o.schema_id), OBJECT_NAME(o.object_id), i.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON o.object_id = ep.major_id AND o.type IN ('V')
INNER JOIN sys.indexes i ON i.object_id = ep.major_id AND i.index_id = ep.minor_id
WHERE ep.class = 7

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @name2, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''VIEW'',' + 
    N'@level1name=N''' + @name + N''', ' + 
    N'@level2type=N''INDEX'',' + 
    N'@level2name=N''' + @name2 + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @name2, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of the function
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(o.schema_id), OBJECT_NAME(o.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON o.object_id = ep.major_id AND o.type IN ('FN', 'TF')
WHERE ep.class = 1 AND ep.minor_id = 0

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''FUNCTION'',' + 
    N'@level1name=N''' + @name + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR

-- Delete extended properties of the parameters of the function
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(o.schema_id), OBJECT_NAME(o.object_id), p.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON o.object_id = ep.major_id AND o.type IN ('FN', 'TF')
INNER JOIN sys.parameters p ON p.object_id = ep.major_id AND p.parameter_id = ep.minor_id
WHERE ep.class = 2 

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @name2, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''FUNCTION'',' + 
    N'@level1name=N''' + @name + N''', ' + 
    N'@level2type=N''PARAMETER'',' + 
    N'@level2name=N''' + @name2 + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @name2, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of the stored procedure
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(p.schema_id), OBJECT_NAME(p.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.procedures p ON p.object_id = ep.major_id
WHERE ep.class = 1 AND ep.minor_id = 0

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''PROCEDURE'',' + 
    N'@level1name=N''' + @name + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of the parameters of a stored procedure
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(o.schema_id), OBJECT_NAME(o.object_id), p.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON o.object_id = ep.major_id AND o.type = 'P'
INNER JOIN sys.parameters p ON p.object_id = ep.major_id AND p.parameter_id = ep.minor_id
WHERE ep.class = 2 

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @name2, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', '+
    N'@level1type=N''PROCEDURE'',' + 
    N'@level1name=N''' + @name + N''', ' + 
    N'@level2type=N''PARAMETER'',' + 
    N'@level2name=N''' + @name2 + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @name2, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of DDL trigger
DECLARE PROP_CURSOR CURSOR FOR
SELECT t.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.triggers t ON t.object_id = ep.major_id AND t.parent_class = 0
WHERE ep.class = 1 AND ep.minor_id = 0

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @name, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''TRIGGER'',' + 
    N'@level0name=N''' + @name + N''''
    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @name, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- Delete extended properties of DML trigger
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(o.schema_id), p.name, OBJECT_NAME(o.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON o.object_id = ep.major_id AND o.type = 'TR'
INNER JOIN sys.objects p ON o.parent_object_id = p.object_id
WHERE ep.class = 1

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @name2, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', ' +
    N'@level1type=N''TABLE'',' + 
    N'@level1name=N''' + @name + N''', ' + 
    N'@level2type=N''TRIGGER'',' + 
    N'@level2name=N''' + @name2 + N''''

    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @name2, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR

-- Delete extended properties of the constraint
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(c.schema_id), OBJECT_NAME(c.parent_object_id), OBJECT_NAME(c.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.check_constraints c ON c.object_id = ep.major_id
WHERE ep.class = 1 and ep.minor_id = 0
UNION
SELECT SCHEMA_NAME(c.schema_id), OBJECT_NAME(c.parent_object_id), OBJECT_NAME(c.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.default_constraints c ON c.object_id = ep.major_id
WHERE ep.class = 1 and ep.minor_id = 0
UNION
SELECT SCHEMA_NAME(c.schema_id), OBJECT_NAME(c.parent_object_id), OBJECT_NAME(c.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.foreign_keys c ON c.object_id = ep.major_id
WHERE ep.class = 1 and ep.minor_id = 0
UNION
SELECT SCHEMA_NAME(c.schema_id), OBJECT_NAME(c.parent_object_id), OBJECT_NAME(c.object_id), ep.name
FROM sys.extended_properties ep
INNER JOIN sys.key_constraints c ON c.object_id = ep.major_id
WHERE ep.class = 1 and ep.minor_id = 0


OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @name2, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', ' +
    N'@level1type=N''TABLE'',' + 
    N'@level1name=N''' + @name + N''', ' + 
    N'@level2type=N''CONSTRAINT'',' + 
    N'@level2name=N''' + @name2 + N''''

    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @name2, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR


-- To delete an extended property of the column
DECLARE PROP_CURSOR CURSOR FOR
SELECT SCHEMA_NAME(o.schema_id), OBJECT_NAME(o.object_id), c.name, ep.name
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON o.object_id = ep.major_id
INNER JOIN sys.columns c ON c.object_id = o.object_id AND c.column_id = ep.minor_id
WHERE ep.class = 1

OPEN PROP_CURSOR

FETCH NEXT FROM PROP_CURSOR
INTO @schema_name, @name, @name2, @prop_name

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'EXEC sys.sp_dropextendedproperty ' + 
    N'@name=N''' + @prop_name + N''',' +
    N'@level0type=N''SCHEMA'',' + 
    N'@level0name=N''' + @schema_name + ''', ' +
    N'@level1type=N''TABLE'',' + 
    N'@level1name=N''' + @name + N''', ' + 
    N'@level2type=N''COLUMN'',' + 
    N'@level2name=N''' + @name2 + N''''

    EXEC (@sql)

    FETCH NEXT FROM PROP_CURSOR
    INTO @schema_name, @name, @name2, @prop_name
END
CLOSE PROP_CURSOR
DEALLOCATE PROP_CURSOR

答案 3 :(得分:0)

上面提到的脚本不支持函数,所以我将它添加到脚本中:

/*
This script will generate calls to sp_dropextendedproperty for every
extended property that exists in your database.
Actually, a caveat: I don't promise that it will catch each and every 
extended property that exists, but I'm confident it will catch most of them!

It is based on this: 
http://blog.hongens.nl/2010/02/25/drop-all-extended-properties-in-a-mssql-database/ 
by Angelo Hongens.

Also had lots of help from this:
http://www.sqlservercentral.com/articles/Metadata/72609/
by Adam Aspin

Adam actually provides a script at that link to do something very similar
but when I ran it I got an error:
Msg 468, Level 16, State 9, Line 78
Cannot resolve the collation conflict between "Latin1_General_100_CS_AS" and "Latin1_General_CI_AS" in the equal to operation.

So I put together this version instead. 

Use at your own risk.

Jamie Thomson
2012-03-25
*/


/*Are there any extended properties? Let's take a look*/
select  *,OBJECT_NAME(major_id) from    sys.extended_properties xp

/*Now let's generate sp_dropextendedproperty statements for all of them.*/
--tables
set nocount on;
select 'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(xp.major_id) + ''''
from sys.extended_properties xp
join sys.tables t on xp.major_id = t.object_id
where xp.class_desc = 'OBJECT_OR_COLUMN'
and xp.minor_id = 0
union
--columns
select 'EXEC sp_dropextendedproperty
@name = '''+sys.extended_properties.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(extended_properties.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(extended_properties.major_id) + '''
,@level2type = ''column''
,@level2name = ''' + columns.name + ''''
from sys.extended_properties
join sys.columns
on columns.object_id = extended_properties.major_id
and columns.column_id = extended_properties.minor_id
where extended_properties.class_desc = 'OBJECT_OR_COLUMN'
and extended_properties.minor_id > 0
union
--check constraints
select  'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(cc.parent_object_id) + '''
,@level2type = ''constraint''
,@level2name = ''' + cc.name + ''''
from    sys.extended_properties xp
join sys.check_constraints cc       on  xp.major_id = cc.object_id
union
--check constraints
select  'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(cc.parent_object_id) + '''
,@level2type = ''constraint''
,@level2name = ''' + cc.name + ''''
from    sys.extended_properties xp
join sys.default_constraints cc     on  xp.major_id = cc.object_id
union
--views
select 'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''view''
,@level1name = ''' + object_name(xp.major_id) + ''''
from sys.extended_properties xp
join sys.views t on xp.major_id = t.object_id
where xp.class_desc = 'OBJECT_OR_COLUMN'
and xp.minor_id = 0
union
--sprocs
select 'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''procedure''
,@level1name = ''' + object_name(xp.major_id) + ''''
from sys.extended_properties xp
join sys.procedures t on xp.major_id = t.object_id
where xp.class_desc = 'OBJECT_OR_COLUMN'
and xp.minor_id = 0
union
--functions
select 'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''function''
,@level1name = ''' + object_name(xp.major_id) + ''''
from sys.extended_properties xp
join sys.functions t on xp.major_id = t.object_id
where xp.class_desc = 'OBJECT_OR_COLUMN'
and xp.minor_id = 0
union
--FKs
select  'EXEC sp_dropextendedproperty
@name = '''+xp.name+'''
,@level0type = ''schema''
,@level0name = ''' + object_schema_name(xp.major_id) + '''
,@level1type = ''table''
,@level1name = ''' + object_name(cc.parent_object_id) + '''
,@level2type = ''constraint''
,@level2name = ''' + cc.name + ''''
from    sys.extended_properties xp
join sys.foreign_keys cc        on  xp.major_id = cc.object_id
union
--PKs
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '], @level1type = ''TABLE'', @level1name = [' + TBL.name + '] , @level2type = ''CONSTRAINT'', @level2name = [' + SKC.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.tables TBL
 INNER JOIN sys.schemas SCH
 ON TBL.schema_id = SCH.schema_id 
 INNER JOIN sys.extended_properties SEP
 INNER JOIN sys.key_constraints SKC
 ON SEP.major_id = SKC.object_id 
 ON TBL.object_id = SKC.parent_object_id 
WHERE SKC.type_desc = N'PRIMARY_KEY_CONSTRAINT'
union
--Table triggers
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '], @level1type = ''TABLE'', @level1name = [' + TBL.name + '] , @level2type = ''TRIGGER'', @level2name = [' + TRG.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.tables TBL
 INNER JOIN sys.triggers TRG
 ON TBL.object_id = TRG.parent_id 
 INNER JOIN sys.extended_properties SEP
 ON TRG.object_id = SEP.major_id 
 INNER JOIN sys.schemas SCH
 ON TBL.schema_id = SCH.schema_id
union
--UDF params
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '], @level1type = ''FUNCTION'', @level1name = [' + OBJ.name + '] , @level2type = ''PARAMETER'', @level2name = [' + PRM.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.objects OBJ
 ON SEP.major_id = OBJ.object_id 
 INNER JOIN sys.schemas SCH
 ON OBJ.schema_id = SCH.schema_id 
 INNER JOIN sys.parameters PRM
 ON SEP.major_id = PRM.object_id 
 AND SEP.minor_id = PRM.parameter_id 
WHERE SEP.class_desc = N'PARAMETER'
 AND OBJ.type IN ('FN', 'IF', 'TF') 
union
--sp params
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '], @level1type = ''PROCEDURE'', @level1name = [' + SPR.name + '] , @level2type = ''PARAMETER'', @level2name = [' + PRM.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.procedures SPR
 ON SEP.major_id = SPR.object_id 
 INNER JOIN sys.schemas SCH
 ON SPR.schema_id = SCH.schema_id 
 INNER JOIN sys.parameters PRM
 ON SEP.major_id = PRM.object_id 
 AND SEP.minor_id = PRM.parameter_id 
WHERE SEP.class_desc = N'PARAMETER'
union
--DB
SELECT 
'EXEC sys.sp_dropextendedproperty @name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
WHERE class_desc = N'DATABASE'
union
--schema
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''SCHEMA'', @level0name = [' + SCH.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.schemas SCH
 ON SEP.major_id = SCH.schema_id 
WHERE SEP.class_desc = N'SCHEMA'
union
--DATABASE_FILE
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''FILEGROUP'', @level0name = [' + DSP.name + '], @level1type = ''LOGICAL FILE NAME'', @level1name = ' + DBF.name + ' ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.database_files DBF
 ON SEP.major_id = DBF.file_id 
 INNER JOIN sys.data_spaces DSP
 ON DBF.data_space_id = DSP.data_space_id 
WHERE SEP.class_desc = N'DATABASE_FILE'
union
--filegroup
SELECT 
'EXEC sys.sp_dropextendedproperty @level0type = N''FILEGROUP'', @level0name = [' + DSP.name + '] ,@name = ''' + REPLACE(CAST(SEP.name AS NVARCHAR(300)),'''','''''') + ''''
FROM sys.extended_properties SEP
 INNER JOIN sys.data_spaces DSP
 ON SEP.major_id = DSP.data_space_id
WHERE DSP.type_desc = 'ROWS_FILEGROUP'