Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to drop all extended properties containing a certain name

Has anyone written, or know of a query, that will find and drop all extended properties (Database, table and column level) that have a given name? I know I can drop one extended property via the sp_dropextendedproperty stored proc.

like image 242
Randy Minder Avatar asked Feb 04 '11 14:02

Randy Minder


People also ask

How do I drop an extended property?

Right click on Table/View in Object Explorer, select Properties. Select Extended Properties from the pane on the left. Select the extended properties and click Delete at the bottom right of the window.

What is MS_Description extended property?

Microsoft provides one extended property, MS_Description, which can be used from both Enterprise Manager and SSMS to provide a description of the object to which it is bound.

Which command in SQL is used to delete a table entire data and structure?

The SQL DROP TABLE Statement. The DROP TABLE statement is used to drop an existing table in a database.


3 Answers

For me, the script in Mikael's link didn't create drops for all extended properties, e.g. those for views. What did work was a script that Jamie Thomson has created, which you can download from this article.

I've pasted his script here (in full with acknowledgements, in-case the article is removed):

/*
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'
like image 177
Co7e Avatar answered Sep 30 '22 21:09

Co7e


A link to something that might be helpful for you. http://social.msdn.microsoft.com/Forums/en/transactsql/thread/747cb7c2-b176-4f75-9d92-c5a9360b4ed3

Generates a dynamic exec sp_dropextendedproperty statement for each property found.

like image 38
Mikael Eriksson Avatar answered Sep 30 '22 19:09

Mikael Eriksson


The previous answers failed to work correctly for me - they didn't remove extended properties for database triggers, xml schema collections or indexes. I found this script to delete extended properties (comments translated from Japanese using Google translate), which worked perfectly for me.

Warning: this actually removes all extended properties, rather than outputting a query to do so, as previous answers do. To change the behavior to output a query instead, change each EXEC (@sql) to 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
like image 34
Andrew Cupper Avatar answered Sep 30 '22 20:09

Andrew Cupper